I'm trying to change the row color of my listView in customAdapter. There's an array of integer that include 0 and 1, I'm trying to read from the array and change the color of rows like this:
0 = white
1 = yellow
but it shows a yellow color in all rows. This is my CustomAdapter:
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<RowItem> {
ArrayList<Integer> test;
Context context;
public CustomAdapter(Context context, int resourceId, List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
SqliteHelper checkFav = new SqliteHelper(context);
checkFav.open();
test = checkFav.getFavForList();
checkFav.close();
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row_layout, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.pTxt);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
int color0 = Color.YELLOW;
int colorDefault = Color.WHITE;
switch (test.get(position)){
case 0:
convertView.setBackgroundColor(colorDefault);
break;
case 1:
convertView.setBackgroundColor(color0);
}
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
And this is my row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/active_icon" />
<TextView
android:id="@+id/pTxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"
android:gravity="right"/>
</LinearLayout>
Thanks in advance.
Fast solution (not very nice code, but works):
@Override
public int getItem(int position){
return test.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int color0 = ....
int color1 = ....
int colorDefault = ...
switch (test.get(position)) {
case 0:
convretview.setBackgroundColor(color0);
break;
case 1:
convretview.setBackgroundColor(color1);
break;
default:
convretview.setBackgroundColor(colorDefault);
}
...
}
getView()
is called for each row in a list so you should not loop over all items in test
but only handle the one denoted by position
and you should do that after (not in) the if (convertView == null)
block.
Edit:
getView()
should do the following things:
- Check if convertView isn't null (if it is, create a new View as you did)
- Check if convertView is of the right type (this only matters if more than one kind of View is used in the list which is not the case here). If the type is wrong, create a new View.
Now we have a valid View for the list row.
- Retrieve the data which affects how to display this row. In your case this would be the result of
test.get(position)
. Theposition
is the number of the requested row (starting with 0 at the top of the ListView). - Adjust the View according to your data (you did this in the for-loop but you should do it only once for the requested entry at
position
). - Return the View
In more complex situations you may have to do the third step before the second but not here.
来源:https://stackoverflow.com/questions/16862608/android-listview-row-color