リストビューの行に色を設定するのに小さな問題があります。以下のコード (アダプター用) を使用して、listiview の色を設定しました。うまく機能する配列に2つの色を入れました。ここで、リストに 5 色を追加したいと思います。しかし、このコードは機能しません。これどうやってするの。
Custom_List_Adapter.java
private class Custom_List_Adapterextends BaseAdapter {
//Defining the background color of rows. The row will alternate between green light and green dark.
private int[] colors = new int[] { 0xAAf6ffc8, 0xAA538d00, 0xAAf6ddc8, 0xAA238d00, 0xAA788d00 };
private LayoutInflater mInflater;
//The variable that will hold our text data to be tied to list.
private String[] data;
public Custom_List_Adapter(Context context, String[] items) {
mInflater = LayoutInflater.from(context);
this.data = items;
}
@Override
public int getCount() {
return data.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
//A view to hold each row in the list
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.headline);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(data[position]);
//Set the background color depending of odd/even colorPos result
int colorPos = position % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
return convertView;
}
}