Change textcolor programmatically in listview who depends of arrayadapter

6.2k views Asked by At

I have the next estructure:

ArrayList<String> arrayList = new ArrayList<>();
list = (ListView) findViewById(R.id.list1);

arrayAdapter = new ArrayAdapter<String>(INGR_NOTAS.this, R.layout.list_item_clickable, R.id.txtitem, arrayList);

list.setAdapter(arrayAdapter);

where the layout of the listview comes from list_item_clickable.xml and the textiview "txtitem" inside it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtitem"
        android:textColor="@android:color/white"
        android:background="@android:drawable/editbox_dropdown_dark_frame"
        android:textAlignment="center" />
</LinearLayout>

This estructure is working in a lot of classes and like Textview says, the textcolor is always white, but now I need to change the textcolor inside of the listview depending of the value of the text (sometimes white, sometimes red). So, How can I add this change without add a big impact in the rest of the code a long all the App????

Thanks for any advice!!

4

There are 4 answers

16
Oleg On BEST ANSWER

try this:

ArrayList<String> arrayList = new ArrayList<>();
list = (ListView) findViewById(R.id.list1);

arrayAdapter = new ArrayAdapter<String>(INGR_NOTAS.this, R.layout.list_item_clickable, R.id.txtitem, arrayList){
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view = super.getView(position, convertView, parent);
        ((TextView)view.findViewById(R.id.txtitem)).setTextColor(position % 2 == 0 ? Color.WHITE : Color.RED); // here can be your logic
        return view;
    };
};

list.setAdapter(arrayAdapter);

hope it helps

0
David Wasser On

Create your own adapter that extends ArrayAdapter and override getView(). It isn't that complicated.

0
Mohd. Merajuddin shaikh On

Use textView.setTextColor(getResources().getColor(R.id.red);, if based on array list item. Then create a class that extends ArrayAdapter, in getView(,,) method set the color.

example:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item1, null);
                holder = new ViewHolder();
                holder.textView = (TextView)convertView.findViewById(R.id.text);
holder.textView.setTextColor(getResources().getColor(R.id.your_color);
                convertView.setTag(holder);
            } 
            return convertView;
        }
0
Darkhan ZD On

With SimpleAdapter it is also possible. Here is my solution.

List<Map<String, String>> detailsList = debitorRecord.getDetails();

    String[] from = {"created_at","comment","sum"};
    int[] to = {R.id.detailCreatedAt, R.id.detailComment, R.id.detailSum};
    ListAdapter listAdapter = new SimpleAdapter(this, detailsList, R.layout.list_detail_item, from, to)
    {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView textView = (TextView) view.findViewById(R.id.detailSum);
            //Do what u want here, in my case I 
            Map<String, String> currentRow = detailsList.get(position);
            int type = Integer.parseInt(currentRow.get("type"));
            if (DebitorRecord.TYPE_PAYMENT == type) textView.setTextColor(Color.GREEN);
            else textView.setTextColor(Color.RED);

            return view;
        }
    };

    listView.setAdapter(listAdapter);