I am trying to give alternate background color to my list view. I am using cursor adapter. Here is my CustomCursorAdapter class
CustomCursorAdapter.java
public class CustomCursorAdapter extends CursorAdapter {
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.single_row_item, parent, false);
return retView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name);
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View row = super.getView(position, convertView, parent);
if (position % 2 == 0)
row.setBackgroundColor(Color.parseColor("#191919"));
else
row.setBackgroundColor(Color.parseColor("#323232"));
return row;
}
}
In getView(); I have setBackgroundColor to view. but they are not get assigned properly only texview background color are got affected. and here is my single_row_item.xml
single_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView2"
android:src="@drawable/icon" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/tv_person_name"
android:layout_marginLeft="10dp" />
</LinearLayout>
and this is my output. please tell me how to do this.
I have found the Answer. It was just small silly mistake. In My Activity's XML in which I have list view. That list view should have width = "fill_parent" or "match_parent".