今天遇到了一个比较让我头疼的问题,不过追根揭底只是我对listview理解的不够透彻罢了,
闲言少叙,说说我遇到的问题吧:
上篇随笔我写了关于listview的使用,如果你也已经写好了列表那么恭喜这一篇对你也有用
当然如果你还没有搭好可以先去看看我的上一篇(上篇地址:http://www.cnblogs.com/wobeinianqing/p/5064740.html)
相信很多新手都跟我一样总想这把被点击的item的内容传递给相应对的详情页,其实这种想法是很错误的
listview的功能只管展示给他的数据并不理会数据的内容的。其监听方法
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
}
各部分参数:
//adapterView相当于listview Y适配器的一个指针,可以通过它来获得Y里装着的一切东西,再通俗点就是说告诉你,你点的是Y,不是X
// view是你点的b这个view的句柄,就是你可以用这个view,来获得b里的控件的id后操作控件
// i 是b在Y适配器里的位置(生成listview时,适配器一个一个的做item,然后把他们按顺序排好队,在放到listview里,意思就是这个b是第position号做好的)
// l 是b在listview Y里的第几行的位置(很明显是第2行)
可以看出通过这些参数并不能得到对应对的数据的那应该怎么拿到相关数据呢?
其实很简单如果你是静态数据(自己在前面整个数组,里面放些数据)可以直接利用postion(就是上面的 i )对应这数组的下标找到相应对的数据
但如果你不是其实也是这么个理论,只要你能拿到数据,再根据postion对应下标就好了
下面细说下:
public class MyAdapter extends BaseAdapter { private LayoutInflater mLayoutInflater; // 映射数据 private List<ItemBean> mDataList; //private long mSumTime; private int id; public MyAdapter(Context context, List<ItemBean> list) { mLayoutInflater = LayoutInflater.from(context); mDataList = list; } // 获取数据量 @Override public int getCount() { return mDataList.size(); } // 获取对应ID项对应的Item @Override public Map<String , String> getItem(int position) { ItemBean itemBean = mDataList.get(position); Map<String , String> map = new HashMap<String , String>(); map.put("title" , itemBean.itemTitle); map.put("explain" , itemBean.itemContent); map.put("price" , itemBean.itemPrice); map.put("senderPhone" , itemBean.itemSenderPhone); map.put("sendTime" , itemBean.itemSendTime); map.put("endTime" , itemBean.itenEndtime); map.put("sender" , itemBean.itemSender); map.put("receiver" , itemBean.itemReceiver); return map; } // 获取对应项ID @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // 文艺式 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取纳秒时间 更加精确 ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); //由于我们只需要将XML转化为View,并不涉及到具体的布局,所以第二个参数通常设置为null convertView = mLayoutInflater.inflate(R.layout.tasklistitem, null); holder.title = (TextView) convertView.findViewById(R.id.task_title); holder.explain = (TextView) convertView.findViewById(R.id.task_explain); holder.price = (TextView) convertView.findViewById(R.id.task_price); holder.faraway = (TextView) convertView.findViewById(R.id.task_distance); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // 取出bean对象 ItemBean bean = mDataList.get(position); // 设置控件的数据 holder.title.setText(bean.itemTitle); holder.explain.setText(bean.itemContent); holder.price.setText(bean.itemPrice); holder.faraway.setText(bean.itemFaraway); return convertView; // 文艺式 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>163258 20 } // ViewHolder用于缓存控件 class ViewHolder{ public TextView title;//标题 public TextView explain;//说明 public TextView price;//价格 public TextView faraway;//距离 } }
// 获取对应ID项对应的Item @Override public Map<String , String> getItem(int position) { ItemBean itemBean = mDataList.get(position); Map<String , String> map = new HashMap<String , String>(); map.put("title" , itemBean.itemTitle); map.put("explain" , itemBean.itemContent); map.put("price" , itemBean.itemPrice); map.put("senderPhone" , itemBean.itemSenderPhone); map.put("sendTime" , itemBean.itemSendTime); map.put("endTime" , itemBean.itenEndtime); map.put("sender" , itemBean.itemSender); map.put("receiver" , itemBean.itemReceiver); return map; }
我获取的是服务器数据,在自定义adapter 中实现上面的getitem方法,这个方法是默认都要复写的方法(继承baseadapter)继承baseadapter要实现四个方法:
// 获取数据量 @Override public int getCount() { return mDataList.size(); } // 获取对应ID项对应的Item @Override public Object getItem(int position) { return postion; } // 获取对应项ID @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) {}
看官方多贴心,我们只要重写上面的getitem方法改成你需要的数据类型
上面我复写的方法可以看到我要传递的内容较多所以我用了个map。我们还看到
数据是从bean中拿到的所以我贴出bean的代码:
package com.example.guwie.yaoren.ui.Bean; /** * Created by guwei on 15-12-17. */ public class ItemBean { public String itemTitle; public String itemContent; public String itemPrice; public String itemFaraway; public String itemSenderPhone; public String itemSendTime; public String itenEndtime; public String itemSender; public String itemReceiver; /*** * title 标题 * content 说明 * price 价格 * faraway 距离 * */ public ItemBean(String itemTitle, String itemContent, String itemPrice, String itemFaraway, String itemSenderPhone, String itemSendTime, String itenEndtime, String itemSender, String itemReceiver) { this.itemTitle = itemTitle; this.itemContent = itemContent; this.itemPrice = itemPrice; this.itemFaraway = itemFaraway; this.itemSenderPhone = itemSenderPhone; this.itemSendTime = itemSendTime; this.itenEndtime = itenEndtime; this.itemSender = itemSender; this.itemReceiver = itemReceiver; } }
bean的数据从哪来呢?当然从服务器啦。我们看下获取代码:
/*** * 获取list数据 * * */ public void getTaskList(int pages) { //Toast.makeText(TaskList.this , pages+"" , Toast.LENGTH_SHORT).show(); RequestQueue mreuestQueut = Volley.newRequestQueue(this); JsonObjectRequest jsreq = new JsonObjectRequest(Request.Method.GET, TASK_LIST_DATE+"page="+pages, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Message msg = new Message(); try { int code = jsonObject.getInt("code"); if(code == 200){ int size = jsonObject.getJSONArray("data").length(); for(int i=0; i<size ; i++){ JSONObject dataObject = jsonObject.getJSONArray("data").getJSONObject(i); String title = dataObject.getString("title"); String price = dataObject.getString("fee"); String explain = dataObject.getString("description"); String senderPhone = dataObject.getString("senderPhone"); String sendTime = dataObject.getString("sendTime"); String endTime = dataObject.getString("endTime"); String sender = dataObject.getString("sender"); String receiver = dataObject.getString("receiver"); dataList.add(new ItemBean(title , explain , price , "3km" , senderPhone , sendTime, endTime , sender ,receiver )); } maxpage = ++page; page = page-1; }else if(code == 205){ //已无更多数据 //maxpage = page; page = maxpage; msg.what=0x001; mHandler.sendMessage(msg); }else if(code == 404){ //未找到数据 msg.what = 0x002; mHandler.sendMessage(msg); } } catch (JSONException e) { e.printStackTrace(); Log.i("----------------", "客户端内部错误"); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.i("----------------","服务器出错"+volleyError.toString()); } }); mreuestQueut.add(jsreq); }
使用volley获取数据请自行百度
获取的是json数据每次传5条如下:
{
"code":200,
"message":"ok",
"data":[
{
"id":10,
"title":"qwd",
"description":"qwdqwd",
"type":1,
"fee":31,
"senderPhone":"123123",
"sender":23,
"receiver":null,
"sendTime":"2015-12-16 21:11:01",
"endTime":"2015-12-16 21:11:03",
"longtitude":null,
"latitude":null,
"finished":0
},
{
"id":11,
"title":"eqwrwr",
"description":"werwer",
"type":2,
"fee":32,
"senderPhone":null,
"sender":24,
"receiver":null,
"sendTime":"2015-12-18 11:35:41",
"endTime":"2015-12-18 11:35:43",
"longtitude":null,
"latitude":null,
"finished":0
},
{
"id":12,
"title":"sadfef",
"description":"ewqr",
"type":1,
"fee":21,
"senderPhone":"122113",
"sender":24,
"receiver":26,
"sendTime":"2015-12-18 11:35:27",
"endTime":"2015-12-18 11:35:46",
"longtitude":null,
"latitude":null,
"finished":1
},
{
"id":13,
"title":"而通过了",
"description":"435435",
"type":3,
"fee":12,
"senderPhone":"21215",
"sender":26,
"receiver":null,
"sendTime":"2015-12-18 11:36:40",
"endTime":"2015-12-23 11:37:03",
"longtitude":null,
"latitude":null,
"finished":0
},
{
"id":14,
"title":"wefwef",
"description":"23wef",
"type":2,
"fee":12,
"senderPhone":"213214",
"sender":27,
"receiver":25,
"sendTime":"2015-12-18 16:29:42",
"endTime":"2015-12-18 16:29:44",
"longtitude":null,
"latitude":null,
"finished":1
}
]
}
上面处理json数据 的方法是官方对的解析方法当然还有其他方法。通过循环吧数据每次add给自定义的adapter让它
将数据组装给list展示,然后就是对每个item进行监听了:
/** * item 监听事件 * * */ mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Map<String ,String> map = new MyAdapter(TaskList.this , dataList).getItem(--i);//不减1会出现错位现象 Intent intent = new Intent(TaskList.this, MorInformation.class); Bundle bundle = new Bundle(); bundle.putString("title" , map.get("title")); bundle.putString("explain" , map.get("explain")); bundle.putString("price" , map.get("price")); bundle.putString("senderPhone" , map.get("senderPhone")); bundle.putString("sendTime" , map.get("sendTime")); bundle.putString("endTime" , map.get("endTime")); bundle.putString("sender" , map.get("sender")); bundle.putString("receiver" , map.get("receiver")); intent.putExtras(bundle); startActivity(intent); } });
这里看到adapter传来的map又被我用bundle包了一遍。其实我也没办法因为intent 并不能传递map 只好借助bundle了
这样在目标页面就已经能拿到这些数据了。什么?bundle如何拿数据?看看吧:
/** * 放入相关数据 * * */ private void SetData() { Bundle bundle = this.getIntent().getExtras(); title.setText(bundle.getString("title")); explain.setText(bundle.getString("explain")); price.setText(bundle.getString("price")); phone.setText(bundle.getString("senderPhone")); date.setText(bundle.getString("endTime")); add.setText(bundle.getString("explain")); }
其实很简单是吧, Bundle bundle = this.getIntent().getExtras(); 获取bundle对象
然后通过bundle.getString("xxx")的方法来拿相应的数据。
就这么多了