我有一个列表视图,其中填充了一个简单的游标适配器,它反映了数据库中的条目。我想从我创建的上下文菜单中删除项目。我写了下面的代码,但它似乎没有工作。单击“编辑”或“删除”时,即使我在单击每个菜单项时写了一行以显示祝酒,也没有任何反应。以下是代码。
// MainActivity
package com.apps.katz.doer;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AlertDialog;
import android.view.ContextMenu;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ListView taskListView;
private DataModel data;
DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
taskListView = (ListView) findViewById(R.id.task_list_view);
Cursor cursor = db.queueAllTasks();
String[] from = new String[] {"taskName"};
int[] to = new int[] {R.id.text};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to);
taskListView.setAdapter(adapter);
registerForContextMenu(taskListView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
int currentPosition;
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle(R.string.task_options);
menu.add(0, v.getId(), 0, R.string.edit_task);
menu.add(0, v.getId(), 0, R.string.delete_task);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int position;
super.onContextItemSelected(item);
if (item.getTitle()=="Edit") {
} else if (item.getTitle()=="Delete") {
Toast.makeText(getBaseContext(),"Task Deleted ",Toast.LENGTH_LONG).show();
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
position = (int)info.id;
db.deleteTask(position);
} else {
return false;
}
return true;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_add_new_task) {
startActivity(new Intent(MainActivity.this, AddTask.class));
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
DataBase处理程序删除方法
public void deleteTask(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_ID + " =?",
new String[] {String.valueOf(id)});
db.close();
}
// DataModel类
package com.apps.katz.doer;
/**
* Created by katz on 14/4/16.
*/
public class DataModel {
int id;
private String taskTitle;
public DataModel(){
}
public DataModel(int id) {
this.id=id;
}
public DataModel(int id, String taskTitle){
this.id=id;
this.taskTitle=taskTitle;
}
public DataModel(String taskTitle) {
this.taskTitle = taskTitle;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTaskTitle() {
return taskTitle;
}
public void setTaskTitle(String taskTitle) {
this.taskTitle = taskTitle;
}
}
答案 0 :(得分:0)
试试这个:
@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
int id = item.getItemId();
if (id == R.id.edit) {
// code
return true;
} else if (id == R.id.delete) {
// code
return true;
}
return false;
}
答案 1 :(得分:0)
通过创建方法refreshListView()并在onCreate中调用它然后在我从上下文菜单中选择“删除”的代码中解决它。 这是onContextItemSelected方法的代码。
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
Toast.makeText(this,"You chose Edit",Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
Toast.makeText(this,"Task Deleted",Toast.LENGTH_LONG).show();
db.deleteTask((int)info.id);
refreshListView();
return true;
default:
return super.onContextItemSelected(item);
}
}
这是方法的代码,refreshListView()。
private void refreshListView() {
taskListView = (ListView) findViewById(R.id.task_list_view);
cursor = db.queueAllTasks();
String[] from = new String[] {"taskName"};
int[] to = new int[] {R.id.text};
adapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to);
taskListView.setAdapter(adapter);
registerForContextMenu(taskListView);
}