从字符串数组的Andr​​oid组的listItem背景颜色数组、字符串、颜色、背景

2023-09-08 10:16:09 作者:白衬衫〆白色的格调

有谁知道如何编程设置列表项的背景从一个字符串数组?我有两个字符串数组一个是文本视图的标题和其他有一个颜色的参考。我加的标题阵列到阵列适配器,并且正显示出,但现在我想改变背景颜色为每个项目在数组中

我的继承人布局

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =50dp
      >

    <的TextView
        机器人:ID =@机器人:ID / text1中
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_marginTop =10dp
        机器人:layout_marginLeft =18dp
        机器人:文本=大文本
        机器人:textAppearance =机器人:ATTR / textAppearanceLarge
        机器人:重力=中心/>

< / RelativeLayout的>
 

我的继承人code到目前为止

  ListView的菜单列表=(ListView控件)findViewById(R.id.listView1);
        的String []项目= {"menuItem1","menuItem2","menuItem3","menuItem4","menuItem4","menuItem5","menuItem6","menuItem7","menuItem8"};
  串[]颜色= {#FFFFFF,#000000,#ffffBB,#ffffDD,#ff654d,#FFFFFF,#FFFFFF,#FFFFFF,#FFFFFF };
        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.drawer_list_item,android.R.id.text1,项目);
        menuList.setAdapter(适配器);
 

解决方案

使用自定义适配器如下:

 公共类MyAdapter扩展ArrayAdapter<字符串> {

    上下文语境;
    INT layoutResourceId;
    字符串数据[] = NULL;
    串色[] = NULL;

    公共MyAdapter(上下文的背景下,INT layoutResourceId,的String []数据的String []颜色){
        超级(上下文,layoutResourceId,数据);
        this.layoutResourceId = layoutResourceId;
        this.context =背景;
        this.data =数据;
        this.color =颜色;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        查看排= convertView;
        的StringHolder持有人= NULL;

        如果(行== NULL)
        {
            LayoutInflater充气=((活动)上下文).getLayoutInflater();
            行= inflater.inflate(layoutResourceId,父母,假);

            持有人=新的StringHolder();
            holder.txtTitle =(TextView中)row.findViewById(R.id.text1);

            row.setTag(保持器);
        }
        其他
        {
            支架=(的StringHolder)row.getTag();
        }

        holder.txtTitle.setText(数据[位置]);
        row.setBackgroundColor(Color.parseColor(颜色[位置]));

        返回行;
    }

    静态类的StringHolder
    {
        TextView的txtTitle;
    }
}
 

然后设置的ListView的适配器如下:

  ArrayAdapter<字符串>适配器=新MyAdapter(这一点,R.layout.drawer_list_item,项目,颜色);
menuList.setAdapter(适配器);
 

更新只注意到:你还需要更新布局文件。这似乎是错误定义的TextView的ID。

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                的xmlns:工具=htt​​p://schemas.android.com/tool​​s
                机器人:layout_width =match_parent
                机器人:layout_height =50dp
        >

    <的TextView
            机器人:ID =@ + ID / text1中
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_marginTop =10dp
            机器人:layout_marginLeft =18dp
            机器人:文本=大文本
            机器人:textAppearance =机器人:ATTR / textAppearanceLarge
            机器人:重力=中心/>

< / RelativeLayout的>
 

下面是结果:

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 contains a colour reference. I have added the titles Array to an array Adapter and that is showing but now i want to change the background colour for each item in the array

heres my layout

<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="@android: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> 

heres my code so far

     ListView menuList = (ListView) findViewById(R.id.listView1);
        String[] items = {"menuItem1","menuItem2","menuItem3","menuItem4","menuItem4","menuItem5","menuItem6","menuItem7","menuItem8"};  
  String[] colors = {"#ffffff","#000000","#ffffBB","#ffffDD","#ff654d","#ffffff","#ffffff","#ffffff","#ffffff"};   
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1, items);
        menuList.setAdapter(adapter);

解决方案

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:

 
精彩推荐
图片推荐