0

I need to change the text color of some specific items of the listview.

lstdetails = (ListView) findViewById(R.id.lstdetails);

ListAdapter adapter = new SimpleAdapter(
    Details.this, details_list,
    R.layout.Details, new String[] {
            "lecture_key", "date",
            "total_lect_int", "attendance" },
    new int[] { R.id.tvKey, R.id.tvdate,
            R.id.tvtotallect, R.id.tvattendance });

lstdetails.setAdapter(adapter);

Text color needs to be changed on the basis of the value of R.id.tvattendance

The above snippet is in the onPostExecute function of the class extending AsyncTask

frogatto
  • 28,539
  • 11
  • 83
  • 129
Sanket
  • 83
  • 1
  • 6

3 Answers3

3

You need to implement custom adapter, extend from BaseAdapter class, and change color in getView method

0

You need to implement custom adapter You can view the full implementation i did in this opensource project

Customadapter in Fragment

  private class listviewAdapter extends BaseAdapter {


   @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      int value= listAdapter.get(position); // or whatever data type is your value

      if (value==0){
            //change color of the textview at runtime here
        }else{

            //or some other color
        }

     }




   }


i am using this inside a fragment and it works perfectly  
Neo
  • 1,359
  • 14
  • 34
0

I think there is two option for you to accomplish it.

  1. Normal Way : Using a custom adapter and customize each row as you wish. For this way, please take a look at here

  2. Ugly Way! : Use mListView.getFirstVisiblePosition() and mListView.getLastVisiblePosition() as well as mListView.getChildAt(...) to obtain a reference to that row and then customize it as you wish.

Community
  • 1
  • 1
frogatto
  • 28,539
  • 11
  • 83
  • 129