Android SimpleAdapter 中添加按鈕響應事件,getView的重寫

來源:互聯網
上載者:User

標籤:android   style   blog   io   color   ar   os   java   sp   

Andriod 裡面的ListView是一個顯示列表資料的控制項,常用適配器SimpleAdapter進行綁定,綁定代碼如下:
ListView lstView = (ListView) this.findViewById(R.id.listView1);SimpleAdapter adapter=new SimpleAdapter(context, data, resource, from, to);lstView.setAdapter(adapter);

  

ListView 列表中的元素的單擊事件響應:
OnItemClickListener mItemClickListener = new OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {         // TODO Auto-generated method stub    }};listView.setOnItemClickListener(mItemClickListener);

 

對於Button ,ImageButton這樣的按鈕控制項實現單事件,接下來我們看看如何?這樣的功能:

繼承SimpleAdapter進行重寫getView方法

為什麼不繼承BaseAdapter適配器進行處理,因為BaseAdapter是一個非常基礎的基類,對於一般的TextViwe ,ImageView,Button控制項的資料繫結都沒有實現

java代碼:

private class MySimpleAdapter extends SimpleAdapter {    @Override    public View getView(int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub        View v = super.getView(position, convertView, parent);                    ImageButton btn=(ImageButton) v.findViewById(R.id.icon);        btn.setTag(position);        btn.setOnClickListener(new OnClickListener() {                        @Override        public void onClick(View v) {           // TODO Auto-generated method stub                               mDataList.remove((int)v.getTag());           notifyDataSetChanged();           Toast.makeText(getApplicationContext(), "µ¥»÷ÎÒÁË"+v.getTag(), 1).show();                         }      });      return v;    }     public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {          super(context, data, resource, from, to);          // TODO Auto-generated constructor stub      }    }

當單擊某個按鈕時,按鈕事件需要知道單擊的是ListView的哪一項,可以為每個Button對象添加一個Tag,我們這裡用listview的項position的索引作為tag

 

Android SimpleAdapter 中添加按鈕響應事件,getView的重寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.