Sanket
Sanket

Reputation: 93

Change color of specific listview item

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

Upvotes: 0

Views: 67

Answers (3)

frogatto
frogatto

Reputation: 29285

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.

Upvotes: 0

Neo
Neo

Reputation: 1379

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  

Upvotes: 0

user2668855
user2668855

Reputation:

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

Upvotes: 3

Related Questions