Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass variables on long press on ListView?
    text
    copied!<p>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.</p> <p>I have the following code</p> <pre><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); } </code></pre> <p>It works just fine... But what I want to do is to get text and database id of selected item. </p> <p>So insetead of "Favorite" I would like to write Favorite: Item1.</p> <p>If anyoune could help I would be thankful. </p> <p>Here is a code for my adapter... I actually used example's adapter.</p> <pre><code> package com.TVSpored; import android.content.Context; import java.util.*; import android.view.*; import android.widget.*; public class ToDoItemAdapter extends ArrayAdapter&lt;ToDoItem&gt; { int resource; public ToDoItemAdapter(Context _context, int _resource, List&lt;ToDoItem&gt; _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; } } </code></pre> <p>And furtherer my Item</p> <pre><code>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; } } </code></pre> <p>Here is a code in main activity class</p> <pre><code> @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; } </code></pre> <p>Here is updateArray function (called on change)</p> <pre><code>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(); } </code></pre> <p>and a populate function... </p> <pre><code>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 &gt; toDoListCursor.getCount()) { toDoDBAdapter.populateDBWhitCheck(); } toDoListCursor = toDoDBAdapter. getAllToDoItemsCursor(); startManagingCursor(toDoListCursor); // Update the array. updateArray(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload