我有一個ListActivity類。它有一個按鈕,它根據其屬性之一對項目進行排序。但是恰巧這個項目正在按照他們的名字排序,但是當點擊它原來的位置的項目時就會被執行。 這裏是代碼片段:如何刷新活動中的ListView
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();
}
});
如何通過點擊任意按鈕或自動設置時間來刷新列表視圖? – Harish
通過點擊任何按鈕多數民衆贊成我想要做的...不能我整合排序和刷新成一個按鈕? – Saurabh
然後調用您從中獲取數據的按鈕的onclick函數以及該數據列出適配器。 – Harish