Skip to content Skip to sidebar Skip to footer

Android Listview, Change Attributes Of Particular View

on my listview I am scrolling to a particular position of it like this getListView().smoothScrollToPosition(scrollposition+1); but I also want to 'highlight' this view. How do I ac

Solution 1:

I ran into this issue a few weeks ago.

You should not use the getChildAt() function of the ListView. If you use getChildAt(5), for example, it will pull the 5th view that is visible.

To solve the issue, I had to create a custom adapter that overrode getView() and set the colors based on what I sent across.

publicclassRunAdapterextendsSimpleAdapter {

publicRunAdapter(Context context, List<HashMap<String, String>> items,
        int resource, String[] from, int[] to) {
    super(context, items, resource, from, to);
}

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    Viewview=super.getView(position, convertView, parent);
    TextViewtxtView= (TextView) view.findViewById(R.id.lstTurnout);
        if (txtView.getText().toString().contains("BLU")) {
            txtView.setBackgroundColor(Color.rgb(0x00, 0x00, 0xaa));
            txtView.setText(txtView.getText().toString().replace("BLU", ""));
        } else {
            txtView.setBackgroundColor(Color.rgb(0xaa, 0x00, 0x00));
            txtView.setText(txtView.getText().toString().replace("RED", ""));
        }
  }

It isn't the prettiest method, but it works! Depending on what color I wanted the TextView to be, I passed 'BLU', or 'RED' Then in the getView, I check to see which the string contains and change the color accordingly.

Hope I was some help, Good Luck!

Edit Constructor as per request

Solution 2:

getChildAt needs an index, not a scrollpostion.

Regards, Stphane

Post a Comment for "Android Listview, Change Attributes Of Particular View"