首先自定义适配器(Adapter)
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
private List
然后在onCreate中使用连接适配器ListView视图对象和数组数据
ListView listView;
MyAdapter adapter;
private List> data1 = new ArrayList>();
listView = (ListView)findViewById(R.id.list_view);
adapter = new MyAdapter(PMListActivity.this,data1);
listView.setAdapter(adapter);
然后与后端交互代码
public void onFooterRefresh(PullToRefreshView view) { //上拉加载更多
mPullToRefreshView.postDelayed(new Runnable() {
@Override
public void run() {
if(lastpg > offset){
getPMList();// 加载更多数据
}else{
Toast.makeText(PMListActivity.this, "已是最后一页!!"
, Toast.LENGTH_SHORT).show();
}
mPullToRefreshView.onFooterRefreshComplete();
}
}, 1000);
}
public void onHeaderRefresh(PullToRefreshView view) { //下拉刷新
mPullToRefreshView.postDelayed(new Runnable() {
@Override
public void run() {
data1.clear();
offset = 1;
getPMList();// 加载更多数据
//设置更新时间
SimpleDateFormat sDateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
String date = sDateFormat.format(new Date());
mPullToRefreshView.onHeaderRefreshComplete("最近更新:"+date);
//mPullToRefreshView.onHeaderRefreshComplete();
}
},1000);
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) { //处理登陆结果
super.handleMessage(msg);
Bundle data = msg.getData();
String Result = data.getString("Result"); //获取结果
Log.i("mylog","请求结果-->" + Result);
JSONObject jsonObject;
try {// 解析后端传回的JSON数据
jsonObject = new JSONObject(Result);
JSONArray entry_list = jsonObject.getJSONArray("entry_list");
lastpg = jsonObject.getInt("lastpg");
for (int i = 0; i < entry_list.length(); i++) {
JSONObject result = entry_list.getJSONObject(i);
Log.i("listlog",result.getString("id"));
String name_value_list = result.getString("name_value_list");
jsonObject = new JSONObject(name_value_list);
JSONObject message = jsonObject.getJSONObject("message");
JSONObject sender = jsonObject.getJSONObject("sender");
JSONObject received = jsonObject.getJSONObject("received");
HashMap map = new HashMap();
map.put("lvw_custom_name", sender.getString("value"));
map.put("lvw_custom_description", message.getString("value"));
map.put("received", received.getString("value"));
map.put("pm_id", result.getString("id"));
data1.add(map); //追加到数组data1
}
offset++;//页码自增
adapter.notifyDataSetChanged();//改变adapter
// SimpleAdapter adapter = new SimpleAdapter(PMListActivity.this, data1, R.layout.lvw_custom,
// new String[] {"lvw_custom_name","lvw_custom_description"},
// new int[] { R.id.lvw_custom_name, R.id.lvw_custom_description});
// //位ListView设置Adapter
// listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
};
/*
* 获取消息列表
*/
public void getPMList() {
Runnable getPMListThread = new Runnable(){
@Override
public void run() {
Map params = new HashMap();
String rest_data="{\"session\":\"" + CurrentUserId + "\"," +
"\"module_name\":\""+module_name+"\"," +
"\"query\":\""+searchtext+"\"," +
"\"offset\":" + offset + "," +
"\"max_results\":" + RowsPerPageInListViews + "," +
"\"sel\":\"1\"}";
System.out.println("map->json:" + rest_data);
params.put("rest_data", rest_data);
params.put("method","get_entry_list");
params.put("fromJava","1");
String loginResult = HttpUtils.sendPostResquest(rest_url, params, "utf-8");//向后端发送请求
Log.v("url response", "true="+loginResult);
Message msg = new Message();
Bundle data = new Bundle();
data.putString("Result",loginResult);
msg.setData(data);
handler.sendMessage(msg);
}
};
new Thread(getPMListThread).start();
}
/*
* 读取消息
*/
public void receivePM(final String id) {
Runnable receivePMThread = new Runnable(){
@Override
public void run() {
Map params = new HashMap();
String rest_data="{\"session\":\"" + CurrentUserId + "\"," +
"\"module_name\":\""+module_name+"\"," +
"\"record\":\""+id+"\"}";
System.out.println("map->json:" + rest_data);
params.put("rest_data", rest_data);
params.put("method","receivePM");
params.put("fromJava","1");
String loginResult = HttpUtils.sendPostResquest(rest_url, params, "utf-8");
Log.v("url response", "true="+loginResult);
}
};
new Thread(receivePMThread).start();
}
最后在onCreate中给listView添加点击事件
//添加点击事件
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView> arg0, View convertView, int position,
long arg3) {
HashMap map = new HashMap();
ViewHolder holder =(ViewHolder)convertView.getTag();
Log.w("onItemClick_received: ", holder.received.getText().toString());
if(holder.received.getText().equals("0")){
map.put("lvw_custom_name", data1.get(position).get("lvw_custom_name"));
map.put("lvw_custom_description", data1.get(position).get("lvw_custom_description"));
map.put("received", "1");
map.put("pm_id", data1.get(position).get("pm_id"));
data1.remove(position);//移除选中项
data1.add(position,map);//在选中位置添加新项
adapter.notifyDataSetChanged();//改变adapter
receivePM(holder.pm_id.getText().toString()); //提交到后端改变数据
}
}
});