Hi,
I have been having some strange behavior when refreshing a listview
from a button in a list item. Say you have a list of 4 items, indexes
0-3 and you click on item 4, and print the index. 3 is printed
correctly, then i refresh the list. I click on item 4 again, but this
time index 0 is printed, click again and we are back to 3. Why is
this happening and how can I fix it?
I have included some source below for you to have a look at how I have
done this. Any help is much appreciated
Binesy
public class CrazyList extends Activity {
private static final String TAG = "CRAZY LIST";
public class Item {
public String name = null;
public int counter = -1;
public Item(String n) {
name = n;
counter = 0;
}
}
private ArrayList<Item> names = null;
private MyAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
names = new ArrayList<Item>();
names.add(new Item("Ross"));
names.add(new Item("Fred"));
names.add(new Item("Bob"));
names.add(new Item("Frank"));
ListView list = (ListView)findViewById(R.id.ListView01);
adapter = new MyAdapter();
list.setAdapter(adapter);
}
private void updateButtonText(int index) {
names.get(index).counter++;
adapter.notifyDataSetChanged();
}
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return names.size();
}
@Override
public Object getItem(int position) {
return names.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
Container c = null;
if(convertView == null) {
convertView =
View.inflate(getApplicationContext(), R.layout.item,
null);
c = new Container();
c.text = (TextView)
convertView.findViewById(R.id.TextView01);
c.button = (Button)
convertView.findViewById(R.id.Button01);
c.button.setTag(position);
c.button.setOnClickListener(new
OnClickListener() {
@Override
public void onClick(View v) {
int number =
(Integer)v.getTag();
Log.d(TAG, "Button clicked is "
+ number);
updateButtonText(number);
}
});
convertView.setTag(c);
}
else
c = (Container) convertView.getTag();
c.text.setText(names.get(position).name);
c.button.setText(names.get(position).counter + "");
return convertView;
}
public class Container {
public TextView text = null;
public Button button = null;
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
To unsubscribe, reply using "remove me" as the subject.