我想通过单击列表视图项中的按钮来删除列表视图中的项目。这有效但我不知道如何在点击按钮后刷新列表视图。有谁可以帮助我?
这是我的一些代码
public class ActionsListView extends AppCompatActivity {
private Button deleteBtn, editBtn, addBtn;
private ListView listView;
DBAdapter myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actions_list_view);
deleteBtn = (Button) findViewById(R.id.deleteBtn);
editBtn = (Button) findViewById(R.id.editBtn);
addBtn = (Button) findViewById(R.id.alv_AddBtn);
openDB();
populateListView();
}
private void openDB(){
myDb = new DBAdapter(this);
myDb.open();
}
private void populateListView(){
Cursor cursor = myDb.getAllRows();
String [] fromFieldNames = new String[] {DBAdapter.KEY_ACTION_NAME, DBAdapter.KEY_ACTION_DURATION};
int [] toViewIDs = new int[]{R.id.itemActionDisplay, R.id.itemDurationDisplay};
MySimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new MySimpleCursorAdapter(getBaseContext(), R.layout.item_action, cursor, fromFieldNames, toViewIDs, 0);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(myCursorAdapter);
}
我的自定义simplecursoradapter
public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View newView(Context _context, Cursor _cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(_context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_action, parent, false);
return view;
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
String actionName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_ACTION_NAME));
String actionDuration = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_ACTION_DURATION));
TextView actionNameTxtField = (TextView) view.findViewById(R.id.itemActionDisplay);
TextView actionDurationTxtField = (TextView) view.findViewById(R.id.itemDurationDisplay);
actionNameTxtField.setText(actionName);
actionDurationTxtField.setText(actionDuration);
Button yourButton = (Button) view.findViewById(R.id.deleteBtn);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (view != null) {
DBAdapter myDb = new DBAdapter(context);
myDb.open();
myDb.deleteAll();
notifyDataSetChanged();
}
}
});
}