I am using a listview with an arrayadapter.. and i wish to know how to change an object property directly with an adapter ? without having to remove it and readding it. It is possible ?
Currently i use ..
开发者_运维百科 btnModifierNom.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
int itemPosition = lstPersonnes.getCheckedItemPosition();
selectedItem = lstPersonnes.getItemAtPosition(itemPosition).toString();
String personne = adapter.getItem(itemPosition);
adapter.remove(personne);
personne = txtNouveauNom.getText().toString();
adapter.insert(personne, itemPosition);
adapter.notifyDataSetChanged(); }
});
But its not a very elegant way.. if possible i would love to avoid removing it and reinserting it.
Any ideas ?
Thanks!
Why can't you just change the value like this:
int itemPosition = lstPersonnes.getCheckedItemPosition();
String personne = adapter.getItem(itemPosition);
personne = txtNouveauNom.getText().toString();
adapter.notifyDataSetChanged();
And if adapter.notifyDataSetChanged()
didn't work with this, then try listView1.setAdapter(adapter)
to refresh the listview.
UPDATE:
Try this:
btnModifierNom.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int itemPosition = lstPersonnes.getCheckedItemPosition();
// Gets the textview of the item at the position in the listview
((TextView)lstPersonnes.getChildAt(itemPosition)).setText(txtNouveauNom.getText().toString());
// Put this
adapter.notifyDataSetChanged();
// Or this to refresh listview
lstPersonnes.setAdapter(adapter);
}
});
It looks like ArrayAdapter is intended for static data. Try extending from BaseAdapter, then notifyDataSetChanged() will work.
精彩评论