I'm having difficulties changing textview's textcolor in my listview.
I have a row that consists of more than one textview but only one textview is visible.
I want the text of that textview to change the color of textview depending on the other textview's value in that row. I'm pretty sure you'll understand what I mean when you see the code.
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, strojeviList,
R.layout.list_item, new String[] { TAG_SIFSTROJA,
TAG_NAZIVSTROJA, TAG_AKTIVAN, TAG_AUTH,
TAG_IP, TAG_POCETAK},
new int[] { R.id.sifstroja, R.id.nazivstroja, R.id.aktivan,
R.id.auth, R.id.ip,R.id.pocetak});
// updating listview
setListAdapter(adapter);
View v;
TextView blab= (TextView)findViewById(R.id.tekst);
blab.setTextColor(getResources().getColor(R.color.zelena));
ListView lv=getListView();
TextView akt;
TextView naziv;
int bla=lv.getCount();
Log.d(TAG_AUTH,String.valueOf(bla));
for (int i = 0; i < lv.getCount(); i++) {
v=(View) lv.getAdapter().getView(i,null,null);
akt = (TextView) v.findViewById(R.id.aktivan);
naziv=(TextView)v.findViewById(R.id.nazivstroja);
String aktiv=akt.getText().toString();
String aktivtekst=naziv.getText().toString();
Log.d(aktiv, aktiv);
if(aktiv.equals("1"))
{
Log.d("uso","uso sam");
Log.d("uso",aktivtekst);
naziv.setTextColor(getResources().getColor(R.color.zelena));
}
else {
Log.d("nisam aktivan","nisam aktivan");
Log.d("nisam aktivan",aktivtekst);
naziv.setTextColor(getResources().getColor(R.color.crvena));
}
}
}
I can get textview values and if part works just fine but color is not changed, so what can I do to change the color?
You need to move this code inside the adapter's getView
method, for example:
....
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, strojeviList,
R.layout.list_item, new String[] { TAG_SIFSTROJA,
TAG_NAZIVSTROJA, TAG_AKTIVAN, TAG_AUTH,
TAG_IP, TAG_POCETAK},
new int[] { R.id.sifstroja, R.id.nazivstroja, R.id.aktivan,
R.id.auth, R.id.ip,R.id.pocetak}){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView akt = (TextView) v.findViewById(R.id.aktivan);
TextView naziv=(TextView)v.findViewById(R.id.nazivstroja);
String aktiv=akt.getText().toString();
String aktivtekst=naziv.getText().toString();
Log.d(aktiv, aktiv);
if(aktiv.equals("1"))
{
Log.d("uso","uso sam");
Log.d("uso",aktivtekst);
naziv.setTextColor(getResources().getColor(R.color.zelena));
}
else {
Log.d("nisam aktivan","nisam aktivan");
Log.d("nisam aktivan",aktivtekst);
naziv.setTextColor(getResources().getColor(R.color.crvena));
}
return v;
}
};
setListAdapter(adapter);
TextView blab= (TextView)findViewById(R.id.tekst);
blab.setTextColor(getResources().getColor(R.color.zelena));