#Override
public boolean onContextItemSelected(MenuItem item) {
// AdapterContextMenuInfo info=
// (AdapterContextMenuInfo)item.getMenuInfo();
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.menu_delete:
this.results.remove(info.position);
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results);
setListAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
return true;
case R.id.menu_editar:
this.results.add(info.position, null);
ArrayAdapter<String> mAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results);
setListAdapter(mAdapter1);
mAdapter1.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
I'm trying to edit a trip and stored in a listView, I manage to delete but not edit.
I would appreciate any help or example.
Thanks for your help
Related
I have a ListView displaying custom views (3 TextViews per item); I have just implemented a context menu like such:
// in onCreateView
ListView list = (ListView) view.findViewById(R.id.list);
registerForContextMenu(list);
and
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.list) {
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.todo_context_menu, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
// your first action code
return true;
case R.id.delete:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}
I wish to know which item has been long clicked (its position in the ListView would be ideal) in order to edit or delete the right one.
How may that be achieved?
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
switch (item.getItemId()) {
case R.id.edit:
// your first action code
return true;
case R.id.delete:
// your second action code
return true;
default:
return super.onContextItemSelected(item);
}
}
I'm creating a simple dialog with a ListView on it. I want to be able to access a context menu on it. Here's the basic code I've:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_lista);
dialog.setTitle("Contact");
ListView lv = (ListView) dialog.findViewById(R.id.listView); Cursor cursor = db.readData();
String[] from = new String[]{DatabaseHandler.KEY_contacts, DatabaseHandler.KEY_number};
int[] to = new int[]{R.id.contacts, R.id.number};
#SuppressWarnings("deprecation")
final
SimpleCursorAdapter adapter = new SimpleCursorAdapter(Home.this, R.layout.show, cursor, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
registerForContextMenu(lv);
here the method onCreateContextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_context, menu);
}
And finally I override the onContextItemSelected:
#Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
Toast.makeText(this,"call" ,Toast.LENGTH_LONG).show();
return true;
case R.id.sms:
Toast.makeText(this,"sms" ,Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
Toast.makeText(this,"delete" ,Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
I've tried also to override setOnMenuItemClickListener() inside onContextItemSelected()
but my problem still is not solved :( Any help?
You can get Item using this method
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = lv.getItemAtPosition(position);
}
});
I have an list view that displays an id name description etc... i created a context menu to use on that list and i want to get the name of a specific row through the context menu. How can i do this
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_ID,
TAG_NAME, TAG_DATA, TAG_DATA2, TAG_QTD},
new int[] { R.id.id, R.id.descricao, R.id.data, R.id.data2, R.id.qtd});
// updating listview
setListAdapter(adapter);
}
});
}
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
getMenuInflater().inflate(R.menu.context, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.item1:
long id = getListAdapter().getItemId(info.position);
Toast.makeText(getApplicationContext(),String.valueOf(id),
Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
return true;
default:
return super.onContextItemSelected(item);
}
}
}
You should use a custom list adapter.
Then you can use an onItemClickListener to get the clicked item and so the name etc...
Here is an tutorial for a custom adapter:
http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/
You want to register for context menu like: registerForContextMenu(yourListView).
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 newbie in Android and i learning context menu but after surfing about context menu i have little bit confusion in Adapter and Inflater. I saw 1 program with using adapter and 1 using Inflater. So, please help me how/when to use Adapter and Inflater.
Here is an example using inflater...
public class MainActivity extends ListActivity {
private String selectedName = "";
private String[] nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nameList = getResources().getStringArray(R.array.name_list);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, nameList));
registerForContextMenu(getListView());
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo adapInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
selectedName = nameList[(int) adapInfo.id];
switch (item.getItemId()) {
case R.id.view:
Toast.makeText(MainActivity.this,
"You have pressed View Context Menu for " + selectedName,
Toast.LENGTH_LONG).show();
return true;
case R.id.save:
Toast.makeText(MainActivity.this,
"You have pressed Save Context Menu for " + selectedName,
Toast.LENGTH_LONG).show();
return true;
case R.id.edit:
Toast.makeText(MainActivity.this,
"You have pressed Edit Context Menu for " + selectedName,
Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
Toast.makeText(MainActivity.this,
"You have pressed Delete Context Menu for " + selectedName,
Toast.LENGTH_LONG).show();
return true;
}
return false;
}
}
Another example using adapter:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Countries = getResources().getStringArray(R.array.Game);
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listitem, Countries);
list.setAdapter(adapter);
registerForContextMenu(list);
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle(Countries[info.position]);
String[] menuItems = getResources().getStringArray(
R.array.contextmenu);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = getResources().getStringArray(R.array.contextmenu);
String[] menuItems1 = getResources().getStringArray(R.array.game);
String menuItemName = menuItems[menuItemIndex];
String listItemName = menuItems1[info.position];
// selectedName = nameList[(int) info.id];
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(String.format("Selected %s for item %s", menuItemName,
listItemName));
return true;
}
These types serve different purposes.
The MenuInflator converts XML files into a Menu object representing the on-screen layout of the menu. In the first example, R.menu.context_menu refers to an associated XML file at res/menu/context_menu.xml that defines the choices that will appear in the menu. See Menu Resource for the format of XML menu resources. Here's a simple example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/open" android:title="Open"/>
<item android:id="#+id/info" android:title="More Info"/>
<item android:id="#+id/delete" android:title="Delete"/>
</menu>
The AdapterContextMenuInfo provides extra information when a context menu is brought up for a list, grid, etc. It allows you to determine which item the user selected (long pressed). Notice that both of your examples use this.