0

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;
    }
};
jonaz
  • 3,764
  • 2
  • 20
  • 20
  • If I were you I would make an assumption about the average number of tags that I would have for a row and add as many tag views as that number from the start in the row layout(mainly to avoid/reduce the creation of views each time). Then in the `ViewBinder` I would simply hide/add extra views depending on the exact number of tags for that row(and maybe remove some). – user Jun 25 '13 at 19:38

1 Answers1

0

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.

Scott
  • 1,652
  • 1
  • 13
  • 10