简体   繁体   中英

Better way to implement custom views in a listview with simpleadapter?

I have a value called tags which is a comma separated list of words. I want to put this into nicely designed "tag-buttons".

The below works. However the line ((LinearLayout) view).removeAllViews(); seems like an ugly fix for not adding the tags multiple times every time adapter.notifyDataSetChanged(); is called after i load more rows with a setOnScrollListener()

Any suggestion to "best practice" here, or at least a more good looking solution?

adapter = new SimpleAdapter(activity,data,
    R.layout.list_transactions,
    new String[] {"comment", "amount","date","tags","category"},
    new int[] { R.id.comment, R.id.amount,R.id.date,R.id.tags_container,R.id.category }
  );


SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Object object, String value) {
    //Log.d(TAG,"view.toString()= "+ view.toString());
    if (view.getId() == R.id.tags_container)
    {

        String[] tags = value.split(",");

        ((LinearLayout) view).removeAllViews();
        for (String tag : tags) {
        View v = createTagView(activity.getLayoutInflater(),tag);
        ((LinearLayout) view).addView(v);

        }
        return true;
    }
    return false;
    }
};

An alternative solution is to maintain a set of Strings that contains to the tags of the views being displayed. Instead of removing all views use

if(set.contains(tag))

to determine if the view should be added. Again this is just an alternative solution. I am not sure about the relative performance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM