i have a complex row layout (“2 imageViews , 3 TextViews “) so i have defined my own adapter. i want to change the color for the textViews upon a click ! and i faced the problem of non final variable refered from inside an inner class is there any twist around this problem ?
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if(convertView==null)
{ minflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = minflater.inflate(R.layout.list_item2, null);
holder.title = (TextView) convertView.findViewById(R.id.rowtext1);
holder.note = (TextView) convertView.findViewById(R.id.rowtext2);
holder.date = (TextView) convertView.findViewById(R.id.rowtext3);
holder.course= (TextView) convertView.findViewById(R.id.rowtext4);
holder.icon = (ImageView) convertView.findViewById(R.id.rowimage1);
holder.read = (ImageView) convertView.findViewById(R.id.rowimage2);
convertView.setOnClickListener(new OnClickListener() {
private int pos = position;
@Override
public void onClick(View v) {
numberOfClicks++;
// Heres the problem
holder.title.setTextColor();
if(pos==0 && numberOfClicks % 2 ==1 )
v.setBackgroundResource(R.drawable.stoprow);
else if (pos==0 && numberOfClicks % 2 !=1 )
v.setBackgroundResource(R.drawable.toprow);
else if (pos==getCount()-1 && numberOfClicks % 2 !=1 )
v.setBackgroundResource(R.drawable.bottomrow);
else
v.setBackgroundResource(R.drawable.smiddlerow);
}
});
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
i too got same problem and finally i got solution as follows
than in the onclick:
Refer this link for further reference Imageview in Listview cannot change at runtime in android