Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing SQLite values from SimpleCursorAdapter
    primarykey
    data
    text
    <p>I have a SQLite table called "Nodes" with a set of all nodes (each node has multiple properties).</p> <p>In an activity, I use an extension of SimpleCursorAdapter to display a <em>subset</em> of the Nodes table, based on a SQL query, passing it the Cursor over the table of results (from the query).</p> <pre><code>{id_long, name_string, description_string, nodetype_enum_toString...} 1, "home", "home_desc", "cool" 2, "item1", "item1_desc", "warm" 3, "item2", "item2_desc", "hot" 4, "item3", "item3_desc", "warm" 5, "item4", "item4_desc", "hot" </code></pre> <p>I want to modify each of the list items in my ListActivity such that it displays a different color based on whether an item is cool, warm or hot. I have a small (empty) view, but it's always defaulting to white (the 'else' portion).</p> <pre><code>public View getView(int position, View convertView, ViewGroup parent) { convertView = super.getView(position, convertView, parent); if (convertView == null) convertView = View.inflate(mContext, R.layout.viewstack_item, null); View nodetype_view = (View) convertView.findViewById(R.id.nodetype_view); mGenCursor_cur = mDataHelper_db.getAll(); mGenCursor_cur.moveToPosition(position); Log.i("test", "node type key " + mGenCursor_cur.getColumnIndex(DataHelper.NODES_TYPE_KEY)); String type_nt = mGenCursor_cur.getString(mGenCursor_cur.getColumnIndex(DataHelper.NODES_TYPE_KEY)); String name_str = mGenCursor_cur.getString(mGenCursor_cur.getColumnIndex(DataHelper.NODES_NAME_KEY)); Log.i("getView", title_str + "typeis " + type_nt + " at position " + position); if (prio_np.equals(NodePrio.HIGH.toString())) { Log.i("prio_np", "high red"); prioView_view.setBackgroundColor(Color.RED); } else if (prio_np.equals(NodePrio.NORMAL.toString())) { Log.i("prio_np", "norm magenta"); prioView_view.setBackgroundColor(Color.MAGENTA); } else { Log.i("prio_np", "else (low) white"); prioView_view.setBackgroundColor(Color.WHITE); } mGenCursor_cur.close(); bindView(convertView, mContext, getCursor()); return(convertView); } </code></pre> <p>My issue is that while the cursor passed to the SimpleListAdapter will contain the _IDs (I assume) of the corresponding nodes in the subset I want, the <em>position</em> parameter in <em>getView</em> is local to the subset, so I can't cross-reference it with the Nodes table. Am I missing some easy technique?</p> <h2>As always, any help appreciated.</h2> <p>comment 4 of the marked answer helped me solve this: storing the cursor that was passed to the SimpleCursorAdapter as a member variable, then using move to position, and getLong:</p> <pre><code>mCursor_cur.moveToPosition(position); long id = mCursor_cur.getLong(0); </code></pre> <p>where <em>position</em> is the local parameter in the getView() method, and 0 is used for getLong because the db_id is the first column in the cursor passed to the SimpleCursorAdapter</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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