所以我对这个 SQLite 很陌生,我终于设法创建基础并使用它等。现在我创建了 ContextMenu,你将能够从打开上下文菜单的 listView 中删除项目。这是我当前的代码。
import java.util.List;
import java.util.Random;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.view.MenuItem;
public class Ann extends ListActivity{
private CommentsDataSource datasource;
EditText edit;
ListView bump;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ann);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
registerForContextMenu(getListView());
}
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
edit = (EditText)findViewById(R.id.editTxt);
Editable txt=(Editable)edit.getText();
String vnos = txt.toString();
comment = datasource.createComment(vnos);
adapter.add(comment);
edit.setText("");
break;
}
adapter.notifyDataSetChanged();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
现在我知道我需要为public boolean onContextItemSelected(MenuItem item)
但是现在如何管理删除被选中的项目。