长按列表视图后的操作(Android Studio)
Action after long click on listview (Android Studio)
我有一个 ListView,它从 sqlite 数据库中获取数据。
现在,我想设置当我长按列表视图中的某个项目时,我可以删除或更新该项目。
我成功执行了上下文菜单,但我不知道如何在单击更新或删除后执行操作。
DataListActivity.java - 列表视图的activity
public class DataListActivity extends ActionBarActivity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
CourseDbHelper courseDbHelper;
Cursor cursor;
ListDataAdaptar listDataAdaptar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
listView = (ListView) findViewById(R.id.list_view);
listDataAdaptar = new ListDataAdaptar(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdaptar);
registerForContextMenu(listView);
courseDbHelper = new CourseDbHelper(getApplicationContext());
sqLiteDatabase = courseDbHelper.getReadableDatabase();
cursor = courseDbHelper.getInformation(sqLiteDatabase);
registerForContextMenu(listView);
if (!cursor.moveToFirst()){
}
else {
do {
String year,semester,course,points,grade;
year = cursor.getString(0);
semester = cursor.getString(1);
course = cursor.getString(2);
points = cursor.getString(3);
grade = cursor.getString(4);
DataProvider dataProvider = new DataProvider(year,semester,course,points,grade);
listDataAdaptar.add(dataProvider);
}
while (cursor.moveToNext());
}
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_data_list, menu);
}
}
menu_data_list.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ido.grades.DataListActivity">
<item android:id="@+id/update_item" android:title="@string/update"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/delete_item" android:title="@string/delete"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
谢谢维卡斯,
我添加了该功能及其作品(而不是 onOptionsItemSelected 我写了 onContextItemSelected):
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.update_item:
Toast.makeText(this,"update",Toast.LENGTH_LONG).show();
return true;
case R.id.delete_item:
Toast.makeText(this,"delete",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我有一个 ListView,它从 sqlite 数据库中获取数据。 现在,我想设置当我长按列表视图中的某个项目时,我可以删除或更新该项目。
我成功执行了上下文菜单,但我不知道如何在单击更新或删除后执行操作。
DataListActivity.java - 列表视图的activity
public class DataListActivity extends ActionBarActivity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
CourseDbHelper courseDbHelper;
Cursor cursor;
ListDataAdaptar listDataAdaptar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_list_layout);
listView = (ListView) findViewById(R.id.list_view);
listDataAdaptar = new ListDataAdaptar(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdaptar);
registerForContextMenu(listView);
courseDbHelper = new CourseDbHelper(getApplicationContext());
sqLiteDatabase = courseDbHelper.getReadableDatabase();
cursor = courseDbHelper.getInformation(sqLiteDatabase);
registerForContextMenu(listView);
if (!cursor.moveToFirst()){
}
else {
do {
String year,semester,course,points,grade;
year = cursor.getString(0);
semester = cursor.getString(1);
course = cursor.getString(2);
points = cursor.getString(3);
grade = cursor.getString(4);
DataProvider dataProvider = new DataProvider(year,semester,course,points,grade);
listDataAdaptar.add(dataProvider);
}
while (cursor.moveToNext());
}
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_data_list, menu);
}
}
menu_data_list.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ido.grades.DataListActivity">
<item android:id="@+id/update_item" android:title="@string/update"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/delete_item" android:title="@string/delete"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
谢谢维卡斯, 我添加了该功能及其作品(而不是 onOptionsItemSelected 我写了 onContextItemSelected):
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.update_item:
Toast.makeText(this,"update",Toast.LENGTH_LONG).show();
return true;
case R.id.delete_item:
Toast.makeText(this,"delete",Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}