I have a list which is populated by my database. Each list row has a text view and two buttons. I want to cycle through all list rows and check the text of each text view that is inside the list and change one of the buttons backgrounds depending on the text read in.
Here is my code:
Button fav, trash;
ListView lv;
TextView tv;
Cursor data;
CursorAdapter dataSource;
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.hs);
tv = (TextView) this.findViewById(R.id.first);
trash = (Button) this.findViewById(R.id.trashButton);
fav = (Button) this.findViewById(R.id.favButton);
data.moveToNext();
dataSource = new SimpleCursorAdapter(this, R.layout.phrasebook, data,
fields, new int[] { R.id.first}, 0);
lv = (ListView) findViewById(R.id.list_view);
lv.setAdapter(dataSource);
You can see that the list is populated by the data source and the text view "first" is filled with strings. I want to loop through each list row read the text view and then change the button background depending on what the text is. My problem is how do you get the id's of the text views and buttons?
I read up and discovered get child and parent but not sure if this is along the right track. I thought if I could access the list rows by id then access its children then I could do it this way but I don't know if this is possible. Any advice is appreciated.
I have experimented with creating a custom adapter, I have it working and also found a way to access positions, I am hoping to exploit this to maybe access the text views.
public class AdapterEx extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
TextView tv;
public AdapterEx(Context context,int layout, Cursor c,String[] from,int[] to) {
super(context,layout,c,from,to);
this.layout=layout;
this.mContext = context;
this.inflater=LayoutInflater.from(context);
this.cr=c;
}
@Override
public View newView (Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
tv=(TextView)view.findViewById(R.id.first);
if(cursor.getPosition()%2==1) {
view.setBackgroundColor(Color.rgb(206, 43, 55));
}
else {
view.setBackgroundColor(Color.rgb(0, 146, 70));
}
}
}