我會有listview和很多項目裏面。我希望該用戶可以長時間按項目並將其設置爲收藏夾。要做到這一點,我需要長按DB ID到這個菜單。如何在ListView上長按傳遞變量?
我有以下代碼
@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。
所以,不要「收藏」我想編寫Favorite:Item1。
如果anyoune可以幫助,我會感激。
這裏是我的適配器的代碼...我實際上使用了示例的適配器。
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;
}
}
而且furtherer我的項目
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();
}
從上面可以看出,ContextMenu.ContextMenuInfo menuInfo。是的,我的列表視圖是使用自定義ArrayAdapter填充的。目前它僅由圖標和名稱填充。 如果你能告訴我如何讓我們說出它的名字,將不勝感激。 謝謝。如果你需要任何額外的數據,我會提供。 – 2011-03-25 08:04:15
更新了答案。如果你仍然不太明白如何把它們放在一起,我認爲我們需要看到更多的源代碼才能夠成爲一個有用的例子。你的陣列適配器的定義,你放入它的項目的定義,他們正在使用的活動的相關部分等。 – rogerkk 2011-03-25 08:50:47
嗨,我有我的項目和我的項目適配器的代碼更新問題(主要問題) 。據我瞭解,我必須在我的項目的類中存儲一個函數來存儲位置在列表視圖和ID?那是對的嗎? 這樣做的最佳解決方案是什麼? – 2011-03-25 11:35:53