Iterate Through Listview In Custom Adapter
Solution 1:
There is one solution:
privatestaticViewlastClicked=null;
... ...
viewHolder.postNameView.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
ViewparentRow= (View) v.getParent();
ViewHolderviewHolder= (ViewHolder)parentRow.getTag();
if(viewHolder.postImageView.getVisibility() != View.VISIBLE)
{
//clear last clicked imageif(lastClicked != null)
{
ViewHolderlastHolder= (ViewHolder)lastClicked.getTag();
lastHolder.postImageView.setVisibility(View.INVISIBLE);
}
selectedTimeZone = viewHolder.postNameView.getText().toString();
viewHolder.postImageView.setVisibility(View.VISIBLE);
lastClicked = parentRow;
}
else
{
viewHolder.postImageView.setVisibility(View.INVISIBLE);
lastClicked = null;
}
});
Hope this help!
Solution 2:
In the onClick you're iterating through the views and make sure that they all have the image hidden, and then after that setting the current one visible. You don't need to do that yourself, just tell the system to do it for you.
Note also that your current code will loop around all entries in the adapter and try and get a view for them... but not all of them will have one, the ones scrolled off the screen won't have a view.
The simplest way to do it is to remove everything from your onClick handler except the line where you set selectedTimeZone
, then call notifyDataSetChanged()
. This will make Android redraw all of the currently-onscreen views.
Then below your setText()
add:
if (datas.get(position).equals(selectedTimeZone)) {
viewHolder.postImageView.setVisibility(View.VISIBLE);
} else {
viewHolder.postImageView.setVisibility(View.INVISIBLE);
}
so when the system re-draws the views each view will be considered for having the image set to visible but only the current one will get it set.
Solution 3:
I wouldn't store any information on the views. I believe that the listview recycles its views so thing could get messy. Try to separate the data from the views.
I would try a different approach. Your datas
should be
ArrayList<MyData> datas;
publicclassMyData{
String place;
boolean: checked = false;
}
To populate the datas
ArrayList you can do this
for (String s : objects){
MyDatad=newMyData();
d.place = s;
}
Then in your adapters getView
you should display/hide the imageView based on the value of checked
like this:
privateclassTimeZoneItemAdapterextendsArrayAdapter<MyData> {
private Activity myContext;
private ArrayList<MyData> datas;
publicTimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<MyDatas> datas) {
super(context, textViewResourceId, datas);
this.datas = datas;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflaterinflater= myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.listview_item, null);
}
TextViewpostNameView= (TextView) convertView.findViewById(R.id.listview_item);
TextViewpostImageView= (ImageView) convertView.findViewById(R.id.image_mark);
postNameView.setText(this.datas.get(position).place);
if(this.datas.get(position).checked){
postImageView.setVisibility(View.VISIBLE);
}else{
postImageView.setVisibility(View.INVISIBLE);
}
return convertView;
}
}
When clicking on an item, you should iterate the datas
, uncheck everything and check the appropriate item. Then to update the listView you call
((TimeZoneItemAdapter)list.getAdapter()).notifyDataSetChanged();
and all the views will be updated automatically.
Post a Comment for "Iterate Through Listview In Custom Adapter"