编辑listView中的行,ArrayAdapter-Android

编辑listView中的行,ArrayAdapter-Android,android,listview,row,android-arrayadapter,Android,Listview,Row,Android Arrayadapter,如何更改单行中文本的颜色 下面是我设置适配器和调用listView的代码: ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), R.layout.row, rez); getListView().setAdapter(adapter); ArrayAdapter=newArrayAdapter(getListView().getContext(),R.

如何更改单行中文本的颜色

下面是我设置适配器和调用listView的代码:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), R.layout.row, rez);
 getListView().setAdapter(adapter);
ArrayAdapter=newArrayAdapter(getListView().getContext(),R.layout.row,rez);
getListView().setAdapter(适配器);

在布局文件夹中有一个名为row.xml的布局。在那里,您将有一个文本视图,您只需添加:

     android:color="@color/your-color"

添加到该xml元素。

您需要创建自己的适配器,并在所需位置指定文本颜色:

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, String[] strings) {
        super(context, resource, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView textView = view.findViewById(R.id.textview);
        if (position == positionYouWantToColor) {
            textView.setTextColor(getResources().getColor(R.id.mycolor));
        } else {
            textView.setTextColor(getResources().getColor(R.id.black));
        }
        return view;
    }
}
公共类MyAdapter扩展了ArrayAdapter{
公共MyAdapter(上下文上下文、int资源、字符串[]字符串){
super(上下文、资源、字符串);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=super.getView(位置、转换视图、父级);
TextView TextView=view.findViewById(R.id.TextView);
如果(位置==位置YouwantToColor){
setextcolor(getResources().getColor(R.id.mycolor));
}否则{
setextcolor(getResources().getColor(R.id.black));
}
返回视图;
}
}

是的,我知道,但不是所有的行都应该是相同的颜色。我需要给你3种不同的颜色。哦,你需要创建一个自定义适配器,然后我得到一个错误,字符串[]无法转换为列表。我选择了一个构造函数,因为在你的问题中没有指定变量“rez”是什么。但是您可以更改构造函数。参见编辑