长按listview后的动作(Android Studio)

时间:2015-08-08 16:08:22

标签: android sqlite listview android-activity long-click

我有一个ListView,它从sqlite数据库中获取数据。 现在,我想设置当我长时间点击列表视图中的某个项目时,我可以删除或更新该项目。

我成功完成了ContextMenu,但是在点击更新或删除后我不知道如何进行操作。

DataListActivity.java - listview的活动

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>

pic:http://i.stack.imgur.com/eCUzQ.png

1 个答案:

答案 0 :(得分:1)

谢谢Vikas, 我添加了该函数及其作用(而不是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);
        }
    }