Reputation: 5083
I currently have the following adapter to set up my listview:
public class MyListAdapter extends ArrayAdapter<String> {
private Context mContext;
public MyListAdapter(Context context, int resource, int textViewResourceId,String[] objects) {
super(context, resource, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Drawable cross = getResources().getDrawable(R.drawable.cross);
Drawable arrow = getResources().getDrawable(R.drawable.arrow);
View row = convertView;
if(row == null){
LayoutInflater inflater= getLayoutInflater();
row = inflater.inflate(R.layout.list_view_format2, parent, false);
}
TextView text =(TextView)row.findViewById(R.id.list_content2);
text.setText(list[position]);
ImageView image = (ImageView)row.findViewById(R.id.rightArrow);
TextView check = (TextView)row.findViewById(R.id.checker);
if (item != null && text.getText().toString().equals(item)){
text.setBackgroundDrawable(cross);
image.setImageDrawable(arrow);
check.setText("cross");
}
return row;
}
}
R.layout.list_view_format2: a custom ListView format. It contains an ImageView and a TextView R.id.list_content2: *Id of the Text View within R.id.list_view_format2*
I am trying to change certain parts of a child if the search is successful. However, instead of only one child being modified, when I scroll up or down a ListView the layout of the other children gets modified as well. I don't exactly get what is happening. Can someone please explain?
Upvotes: 0
Views: 384
Reputation: 5083
I figured it out. I just had to set the others to null! I did that in the else block. Now it works perfectly.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Drawable cross = getResources().getDrawable(R.drawable.cross);
Drawable arrow = getResources().getDrawable(R.drawable.arrow);
View row = convertView;
if(row == null){
LayoutInflater inflater= getLayoutInflater();
row = inflater.inflate(R.layout.list_view_format2, parent, false);
}
TextView text =(TextView)row.findViewById(R.id.list_content2);
text.setText(list[position]);
ImageView image = (ImageView)row.findViewById(R.id.rightArrow);
TextView check = (TextView)row.findViewById(R.id.checker);
if (item != null && text.getText().toString().equals(item)){
text.setBackgroundDrawable(cross);
image.setImageDrawable(arrow);
check.setText("cross");
}
else {
text.setBackgroundDrawable(null);
image.setImageDrawable(null);
check.setText("none");
}
return row;
}
Upvotes: 0
Reputation: 9189
ArrayAdatper is a very limited class, try moving over to SimpleAdapter which has several more features which will make what you want to do easier. Instead of overriding getView()
look at the features of SimpleAdapter.ViewBinder which will allow you to do extensive modifications to the list items based on the underlying data. Here you will be able to make only certain list items to appear different than the others.
Upvotes: 1