How to pass variables on long press on ListView?
I would have listview and a lot of items inside. I want that user can long press on item and set it as Favorite. To do that, I need to get DB id to this menu on long press.
I have the following code
@Override
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Favorite");
menu.add(0, REMOVE_TODO, Menu.NONE, R.string.favorit_add);
}
It works just fine... But what I want to do is to get text and database id of selected item.
So insetead of "Favorite" I would like to write Favorite: Item1.
If anyoune could help I would be thankful.
Here is a code for my adapter... I actually used example's adapter.
package com.TVSpored;
import android.content.Context;
import java.util.*;
import android.view.*;
import android.widget.*;
public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> {
int resource;
public ToDoItemAdapter(Context _context,
int _resource,
List<ToDoItem> _items) {
super(_context, _resource, _items);
resource = _resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout todoView;
ToDoItem item = getItem(position);
String taskString = item.getTask();
String icon_name = item.getCreated();
int fav = item.getFavorite();
if (convertView == null) {
todoView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, todoView, true);
} else {
todoView = (LinearLayout) convertView;
}
ImageView favView = (ImageView)todoView.findViewById(R.id.rowImgFav);
ImageView channelView = (ImageView)todoView.findViewById(R.id.rowImg);
TextView channelName = (TextView)todoView.findViewById(R.id.row);
//dateView.setText(dateString);
channelView.setImageResource(getContext().getResources().getIdentifier("com.TVSpored:drawable/channels_"+icon_name , null, null));
channelName.setText(taskString);
if(fav == 0)
{
favView.setImageResource(R.drawable.sys_srcek_disabled);
}
else
{
favView.setImageResource(R.drawable.sys_srcek);
}
return todoView;
}
}
And furtherer my Item
package com.TVSpored;
import java.text.SimpleDateFormat;
public class ToDoItem {
String task;
String created;
Integer fav;
Integer id;
public String getTask() {
return task;
}
public String getCreated() {
return created;
}
public Integer getFavorite()
{
return fav;
}
public Integer getID()
{
return id;
}
public ToDoItem(String _task, String _created, int _fav, int _id) {
task = _task;
created = _created;
fav = _fav;
id = _id;
}
}
Here is a code in main activity class
@Override
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Urejanje kanala");
menu.add(0, REMOVE_TODO, Menu.NONE, R.string.favorit_add);
// static final private int REMOVE_TODO = Menu.FIRST + 1; // defined ad the begining
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
int arrayAdapterPosition = menuInfo.position;
ToDoItem todoItem = (ToDoItem)aa.getItem(arrayAdapterPosition);
String task = todoItem.getTask();
int id = todoItem.getID();
int index = myListView.getSelectedItemPosition();
aa.getItemId(index);
changeFavorite(id);
return true;
}
Here is updateArray function (called on change)
private void updateArray() {
toDoListCursor.requery();
todoItems.clear();
int j = 0;
if (toDoListCursor.moveToFirst())
do
{
String task = toDoListCursor.getString(toDoLi开发者_Go百科stCursor.getColumnIndex(ToDoDBAdapter.KEY_NAME));
String created = toDoListCursor.getString(toDoListCursor.getColumnIndex(ToDoDBAdapter.KEY_EPG_NAME));
int fav = toDoListCursor.getInt(toDoListCursor.getColumnIndex(ToDoDBAdapter.KEY_EPG_NAME));
int id = toDoListCursor.getInt(toDoListCursor.getColumnIndex(ToDoDBAdapter.KEY_ID));
ToDoItem newItem = new ToDoItem(task, created, fav, id);
todoItems.add(0, newItem);
j++;
}
while(toDoListCursor.moveToNext());
aa.notifyDataSetChanged();
}
and a populate function...
private void populateTChannels() {
// Get all the todo list items from the database.
toDoListCursor = toDoDBAdapter. getAllToDoItemsCursor();
if((toDoListCursor.getCount() == 0) || !toDoListCursor.moveToFirst())
{
toDoDBAdapter.populateDB();
}
if(toDoDBAdapter.ChannelsArray.length > toDoListCursor.getCount())
{
toDoDBAdapter.populateDBWhitCheck();
}
toDoListCursor = toDoDBAdapter. getAllToDoItemsCursor();
startManagingCursor(toDoListCursor);
// Update the array.
updateArray();
}
The ContextMenu.ContextMenuInfo
you get passed contains information about which item in the list was clicked. You can probably use this to get the information you need.
Update:
Somewhat like dziobas mentions in his answer you can do something like this to get to know which position the selected item has in your adapter:
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
long arrayAdapterPosition = menuInfo.position;
Now you know the position, and can fetch it from your ArrayAdapter
. If you have this ArrayAdapter
instance stored in a member variable (in this example I have named it myArrayAdapter), you can then proceed to get the item with ArrayAdapter.getItem(int position)
:
ToDoItem todoItem = (ToDoItem)myArrayAdapter.getItem(arrayAdapterPosition);
String task = todoItem.getTask();
int id = todoItem.getId();
You could now proceed to set the menu header title as follows:
menu.setHeaderTitle("Favorite: " + task + Integer.toString(id));
If your adapter supports getting Id, so it should look like this:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
long id = menuInfo.id;
...
精彩评论