0
我已經在這個網絡中搜索這個問題,但沒有任何幫助我呢。許多人問同樣的事情,但沒有人回答我的問題。我有一個列表視圖,每個列都有一個按鈕刪除。除了讓我的列表視圖更新之外,我已經完成了所有工作。該列表視圖用存儲在數據庫中的信息填充。對於每一行,我都有一個自定義的ListAdapter,並且我在這個類中處理了我的delete Button的onClick事件。刷新列表視圖當按鈕刪除被點擊內部自定義適配器
所以這是我的主要活動,我設置的自定義適配器:
public class ClientsActivity extends ListFragment {
private Cursor mCursor;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_clients, container, false);
listEmployees(view);
...
}
private void listEmployees(View view){
//Get the list from DB and set other variables..
...
ListView lvEmployees = (ListView) view.findViewById (android.R.id.list);
ListClientCursorAdapter notes = new ListClientCursorAdapter(context, R.layout.activity_row_client, mCursor, from, to, 0);
lvEmployees.setAdapter(notes);
}
}
這是我的自定義適配器類:
public class ListClientsCursorAdapter extends SimpleCursorAdapter{
//Set the constructor and stuff
...
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
//Things that populate my rows
...
ImageButton buttonDelete = (ImageButton) v.findViewById(R.id.imageButtonDelete);
//I set in a bundle, the id of the employee that I want to delete.
final Bundle mArguments = new Bundle();
mArguments.putString("id", id);
buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
alert.setMessage("are you sure?");
alert.setTitle("Deleting..");
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}});
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//I get from the bundle the Id of my employee
String id = mArguments.getString("id");
deleteEmployee(id);
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}});
return v;
}
private void deleteEmployee(String id) {
//Here I delete the item in the DB
//and everything works fine ..
...
//SOLUTION:
//HERE I HAVE TO REFRESH THE LIST. THIS IS HOW
this.changeCursor(DB.listCompanies(context));
this.notifyDataSetChanged();
}
}
我所知notifyDataSetChanged()的作品,如果我得到我的數據來自數組中的列表。但這不是我的情況。
任何幫助將是apreciated !!。
SOLUTION: 內deleteEmployee我把下面的代碼,(你可以在代碼中看到以上)的方法:
this.changeCursor(DB.listEmployees(context));
this.notifyDataSetChanged();
凡
DB.listEmployees(context)
是方法,我用於從數據庫檢索值來填充listView。
因此,正如您所看到的,我需要將舊光標更改爲新數據並更新數據。
您忘記告訴我調用changeCursor()方法。但你是對的,我需要重新查詢並調用notifyDataSetChanged()。所以你的答案被接受。 – kiduxa 2013-05-29 23:32:16
依賴我通常將mCursor傳遞給我的適配器,所以我不需要調用changeCursor(),只需要再查詢,然後notifyDataSetChanged()。 – 2013-05-29 23:35:58