开发者

ListView item setTextColor modified other elements

I have a ListView backed by SimpleCursorAdapter and custom ViewBinder. I want to make items in this listview change their color on clicking. If I do that in the OnClickListener - it works paritally, changing the color of the item clicked, and of the items down the list, each 7th (I guess, the period depends on on the viewable area of the listview).

Can anyone suggest how to deal with this? Or, maybe point to a more elegant way of making items in the listView selectable?

Thank you.

UPD: (sorry for bad formatting - this is the first time I post a question):

Below is how I try to make an item in the ListView "selected":

    private void setupListView(final ListView lv) {

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int position, final long id) {
                RelativeLayout layout = (RelativeLayout) view;
                int color;
                if (conditionMet) {
                      color = R.color.gray;
                 } else {
                      color = R.color.red;
                 }

                 for(int i = 0; i < layout.getChil开发者_如何转开发dCount(); i++) {
((TextView)layout.getChildAt(i)).setTextColor(getResources().getColor(color)); 
    }

                 return;
            }}

This is how I init the adapter:

        final SimpleCursorAdapter adapter =
                new SimpleCursorAdapter(
                        this,
                        itemId,
                        cursor,
                        from,
                        to
                );
        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                final TextView textView = (TextView) view;
// do necessary conversions
                return true;
            }
        });
listView.setAdapter(adapter);


You can use the property android:listSelector to set the theme or any drawable or color of the currently selected item in a list.


Since no other answer, and, I think, I had some troubles with the suggestion below, I post how I did it:

  1. I store ids of the items clicked in a special map

  2. in the listview onclick I check whether the id of the just clicked item is in the map: if yes, I remove it and make the item and its children color A, otherwise I add the id to the map and set the color to B

    public void onItemClick(AdapterView<?> adapterView, View view, int position, final long id) {
                Context ctx = MainActivity.this;
                    RelativeLayout layout = (RelativeLayout) view;
                    try {
    
                            int color;
                            if (items.containsKey(id)) {
                                items.remove(id);
                                color = R.color.gray;
                                tempIds.remove(id);
                            } else {
                                items.put(id, sum);
                                color = R.color.red;
                                tempIds.add(id);
                            }
    
    
                        for (int i = 0; i < layout.getChildCount(); i++) {
                            final TextView textView = (TextView) layout.getChildAt(i);
                            textView.setTextColor(getResources().getColor(color));
                        }
                    } catch (ParseException e) {
                        Log.e(MainActivity.class.toString(), "Exception parsing", e);
                    }
                    return;
                }
    

    }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜