I want to create a very simple custom cursor adapter to make it easier to change the color of line items on click. Using the following code
private static int save = -1;
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
if (save != -1 && save != position){
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
}
I got the code from this thread https://stackoverflow.com/a/464829/
I would use a simple cursor adapter and put the code in onClick, but since the default list in the ListFragment repeats the scans, as you scroll through several views, they are shown as highlighted. Speaking of IRC, it was proposed to create a custom cursor adapter. However, I cannot find best practice on how to do this and where the code snippet will be placed. May be very grateful for the help.
public class AreaCursorAdapter extends CursorAdapter {
private Context context;
public AreaCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView list_item = (TextView)view.findViewById(android.R.id.text1);
list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
bindView(v, context, cursor);
return v;
}
}
, . .
1. , "", .
2. Eclipse , .