简体   繁体   中英

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

您需要实现自定义适配器,从BaseAdapter类扩展,并在getView方法中更改颜色

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  

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM