android:ListView:convertView.setTag()來設定資料

來源:互聯網
上載者:User
void android.view.View.setTag(Object tag)public void setTag (Object tag)Since: API Level 1

Sets the tag associated with this view. A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.

Parametersan Object to tag the view with


also see

  • getTag()
  • setTag(int,
    Object)
    public View getView(int position, View convertView, ViewGroup parent) {            final int cc = position;            ViewHolder holder = null;            if (convertView == null) {                holder = new ViewHolder();                convertView = mInflater.inflate(R.layout.itemrow, null);                holder.tagcolor = (ImageView) convertView.findViewById(R.id.itemrowiamge);                holder.itemname = (TextView) convertView.findViewById(R.id.itemrowtext);                holder.cb = (CheckBox) convertView.findViewById(R.id.itemrowcb);                convertView.setTag(holder);            } else {                holder = (ViewHolder) convertView.getTag();            }             //擷取ViewHolder中所填入的資料            holder.tagcolor.setBackgroundResource((Integer) itemList.get(position).get("IMG"));            holder.itemname.setText((String) itemList.get(position).get("ITEMNAME"));            holder.ckd = (String) itemList.get(position).get("CHECKED");             //單個項目中的CheckBox的按鍵監聽函數            holder.cb.setOnClickListener(new View.OnClickListener() {                public void onClick(View v) {                    setC(cc);//當被按下後調用函數改變狀態                }            });            if (holder.ckd.equals("t"))                holder.cb.setChecked(true);            else                holder.cb.setChecked(false);            return convertView;        }

設一個holder類

import android.widget.CheckBox;import android.widget.ImageView;import android.widget.TextView; //ViewHolder類用以儲存每一條項目所需的資料public class ViewHolder {    public ImageView tagcolor;//單條項目中的圖片    public TextView itemname;//單條項目中的文本    public CheckBox cb;//單條項目中的CheckBox    public String ckd;//單條項目中用以標識CheckBox狀態的字串變數}

即可。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.