首页 新闻 搜索 专区 学院

android 中ListView设置行选中

1
[已解决问题] 解决于 2017-11-22 10:38

activity里取到的列表

 

list = new ArrayList<Map<String, Object>>();
                Map<String, Object> map = new HashMap<String, Object>();
                try {
                    JSONArray jsonArray = new JSONArray(content);
                    for(int i=0; i<10; i++)
                    {
                        map = new HashMap<String, Object>();
                        map.put("itemNo", i+"itemNo");
                        map.put("lineName", i+"lineName");
                        map.put("patrolTime", i+"patrolTime");
                        map.put("groupName", i+"groupName");
                        map.put("guid",i+"guid");//不显示
                        list.add(map);
                    }
                }catch(JSONException ex)
                {
                    Log.e("JSON Error: ", ex.toString());
                }
                ListView listView = (ListView) findViewById(R.id.list);
                PatrolAcceptsAdapter patrolAcceptsAdapter = new PatrolAcceptsAdapter(PatrolAcceptsActivity.this, list,
                        R.layout.sync_accepts_item, new String[] { "itemNo", "lineName","patrolTime","groupName","guid"}, new int[] { R.id.itemNo,
                        R.id.itemLine,R.id.itemTime,R.id.itemGroup,R.id.guid});
                listView.setAdapter(patrolAcceptsAdapter);

adapter里传数据

 1 public class PatrolAcceptsAdapter extends BaseAdapter {
 2 
 3     private PatrolAcceptsActivity mContext;
 4     //单行的布局
 5     private int mResource;
 6     //列表展现的数据
 7     private List<? extends Map<String, ?>> mData;
 8     //Map中的key
 9     private String[] mFrom;
10     //view的id
11     private int[] mTo;
 1  public PatrolAcceptsAdapter(PatrolAcceptsActivity context, List<? extends Map<String, ?>> data,
 2                                 int resource, String[] from, int[] to){
 3         mContext = context;
 4         mData = data;
 5         mResource = resource;
 6         mFrom = from;
 7         mTo = to;
 8     }
 9 
10     @Override
11     public int getCount() {
12         return mData.size();
13     }
14 
15     @Override
16     public Object getItem(int position) {
17         return mData.get(position);
18     }
19 
20     @Override
21     public long getItemId(int position){
22         return position;
23     }
 1  @Override
 2     public View getView(final int position, View convertView, ViewGroup parent){
 3         final PatrolAcceptsAdapter.ViewHolder holder;
 4 
 5         if(convertView == null){
 6             //使用自定义的list_items作为Layout
 7             convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
 8             //使用减少findView的次数
 9             holder = new PatrolAcceptsAdapter.ViewHolder();
10             holder.itemNo = ((TextView) convertView.findViewById(mTo[0]));
11             holder.lineName = ((TextView) convertView.findViewById(mTo[1]));
12             holder.patrolTime = ((TextView) convertView.findViewById(mTo[2]));
13             holder.groupName = ((TextView) convertView.findViewById(mTo[3]));
14             holder.guid = ((TextView) convertView.findViewById(mTo[4]));
15 
16             //设置标记
17             convertView.setTag(holder);
18         }else{
19             holder = (PatrolAcceptsAdapter.ViewHolder) convertView.getTag();
20         }
21         //设置数据
22         final Map<String, ?> dataSet = mData.get(position);
23         if (dataSet == null) {
24             return null;
25         }
26         //获取该行数据
27         final Object itemNo = dataSet.get(mFrom[0]);
28         final Object lineName = dataSet.get(mFrom[1]);
29         final Object patrolTime = dataSet.get(mFrom[2]);
30         final Object groupName = dataSet.get(mFrom[3]);
31         final Object guid = dataSet.get(mFrom[4]);
32 
33         holder.itemNo.setText(itemNo.toString());
34         holder.lineName.setText(lineName.toString());
35         holder.patrolTime.setText(patrolTime.toString());
36         holder.groupName.setText(groupName.toString());
37         holder.guid.setText(guid.toString());
38  
39         return convertView;
40     }
41 
42     /**
43      * ViewHolder类
44      */
45     static class ViewHolder {
46         TextView itemNo;
47         TextView lineName;
48         TextView patrolTime;
49         TextView groupName;
50         TextView guid;//id
51 
52 
53     }    

现在,设置点击事件,只是单个设置

 holder.itemNo.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {}
});

有什么方法,请问可以点击就选中一行?并取到值
类似于下边默认选中第一行的操作
//默认选中第一行
if(selectedPosition == position){
convertView.setBackgroundResource(R.drawable.timer_bg);
}

可以怎么设置点击选中一行的操作???多谢
选中一行,而不是单个?


 
mx夕酉的主页 mx夕酉 | 菜鸟二级 | 园豆:222
提问于:2017-11-17 11:17
< >
分享
最佳答案
0

在getView中给convertView添加点击事件

奖励园豆:5
授之以渔 | 小虾三级 |园豆:1100 | 2017-11-17 15:51
其他回答(1)
0

好久不搞安卓了,看起来代码好像没问题啊。帮顶

数据酷软件 | 园豆:130 (初学一级) | 2017-11-17 13:02

多谢

支持(0) 反对(0) mx夕酉 | 园豆:222 (菜鸟二级) | 2017-11-17 14:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册