如何在ListView上长按传递变量?

时间:2011-03-24 13:56:41

标签: android listview variables contextmenu

我会有listview和很多项目。我希望用户可以长按项目并将其设置为收藏夹。要做到这一点,我需要在长按时将DB id添加到此菜单中。

我有以下代码

@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);
}

它工作得很好......但我想要做的是获取所选项目的文本和数据库ID。

如此不喜欢“收藏”我想写收藏:Item1。

如果有任何可以帮助的话,我会感激不尽。

这是我的适配器的代码......我实际上使用了示例的适配器。

    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;
  }
  }

并继续我的项目

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;
  }

  }

以下是主要活动类

中的代码
 @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;
 }

这是updateArray函数(在更改时调用)

private void updateArray() {
  toDoListCursor.requery();

  todoItems.clear();
  int j = 0;
  if (toDoListCursor.moveToFirst())
    do 
    { 
      String task =            toDoListCursor.getString(toDoListCursor.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();
}

和填充函数......

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();
}

2 个答案:

答案 0 :(得分:2)

您传递的ContextMenu.ContextMenuInfo包含有关列表中的哪个项目的信息。您可以使用它来获取所需的信息。

<强>更新

有点像dziobas在他的回答中提到你可以做这样的事情来了解所选项目在适配器中的位置:

AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
long arrayAdapterPosition = menuInfo.position;

现在您知道该位置,并可以从ArrayAdapter获取该位置。如果您将ArrayAdapter实例存储在成员变量中(在此示例中我将其命名为myArrayAdapter),则可以继续使用ArrayAdapter.getItem(int position)获取该项:

ToDoItem todoItem = (ToDoItem)myArrayAdapter.getItem(arrayAdapterPosition);
String task = todoItem.getTask();
int id = todoItem.getId();

您现在可以按如下方式设置菜单标题标题:

menu.setHeaderTitle("Favorite: " + task + Integer.toString(id));

答案 1 :(得分:0)

如果您的适配器支持获取Id,那么它应如下所示:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
    long id = menuInfo.id;
    ...