I have searched this issue in this web, but nothing has helped me yet. Many people ask the same thing but none answers my problem. I have a listView with a button delete for each row of my list. I have every thing done, except get my list view updated. This list view is populated with information stored in a database. For each row, I have a custom ListAdapter and, I handle the onClick event of my delete Button inside this class.
So this is my Main Activity where I set the custom Adapter:
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);
}
}
And this is my custom adapter class:
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();
}
}
As I know the notifyDataSetChanged() works if I get my data for my list from an array. But this is not my case.
Any help will be apreciated!!.
SOLUTION: Inside the method deleteEmployee I put the following code, (as you can see in the code above):
this.changeCursor(DB.listEmployees(context));
this.notifyDataSetChanged();
Where
DB.listEmployees(context)
is the method that I use to retrieve the values from DB to populate the listView.
So, as you can see, I needed to change my old cursor for a new one with the data updated.
您重新查询mCursor,然后调用notifyDataSetChanged()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.