如何通过长preSS变量上的ListView? [英] How to pass variables on long press on ListView?

查看:101
本文介绍了如何通过长preSS变量上的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会ListView和很多物品的内部。我希望用户能够在项目长preSS并将其设置为收藏。为了做到这一点,我需要DB id来长preSS此菜单。

我有以下的code

  @覆盖
公共无效onCreateContextMenu(文本菜单菜单,
                              视图V,
                              ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo);

menu.setHeaderTitle(最爱);
menu.add(0,REMOVE_TODO,Menu.NONE,R.string.favorit_add);
}
 

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

所以,最喜欢的insetead我想写喜欢的:项目1

如果anyoune可以帮助我将感谢。

下面是一个$ C $下我的适配器......我实际使用的例子的适配器。

 包com.TVSpored;

进口android.content.Context;
导入的java.util。*;
进口android.view *。
进口android.widget *。

公共类ToDoItemAdapter扩展ArrayAdapter<内的TodoItem> {

  INT资源;

  公共ToDoItemAdapter(上下文_context,
                             INT _resource,
                             名单<内的TodoItem> _items){
    超(_context,_resource,_items);
    资源= _resource;
  }

  @覆盖
  公共查看getView(INT位置,查看convertView,ViewGroup中父){
    的LinearLayout todoView;

    内的TodoItem项目=的getItem(位置);

    串taskString = item.getTask();
    串icon_name = item.getCreated();
    INT最爱= item.getFavorite();

    如果(convertView == NULL){
      todoView =新的LinearLayout(的getContext());
      字符串充气= Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater VI =(LayoutInflater)的getContext()getSystemService(充气)。
      vi.inflate(资源,todoView,真正的);
    } 其他 {
      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);

    如果(最爱== 0)
    {
        favView.setImageResource(R.drawable.sys_srcek_disabled);
    }
    其他
    {
        favView.setImageResource(R.drawable.sys_srcek);
    }
    返回todoView;
  }
  }
 

和furtherer我的项目

 包com.TVSpored;

进口java.text.SimpleDateFormat的;

公共类内的TodoItem {

  字符串的任务;
  字符串创建;
  整数最爱;
  整数ID;

  公共字符串getTask(){
    返回任务;
  }

  公共字符串getCreated(){
    返回创建的;
  }

  公共整数getFavorite()
  {
      回到最爱;
  }

  公共整数的getID()
  {
      返回ID;
  }

  公共内的TodoItem(字符串_Task,字符串_created,INT _fav,诠释_id){
    任务= _Task;
    创建= _created;
    最爱= _fav;
    ID = _id;
  }

  }
 

下面是一个code的主要活动类

  @覆盖
 公共无效onCreateContextMenu(文本菜单菜单,
                              视图V,
                              ContextMenu.ContextMenuInfo menuInfo){
 super.onCreateContextMenu(菜单,V,menuInfo);


 menu.setHeaderTitle(Urejanje kanala);
 menu.add(0,REMOVE_TODO,Menu.NONE,R.string.favorit_add);
 // static final的私人诠释REMOVE_TODO = Menu.FIRST + 1; //定义的广告开始时
 }

 @覆盖
 公共布尔onOptionsItemSelected(菜单项项){
 super.onOptionsItemSelected(项目);

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

 内的TodoItem内的TodoItem =(内的TodoItem)aa.getItem(arrayAdapterPosition);
 串任务= todoItem.getTask();
 INT的id = todoItem.getID();

 INT指数= myListView.getSelectedItemPosition();
 aa.getItemId(指数);

 changeFavorite(ID);
 返回true;
 }
 

下面是updateArray功能(呼吁其他城市)

 私人无效updateArray(){
  toDoListCursor.requery();

  todoItems.clear();
  INT J = 0;
  如果(toDoListCursor.moveToFirst())
    做
    {
      串任务= toDoListCursor.getString(toDoListCursor.getColumnIndex(ToDoDBAdapter.KEY_NAME));
      字符串创建= toDoListCursor.getString(toDoListCursor.getColumnIndex(ToDoDBAdapter.KEY_EPG_NAME));
      INT最爱= toDoListCursor.getInt(toDoListCursor.getColumnIndex(ToDoDBAdapter.KEY_EPG_NAME));
      INT的id = toDoListCursor.getInt(toDoListCursor.getColumnIndex(ToDoDBAdapter.KEY_ID));

      内的TodoItem的newitem =新内的TodoItem(任务,创建最爱,身份证);
      todoItems.add(0,的newitem);
      J ++;
    }
    而(toDoListCursor.moveToNext());

  aa.notifyDataSetChanged();
}
 

和一个填充功能...

 私人无效populateTChannels(){
//从数据库中所有的待办事项列表项。
toDoListCursor = toDoDBAdapter。 getAllToDoItemsCursor();
如果((toDoListCursor.getCount()== 0)||!toDoListCursor.moveToFirst())
{
    toDoDBAdapter.populateDB();
}

如果(toDoDBAdapter.ChannelsArray.length> toDoListCursor.getCount())
{
    toDoDBAdapter.populateDBWhitCheck();
}
toDoListCursor = toDoDBAdapter。 getAllToDoItemsCursor();
startManagingCursor(toDoListCursor);

//更新阵列。
updateArray();
}
 

解决方案

ContextMenu.ContextMenuInfo 您获得通过包含哪些项目列表中的被点击的信息。你也许可以用它来获得您所需要的信息。

更新:

有点像dziobas提到了他的答案,你可以做这样的事情来了解哪个位置选定的项目在您的适配器:

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

现在你知道的位置,并且可以从你的 ArrayAdapter 获取它。如果您有存储在一个成员变量此 ArrayAdapter 实例(在这个例子中,我把它命名为myArrayAdapter),你就可以继续获得与的项目ArrayAdapter.getItem(INT位置)

 内的TodoItem内的TodoItem =(内的TodoItem)myArrayAdapter.getItem(arrayAdapterPosition);
串任务= todoItem.getTask();
INT的id = todoItem.getId();
 

您现在可以进行设置的菜单标题标题如下:

  menu.setHeaderTitle(最喜欢的:+任务+ Integer.toString(ID));
 

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

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

这篇关于如何通过长preSS变量上的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆