Android更改listview项目文本颜色

Moh*_*zen 6 android listview

我正在尝试根据标志更改列表视图中的某些项目文本颜色(或背景颜色).经过长时间的搜索,我没有找到如何做到这一点,我在改变颜色的具体行动后调用下面的循环:

ListView _listView = (ListView) findViewById(R.id.listView2);

        for (int i=0;i<=_listView.getCount();i++)
        {   
           if(Falg == True)
           {
            //here i want to change the list view item color or backgroud color
           }
        }
Run Code Online (Sandbox Code Playgroud)

Adn*_*lla 26

您可以覆盖Array适配器的getView方法并更改颜色:

ArrayAdapter<String> adapter = 
                    new ArrayAdapter<String>(getApplicationContext(), 
                    android.R.layout.simple_list_item_1, 
                    myList) {

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

        View view = super.getView(position, convertView, parent);
        TextView text = (TextView) view.findViewById(android.R.id.text1);

        if (flag == True) {
            text.setTextColor(Color.BLACK);
        }   

        return view;
    }
};
Run Code Online (Sandbox Code Playgroud)