androidsqlitelistviewandroid-activitylong-click

Action after long click on listview (Android Studio)


I have a ListView that take the data from sqlite database. Now, I want to set when i long click on some item in the listview, i will can delete or update the item.

I success to do the ContextMenu, but i dont know how to make the action after i click update or delete.

DataListActivity.java - The activity of the 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: https://i.sstatic.net/eCUzQ.png


Solution

  • Thanks Vikas, I added that function and its works (Instead onOptionsItemSelected i wrote 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);
            }
        }