简体   繁体   English

更改特定列表视图项的颜色

[英]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 文本颜色需要根据R.id.tvattendance的值进行更改

The above snippet is in the onPostExecute function of the class extending AsyncTask 上面的代码片段在扩展AsyncTask的类的onPostExecute函数中

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

You need to implement custom adapter You can view the full implementation i did in this opensource project 您需要实现自定义适配器您可以查看我在此开源项目中所做的完整实现

Customadapter in Fragment 片段中的Customadapter

  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. :使用mListView.getFirstVisiblePosition()mListView.getLastVisiblePosition()以及mListView.getChildAt(...)获得对该行的引用,然后根据需要对其进行自定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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