What i want is when the user clic on contact a context Menu appear with item "delete" so i did this, but the problem is I want that when he clic on delete a row from Sqlit ( that corresponds to the contact I just clicked)
Activity.class
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int i = info.position;
switch (item.getItemId()) {
case R.id.delete:
dbHandler.deleteEtudiant(i);
Toast.makeText(this,"deleted "+i, Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
MyDBHandler.class
public void deleteEtudiant(int i)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete("etudiant", "Id+ =" + i, null);
}
The problem with your code is that your are getting the id of "AdapterContextMenuInfo" object and sending it to the database "delete()" method. But actually you need to send the id of the of the item of database, the value that is stored in "ID" column of that particular row in SQlite Database.
Please have a look on the tutorial given below, specially check the "getContact()" and "deleteContact()" method of the tutorial.
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
Related
As you can see in the code below, when I LongClick on an item in ListView, i get a popup menu with option to manipulate the item (delete, update, etc).
the problem is that I use my function on item.toString instead the item itself.
How can i get the item itself, and put it as an argument in my functions?
onCreateContextMenu:
public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.db_list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
onContextItemSelected:
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
Object obj = lv.getItemAtPosition(info.position);
String nameToString = obj.toString();
if (item.getTitle().equals("Delete")) {
deletePlayerFromLongClick(nameToString);
} else if (item.getTitle().equals("Update")) {
updatePlayerFromLongClick(nameToString);
} else if (item.getTitle().equals("Change Host/Guest")) {
changeMembership(nameToString);
}
return true;
}
do like:
yourAdapter.getItem(info.position);
or
((YourAdapter)lv.getAdapter()).getItem(position);
or even simpler,
listOfItem.get(info.position);
I am building a custom ListView (card style) and would like to have an edit and delete button on the card. In order to invoke the methods on the correct database rows, I need to know which _id (View) the user selected.
Currently, I am using a context menu which is pretty easy
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
position = (int) info.id;
MenuInflater m = getMenuInflater();
m.inflate(R.menu.history_popup_menu, menu);
}
#Override
public boolean onContextItemSelected(final MenuItem item) {
switch(item.getItemId()){
case R.id.delete:
...
in this case, I get the id (position) in the onCreateContextMenu
I am wanting to get rid of the context menu now and just have edit and delete on the card itself.
How do I go about retrieving the _id from the database and assign it to a value to be used in my delete/edit method?
This is my method for creating the list
private void addItemsToList(){
Cursor cursor = db.getAllLogs();
Log.d("history.java", "finished Cursor cursor = db.getAllLogs();");
String[] from = {"_id", "date", "gallons", "pricePerGallon", "odometer", "filledOrNot", "comments", "totalSpent"};
int[] to = {deleteVal, R.id.cardDate, R.id.cardGallons, R.id.cardPrice, R.id.cardOdometer, R.id.cardFilledOrNot, R.id.cardComments, R.id.usd};
adapter = new SimpleCursorAdapter(this, R.layout.history_list_item, cursor, from, to, 0);
String filledOrNotVal = String.valueOf(R.id.cardFilledOrNot);
if (filledOrNotVal == "False"){
Log.d("history", "False");
} else {
Log.d("history", "True");
}
listContent.setAdapter(adapter);
}
Already had the ability to delete a listview item using an onItemLongClick method but I'd rather use a floating context menu to do this.
Below is the code I currently have for the floating context menu. I followed the documentation which helped me set it up and then tried to search for a similar example to what I'm doing but couldn't find anything appropriate.
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.payments_context, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
return true;
case R.id.delete:
return true;
default:
return super.onContextItemSelected(item);
}
}
This is the code I had to delete the items in my listview before I decided to switch to a floating context menu
public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
{
String temp = paymentTitle.get(position).toString();
paymentTitle.remove(position);
paymentDate.remove(position);
reminderDate.remove(position);
reminderTime.remove(position);
paymentVal.remove(position);
mDatabase = new MOSDatabase(this);
SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
readableDB.delete("PaymentTable", "PTITLE=?",
new String[]{temp});
aa.notifyDataSetChanged();
return false;
}
If someone could advise me on how to get this floating context menu working I'd be really grateful. I don't have the edit method done just yet, it's what I have to do after I get this completed.
If I understand correctly, you can get the index of the item in the ListView at the click position by using the following code:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;
Using position, you can reuse the code of onItemLongClick pretty much as is:
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
switch (item.getItemId()) {
case R.id.edit:
return true;
case R.id.delete: {
String temp = paymentTitle.get(position).toString();
paymentTitle.remove(position);
paymentDate.remove(position);
reminderDate.remove(position);
reminderTime.remove(position);
paymentVal.remove(position);
mDatabase = new MOSDatabase(this);
SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
readableDB.delete("PaymentTable", "PTITLE=?",
new String[]{temp});
aa.notifyDataSetChanged();
}
return true;
default:
return super.onContextItemSelected(item);
}
}
You might want to look at the answer to this question.
I'm doing a proyect where i have a view with multiple listview created at runtime, every listview load specifics rows of a database. and i want to implement a contextmenu. The problem is how can i get the row of the listview to retieve the id of the database? How can i get my list adapter inside the contextmenu ? or some other solution. Thanks!
here is a part of the code...
private void makeView(){
yearsArray = db.getUniqueYears(TABLE_NAME);
for (int i = 0; i < yearsArray.size() ; i++){
list = db.getDocByYear(TABLE_NAME, yearsArray.get(i));
custom_adapter = new Document_adapter(this, list);
ListView lv = new ListView(this);
lv.setAdapter(custom_adapter);
lv.setBackgroundResource(R.drawable.title_container_bg);
registerForContextMenu(lv);
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.doc_options, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
/*
*/
return super.onContextItemSelected(item);
}
If you set your database up properly you can just do this info.id; in your onContextItemSelected and that gives the database id
My app contains 1 list view, data source is 1 sqlite table, when i hold long click on any row in listview it will show me 1 menu option to change the color of that row, for this i have used onContextItemSelected function, on selecting menu option it will call 1 function change_color. What should i write in change_color function so that i can change row bg color.
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color();
return true;
}
return super.onContextItemSelected(item);
}
Call your method as :
change_color(pass_your_list_view, pass_selected_position_of_list_view);
And define change_color() as:
private void change_color(ListView listView, int position) {
listView.getChildAt(position).setBackgroundColor(Color.BLACK);
}
Hope this will help.
Edited
Define a variable a position
public static int position;
And replace your code as
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, PROCESSED_ID, 0, R.string.menu_processed);
// Get the info on which item was selected
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// Retrieve the position at where you long pressed
position = info.position;
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PROCESSED_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
change_color(getListView(), position);
return true;
}
return super.onContextItemSelected(item);
}
refer to this tutorial Colored Row in List View