I have this class but I cannot get the ListView Items without the click listener ... I simply want to update the color of the text onResume ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter( new ArrayAdapter<String>(this, R.layout.overlay_options, FishSpecie.getFishSpecies()));
mList = getListView();
mList.setTextFilterEnabled(true);
mList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Boolean status = doClick(((TextView) arg1).getText());
if(status == true)
{
((TextView) arg1).setTextColor(Color.GREEN);
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText() + " Enabled", Toast.LENGTH_SHORT).show();
开发者_JAVA百科 }
else
{
((TextView) arg1).setTextColor(Color.DKGRAY);
Toast.makeText(getApplicationContext(), ((TextView) arg1).getText() + " Disabled", Toast.LENGTH_SHORT).show();
}
}
});
// Create all the overlay check boxes and labels
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
updateList();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
updateList();
}
protected void updateList() {
mList = getListView();
for (int i = 0; i < mList.getChildCount(); i++)
{
View v = mList.getChildAt(i);
TextView tx = (TextView) v.findViewById(R.layout.overlay_options);
if(FishSpecie.isFishSpecieEnabled(tx.getId()) == true) {
// Change color of list item
tx.setTextColor(Color.GREEN);
}
else
{
tx.setTextColor(Color.DKGRAY);
}
}
}
View v = mList.getChildAt(i); this never finds the child
To clarify mList.getChildCount() always returns 0
I think i've nearly fixed it but I can't understand why the color change doesn't take affect ... Below is the new code for the update function
protected void updateList() {
for (int i = 0; i < mArray.getCount();i++) {
//TextView v = (TextView)(findViewById((int) mArray.getItemId(i)));
TextView v = (TextView) mArray.getView(i, null , null);
String text = (String) v.getText();
if(FishSpecie.isFishSpecieEnabled(i) == true) {
// Change color of list item
v.setTextColor(Color.GREEN);
}
else
{
v.setTextColor(Color.DKGRAY);
}
}
}
When searching for text view you probably want R.id.overlay_options
not R.layout.overlay_options
.
Update
Not sure why you get 0 child count but your approach is, probably, not going to work as those views are reused for diffeent items. Better approach may be to customize getView
method on adapter to show proper color.
精彩评论