ListView,在Android中使用颜色代码数组更改每一行

时间:2016-02-15 20:52:28

标签: android arrays listview

我试图改变每行的颜色,我有2个数组。一个有颜色名称,另一个有颜色代码。

我有一个带有颜色名称的ListView,名称存储在一个String数组中。

String[] colourNames;
String[] colourCodes;

 protected void onCreate(Bundle savedInstanceState) {
     colourNames = getResources().getStringArray(R.array.listArray);
     colourCodes = getResources().getStringArray(R.array.listValues);

     ListView lv = (ListView) findViewById(R.id.listView);

    ArrayAdapter aa = new ArrayAdapter(this, R.layout.activity_listview, colourNames);

    lv.setAdapter(aa);

    for(int i=0; i<colourCodes.length; i++)
        lv.getChildAt(i).setBackgroundColor(Color.parseColor(colourCodes[i]));
 }

在arrays.xml中:

<string-array name="listArray">
        <item>aliceblue</item>
        <item>antiquewhite</item>
        <item>aquamarine</item>
        <item>azure</item>
</string-array>
<string-array name="listValues">
        <item>00f0f8ff</item>
        <item>00faebd7</item>
        <item>007fffd4</item>
        <item>00f0ffff</item>
 </string-array>

应用程序在lv.getChildAt(i).setBackgroundColor(Color.parseColor(colourCodes[i]));

崩溃

3 个答案:

答案 0 :(得分:0)

试试此代码

Function CountCellsByColor2(rData As String, cellRefColor As Range) As Long
    Dim indRefColor As Long
    Dim cellCurrent As Variant
    Dim cntRes As Long
    Dim test As Long

    'Application.Volatile
    cntRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Interior.Color

    For Each cellCurrent In Split(rData, ",")
        If indRefColor = Range(cellCurrent).Interior.Color Then
            cntRes = cntRes + 1
        End If
    Next cellCurrent

    CountCellsByColor2 = cntRes
End Function

答案 1 :(得分:0)

问题是此时ListView没有任何孩子。如果要更改子项的显示方式,则需要创建自己的Adapter实现并覆盖getView()。在这种情况下,您可以简单地将ArrayAdapter子类化并传递给您的颜色数组(或者让它在适配器中加载颜色,就像我一样),然后根据位置选择颜色。

此外,您还可以将颜色设为integer array

<integer-array name="listValues">
    <item>0xfff0f8ff</item>
    <item>0xfffaebd7</item>
    <item>0xff7fffd4</item>
    <item>0xfff0ffff</item>
</integer-array>

public class ColorsAdapter extends ArrayAdapter<String> {

    private int[] mColors;

    public ColorsAdapter(Context context) {
        super(context, R.layout.activity_listview,
                context.getResources().getStringArray(R.array.listArray));
        mColors = context.getResources().getIntegerArray(R.array.listValues);
    {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        int color = mColors[position % mColors.length]; // might as well be safe
        view.setBackgroundColor(color);
        return view;
    }
}

我还强烈建议观看有关ListView如何工作的视频:The World of ListView。此外,现在人们正在转向RecyclerView;您无需这样做,但无论哪种方式,此视频都可以帮助您了解这些组件的行为方式。

答案 2 :(得分:0)

您必须编写自己的自定义ArrayAdapter。

首先写一个颜色类:

<强> color.java:

public class color {
    private String name;
    private String color;

    public color(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

然后列出项目布局:

<强> list_item_layout.xml

<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:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

最后编写自定义适配器:

<强> ColorListAdapter.java:

public class ColorListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<color> mColorList;

public ColorListAdapter(Activity activity, List<color> mColorList) {
    mInflater = (LayoutInflater) activity.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    this.mColorList = mColorList;
}

@Override
public int getCount() {
    return mColorList.size();
}

@Override
public Object getItem(int position) {
    return mColorList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    // Get item_layout:
    rowView = mInflater.inflate(R.layout.list_item_layout, null);

    // Get TextView from item_layout:
    TextView textView =
            (TextView) rowView.findViewById(R.id.name);

    // Get color and text from current position set TextView
    color myColor = mColorList.get(position);
    textView.setText(myColor.getName());
    textView.setBackgroundColor(Color.parseColor(myColor.getColor()));
    return rowView;
  }
}

这些是 MainActivity.java activity_main.xml

<强> MainActivity.java:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<color> colorList = new ArrayList<>();

        // Add color objects:
        colorList.add(new color("RED", "#FF0000"));
        colorList.add(new color("GREEN", "#00FF00"));
        colorList.add(new color("BLUE", "#0000FF"));
        colorList.add(new color("MY BEST", "#013583"));


        // Add list to your custom adapter
        ListView myListView = (ListView) findViewById(R.id.liste);
        ColorListAdapter mAdapter = new ColorListAdapter(this, colorList);

        // Set Adapter
        myListView.setAdapter(mAdapter);

    }

<强> activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/liste"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

</RelativeLayout>