我有一个ListActivity类。它有一个按钮,根据其中一个属性对项目进行排序。但事实上,该项目正按其名称进行排序,但点击其原始位置的项目后即被执行。 以下是代码段:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mButton = (ToggleButton) findViewById(R.id.mButton);
ArrayList<myObject> ListData = new ArrayList<myObject>();
back = (ImageButton) findViewById(R.id.backButton);
label = (TextView) findViewById(R.id.likethis);
label.setText("List");
inputSearch = (EditText) findViewById(R.id.inputSearch);
//Manager -- class that reads from the sdcard
Manager plm = new Manager();
// get all files from sdcard
//getList() function of Manager class
//List declared outside oncreate()
this.List = plm.getList();
final ArrayList<myObject> backUp = new ArrayList<myObject>(songsList);
if(sortCalled) {
mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_checked));
mButton.setChecked(true);
Collections.sort(List);
notifyDataChanged(List);
}
else {
mButton.setBackground(getResources().getDrawable(R.drawable.m_unchecked));
mButton.setChecked(false);
notifyDataChanged(backUp);
}
back.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
inputSearch.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
//ArrayList <Song> temp;
((SimpleAdapter) PlayListActivity.this.adapter).getFilter().filter(s, new Filter.FilterListener() {
public void onFilterComplete(int count) {
// TODO Auto-generated method stub
adapter.notifyDataSetChanged();
}
});
adapter.notifyDataSetChanged();
}
});
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mButton.isChecked()){
mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_checked));
Collections.sort(List);
sortCalled = true;
notifyDataChanged(List);
}
else{
sortCalled = false;
mButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.m_unchecked));
notifyDataChanged(backUp);
}
}
});
}
public void settingListAdapter(SimpleAdapter adapter){
setListAdapter(adapter);
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int Index = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
MainActivity.class);
// Sending Index to MainActivity
in.putExtra("Index", Index);
setResult(100, in);
// Closing ListView
finish();
}
});
}
public void notifyDataChanged(ArrayList<Song> songsList) {
// TODO Auto-generated method stub
setListAdapter(null);
sortCalled = true;
ArrayList<myObject> songsListData = new ArrayList<myObject>();
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
myObject ob = sList.get(i);
// adding HashList to ArrayList
ListData.add(ob);
}
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String,String>>();
// int j = 0;
for(myObject i : ListData){
HashMap<String, String> feed = new HashMap<String, String>();
feed.put("Title", i.getTitle());
array.add(feed);
}
BaseAdapter adapter1;
// int i = 0;
// Adding menuItems to ListView
adapter1 = new SimpleAdapter(this, array,
R.layout.list_item, new String[]{"Title" }, new int[] {
R.id.Title });
setListAdapter(adapter1);
adapter1.notifyDataSetChanged();
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting listitem index
int Index = position;
// Starting new intent
Intent in = new Intent(getApplicationContext(),
MainActivity.class);
// Sending songIndex to MainActivity
in.putExtra("Index", Index);
setResult(100, in);
// Closing ListView
finish();
}
});
答案 0 :(得分:2)
我认为你应该试试这个。当你对listview的数据进行排序时,我的工作非常完美
adapter.notifyDataSetChanged();
答案 1 :(得分:1)
每次更改适配器时,都应该在UIThread中调用notifyDataSetChanged()
请检查this video以更好地了解listView
修改强>
对于ArrayAdapter,只有在适配器上使用add, insert, remove, and clear
函数时,notifyDataSetChanged才有效。
构造ArrayAdapter时,它保存传入的List的引用。如果要传入属于Activity成员的List,并稍后更改该Activity成员,则ArrayAdapter仍然保持引用原始列表。适配器不知道您更改了活动中的列表。
您的选择是:
使用ArrayAdapter的功能修改基础列表(add, insert, remove, clear, etc.)
使用新的List数据重新创建ArrayAdapter。 (使用大量资源和垃圾收集。)
创建自己的派生自BaseAdapter和ListAdapter的类,允许更改基础List数据结构。
每次更新列表时使用notifyDataSetChanged。要在UI-Thread上调用它,请使用Activity的runOnUiThread方法。然后notifyDataSetChanged将起作用。
答案 2 :(得分:0)
您可以尝试使用Adapter.notifyDataSetChanged()函数。它可能会奏效。
如果您使用的是SimpleCursorAdapter,则在连接到适配器的光标上调用requery()将自动刷新适配器和附加视图。
如果它的BaseAdapter内容发生了变化,你可以调用BaseAdapter.notifyDataSetChanged()来告诉ListView自我更新。
换句话说,你只需进行以下微小改动:
public void setValue(Object newValue) {
this.value = newValue;
notifyDataSetChanged();
}
实际上,由于您正在更改现有项目的状态(而不是添加新项目等),因此notifyDataSetInvalidated()将是更好的选择。