②给ListView设置Adapter,并实现接口的Load方法
package com.mg.message.ui;
import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.AdapterView.OnItemClickListener; import com.mg.core.base.BaseActivity; import com.mg.core.net.OperateCode; import com.mg.core.net.ThreadMessage; import com.mg.message.R; import com.mg.message.data.GetMessageData; import com.mg.message.domain.Message; import com.mg.message.ui.adapter.ContentAdapter; import com.mg.message.utils.ViewUtil; import com.mg.message.view.LX_LoadListView; import com.mg.message.view.LX_LoadListView.ILoadListener; /* * 获得留言*/ public class LX_GetContentActivity extends BaseActivity implements ILoadListener { private LX_LoadListView listView; private List<Message> totalMessages=new ArrayList<Message>(); private static ContentAdapter myAdapter; private int totalSize; List<Message> messages=new ArrayList<Message>(); @Override protected void initEvents() { listView.setOnItemClickListener(new OnItemClickListener() //进入删除页 { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Bundle bundle = new Bundle(); //传递多参数时使用Bundle bundle.putString("ID", messages.get(position).getID()); bundle.putString("UContent", messages.get(position).getContent().toString()); bundle.putString("UTime", messages.get(position).getCreateTime()); startCOActivity(LX_DeleteActivity.class,bundle); finish(); } }); //修改留言 listView.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3) { Bundle bundle = new Bundle(); //传递多参数时使用Bundle bundle.putString("ID", messages.get(position).getID()); startCOActivity(LX_AddMessageActivity.class,bundle); finish(); return false; } }); }
@Override protected void initViews() { setContentView(R.layout.lx_get_content_activity); mTopBar.setVisibility(View.VISIBLE); mTopBar.SetTvName("查看留言"); listView=(LX_LoadListView)findViewById(R.id.list_content); listView.setInterface(this); myAdapter=new ContentAdapter(messages); listView.setAdapter(myAdapter); } @Override protected void onResume() { super.onResume(); showToast("onResume", false); sendToBackgroud(OperateCode.i_getMessageInfo); } @Override protected void receiveRequest(ThreadMessage threadMessage) { hideProgressDialog(); OperateCode operateCode = threadMessage.getOperateCode(); List<Message> messageList; switch (operateCode) { //查看留言 case i_getMessageInfo: messageList=GetMessageData.getMessagesList(); if(ViewUtil.isNotEmpty(messageList)) { totalMessages.clear(); messages.clear(); totalMessages.addAll(messageList); totalSize=totalMessages.size(); getData(); myAdapter.notifyDataSetChanged(); } break; default: break; } } //listview初始化条数 private void getData() { try { if (totalSize<9) { messages.addAll(GetMessageData.getMessagesList()); showToast("留言小于9加载完毕", false); } else { messages.addAll(GetMessageData.getMessagesList().subList(0, 8)); } } catch (Exception e) { } } //加载数据 private void getLoadData() { int lastsize=totalSize-messages.size(); if(messages.size()<totalSize) { if (lastsize<5) { messages.clear(); messages.addAll(GetMessageData.getMessagesList()); showToast("加载项<5加载完毕", false); } else { messages.addAll(totalMessages.subList(messages.size(), messages.size()+5)); } } else { messages.clear(); messages.addAll(GetMessageData.getMessagesList()); showToast("全部加载完毕", false); } myAdapter.notifyDataSetChanged(); } @Override public void onLoad() { android.os.Handler handler=new android.os.Handler(); handler.postDelayed(new Runnable() { @Override public void run() { getLoadData();//获取更多数据 listView.loadComplete();//通知listview加载完毕 } }, 2000); } }
|