Context Menus is List View using Simple Cursor Adapter

You have already seen how Context Menus work with ArrayAdapter. In case of SimpleCursorAdapter there are a few methods that can be used in order to retrieve the data about the item that is being clicked. This process is a bit different from the one we have already seen. I provide below the Source Code for doing the same.

Again, the Source Code is changed only in the MainActivity.java file. The rest of the codes namely DatabaseHelper.java, activity_main.xml, row_item.xml remain the same.

MainActivity.java

[java]
package com.nero.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends ListActivity {
DatabaseHelper db;
SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(Main.this);
displayList();
}

public void displayList(){
db.InsertValues();
Cursor cursor=db.GetAllData();
String from [] = new String[]{db.colName,db.colAge};
int to [] = new int[] {R.id.textView1,R.id.textView2};
dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, from, to, 0);
db.close();

ListView lv = getListView();
lv.setAdapter(dataAdapter);
registerForContextMenu(lv);
}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
Cursor cursor = (Cursor) lv.getItemAtPosition(info.position);
final int title = cursor.getInt(cursor.getColumnIndex("_id"));
menu.setHeaderTitle(title);
menu.add(0, v.getId(), 0, "Edit");
menu.add(0, v.getId(), 0, "Delete");
}

public boolean onContextItemSelected(MenuItem item){
if(item.getTitle()=="Edit")Toast.makeText(getApplicationContext(), "Edit Clicked", Toast.LENGTH_LONG).show();
if(item.getTitle()=="Delete")Toast.makeText(getApplicationContext(), "Delete Clicked", Toast.LENGTH_LONG).show();
return true;
}
}
[/java]

Leave a Comment

Your email address will not be published. Required fields are marked *