Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get actual position of Cursor?
    text
    copied!<p>I'm trying to organize favourites in my app. I have a database with three columns:</p> <pre><code>_id | name | description | star 1 London some desc. 2 Berlin some desc. 3 Paris some desc. yes </code></pre> <p>I want to display favourites in listview. The refreshCursor() returns list of names with "star" column value "yes":</p> <pre><code>private static final String COLUMN_STAR = "star"; private void refreshCursor() { stopManagingCursor(cursor); Intent intent = getIntent(); String TABLE_NAME = intent.getStringExtra("tableName"); cursor = database.query(TABLE_NAME, null, COLUMN_STAR + " = ?", new String[] { "yes" }, null, null, null); startManagingCursor(cursor); } </code></pre> <p>It's ok.</p> <p>Then after I click on <code>Paris</code> I send extra string with clicked position:</p> <pre><code>lvData.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { String entryID = new Integer(position).toString(); Intent intent = new Intent(); intent.setClass(FavouritesActivity.this, ViewFavsActivity.class); Bundle b = new Bundle(); b.putString("elementId", entryID); intent.putExtras(b); startActivity(intent); } }); </code></pre> <p>But I get position (of <code>endtryID</code>) <code>0</code> and <code>ViewFavsActivity</code> displays decription of <code>London</code>. </p> <p>How to get actual position of <code>Cursor</code> and send it to <code>ViewFavsActivity</code>? Help, please.</p> <hr> <p>Part of <code>FavouritesActivity</code> (onCreate): // method refreshCursor is above</p> <pre><code>refreshCursor(); String[] from = new String[] { COLUMN_NAME }; int[] to = new int[] { R.id.tvText }; scAdapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to); lvData = (ListView) findViewById(R.id.list); lvData.setAdapter(scAdapter); lvData.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { String entryID = String.valueOf(id); Intent intent = new Intent(); intent.setClass(FavouritesActivity.this, ViewFavsActivity.class); Bundle b = new Bundle(); b.putString("elementId", entryID); intent.putExtras(b); startActivity(intent); </code></pre> <p>Part of <code>ViewFavsActivity</code>:</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // opening DB refreshCursor(); bundle = getIntent().getExtras(); TextView titleText = (TextView) findViewById(R.id.titleText); setTitle = database.query(TABLE_NAME, new String[] { COLUMN_DESC }, null, null, null, null, null); int entryID = Integer.parseInt(bundle.getString("elementId")); setTitle.moveToPosition(entryID); titleText.setText(setTitle.getString(setTitle .getColumnIndex(COLUMN_DESC))); } private void refreshCursor() { stopManagingCursor(cursor); Intent intent = getIntent(); cursor = database.query(TABLE_NAME, new String[] { COLUMN_ID, COLUMN_STAR, COLUMN_DESC }, "_id = ? AND star = ?", new String[] { intent.getStringExtra("elementId"), "yes" }, null, null, null); } </code></pre> <hr> <p><strong>Added:</strong></p> <p><a href="http://pastebin.com/kSiX5W5G" rel="nofollow">FavouritesActivity</a> on pastebin</p> <p><a href="http://pastebin.com/7MkQWiRK" rel="nofollow">ViewFavsActivity</a> on pastebin</p> <p><a href="http://www.2shared.com/file/OcgoUda7/SSCCE.html" rel="nofollow">Project</a></p>
 

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