Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying Cursor Results from DB in ListView after OnItemClick
    text
    copied!<p>I have an activity where I want it to display results from a database using ListView. The table has three columns: word, description, and category. On the activity page, I have a list of categories being read from an array. I want to set it up so that if you click on an item on the list (for example Cat1), the results returned from the cursor will be all words/descriptions with the category Cat1 in the DB. Currently I simply have a Toast with that category's name appear when clicked.</p> <p>As it is right now, the activity does not run properly. I have read around on the web, and am not sure how to proceed. Here is the code I have so far. If anyone can help me, I would really appreciate it.</p> <p>public class Categories extends ListActivity { DataBaseHelper db = new DataBaseHelper(this); private SQLiteDatabase data; int position;</p> <pre><code>private ListView list; private String[] categories = { "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12" }; @Override public void onCreate(Bundle icicle){ super.onCreate(icicle); setContentView(R.layout.cat_list); list = (ListView)findViewById(R.id.cat_listing); list.setAdapter(new ArrayAdapter&lt;String&gt;(this, R.layout.categories, categories)); list.setTextFilterEnabled(true); Cursor cursor = data.rawQuery("SELECT term, desc FROM words WHERE cat = '" + categories[position] + "'", null); startManagingCursor(cursor); String columns[] = new String[] { "term", "desc" }; int[] to = new int[] { R.id.cat_term, R.id.cat_desc }; SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.cat_result, cursor, columns, to); this.setListAdapter(myAdapter); list.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id){ CharSequence text = categories[position]; Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT); toast.show(); } }); } </code></pre> <p>}</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