The Adapter of ListView: Just adapt data to view, don’t do anything else

原创 2013年12月05日 11:15:51

The design of SimpleAdapter is not good in my opinion.

An adapter should just adapter the data to view, not care to inflate xml to create Layout View, hold the member Views of the layout view, and, fill the datas to each member View one by one. SimpleAdapter does these things all together. It is high coupling design.

The solution is a ItemViewHolder class, which inflate and hold the layout view and its member views, and parse datas to fill them  to member views one by one. And the most cool is, it’s layout view of ItemViewHolder set “this” as its tag. So that you can get the ItemViewHolder instance of the convertView in getView(…, View convertView, …) function.

See below code snippets.

In Adapter, passed in a List as data list.

List<Data> dataList;

and override getView() like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ItemViewHolder item = null;

    if(convertView == null) {
        item = new ItemViewHolder(context);
        convertView = item.getLayoutView();
    } else {
        item = (ItemViewHolder)convertView.getTag();
    }

    item.setItemData(dataList.get(position));

    return convertView;
}

ItemViewHolder is the views holder of the convertView.

public class ItemViewHolder {

    //The layout View of the item of the ListView.
    private View layoutView = null;
    //The member Views to display data.
    private TextView textView = null;
    private ImageView imageView = null;
    … …

    public ItemViewHolder (Context context) {
        super(context);

         initUI();
    }

    public View getLayoutView() {
        return layoutView;
    }

    public void setItemData(Data data) {
        textView.setText(data.getText());
        imageView.setImage(data.getImage());
        … …
    }

    private void initUI() {
         LayoutInflater inflater = LayoutInflater.from(mContext);
         layoutView = inflater.inflate(R.layout.item_view_layout, null);
        //
        textView = (TextView) layoutView.findViewById(R.id.textview);
        imageView = (ImageView)layoutView.findViewById(R.id.imageview);
       //This is the most important code.
       layoutView.setTag(this);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

The file “***” couldn’t be opened because you don’t have permission to view it

前两天在运行demo时遇到The file “UIWebViewDemo” couldn’t be opened because you don’t have permission to view i...

How to do...well...anything...in DB2 SQL

1. Selecting a value SQL Server: SELECT 'Hello, world!' (1 row(s) affected) DB2: SELECT 'He...

ccah-500 第45题 You want to minimize the chance of data loss in your cluster. What should you do

45.You have A 20 node Hadoop cluster, with 18 slave nodes and 2 master nodes running HDFS High Avail...

wamp环境下出现You don’t have permission to access/on this server提示

****本地搭建wamp,输入http://127.0.0.1访问正常,当输入http://localhost/,apache出现You don’t have permission to access...

I don’t want to see another “using namespace xxx;” in a header file ever again

原文链接:http://www.lonecpluspluscoder.com/2012/09/i-dont-want-to-see-another-using-namespace-xxx-in-a-h...

I don’t need to test my programs. I have an error-correcting modem.

I don’t need to test my programs. I have an error-correcting modem. ~ Om I. Baud       Leavin...

zz - reprint of "How do I implement a data bound dialog box?"

the original post of this article is available here: http://bea.stollnitz.com/blog/?p=15   this pos...

Christian Louboutin femme We'll a range of to do with merchandise category could be the by the name

We are frequently a multi functional collection about related merchandise category is the fact calle...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The Adapter of ListView: Just adapt data to view, don’t do anything else
举报原因:
原因补充:

(最多只允许输入30个字)