Android set listItem background colour from String Array

后端 未结 3 1117
故里飘歌
故里飘歌 2021-01-24 05:37

Does anyone know how to programatically set the background of a list item from a String Array? I have two string arrays one is the title for the text view and the other contain

相关标签:
3条回答
  • 2021-01-24 05:56

    Use a custom adapter as follows:

    public class MyAdapter extends ArrayAdapter<String> {
    
        Context context; 
        int layoutResourceId;    
        String data[] = null;
        String color[] = null;
    
        public MyAdapter(Context context, int layoutResourceId, String[] data, String[] color) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
            this.color = color;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            StringHolder holder = null;
    
            if(row == null)
            {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);
    
                holder = new StringHolder();
                holder.txtTitle = (TextView)row.findViewById(R.id.text1);
    
                row.setTag(holder);
            }
            else
            {
                holder = (StringHolder)row.getTag();
            }
    
            holder.txtTitle.setText(data[position]);
            row.setBackgroundColor(Color.parseColor(color[position]));
    
            return row;
        }
    
        static class StringHolder
        {
            TextView txtTitle;
        }
    }
    

    Then set the adapter of the listView as follows:

    ArrayAdapter<String> adapter = new MyAdapter(this, R.layout.drawer_list_item, items, colors);
    menuList.setAdapter(adapter);
    

    Update Just noticed: you also need to update the layout file. It seems to be defining the id of the textview wrongly.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
            >
    
        <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="18dp"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:gravity="center" />
    
    </RelativeLayout> 
    

    Here is the result:

    enter image description here

    0 讨论(0)
  • 2021-01-24 06:09

    This is what i have done for 2 colors.

    i have kept 2 colors string in color.xml and i have used it in the custom adapter of the listview

    if (position % 2 == 0)
    
                convertView.setBackgroundResource(R.color.color1);
            else
                convertView.setBackgroundResource(R.color.color2);
    
    0 讨论(0)
  • 2021-01-24 06:12

    You can use a custom array adapter. Extend ArrayAdapter and override getView to return a textview with background color according to the parameter position.

    0 讨论(0)
提交回复
热议问题