title: How to retrieve data from a particular db table in a new activity as a result of selecting a list item from a db table in the previous activity
body: <p>I need some help here, I am retrieving a list of items (categories) from a database table in the main activity and that’s all working well. The issue is that the user is supposed to click on one of the items which will cause another table in the db to be queried but in another activity (workbook.java). 
  So how do I carry the IDs of the categories clicked to the next activity and also know exactly which item was clicked so that the right table is queried?</p>
  
  <p>Code that retrieves the categories from one db table and presents them in a list view and also the method to handle clicks on the list items:
  MainActivity.java</p>
  
  <pre><code>@SuppressLint("NewApi")
  public void displayCategories(){
      cursor = dbhelper.getCategories();
      String[] FROM = { FeedReaderDbHelper.KEY_CATEGORY_NAME};
      int[] TO = {R.id.textView1};
      adapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, FROM, TO, 0);
      categories = (ListView) findViewById(android.R.id.list); 
      categories.setAdapter(adapter);
      categories.setOnItemClickListener(new ListClickHandler());
  }
  
  public class ListClickHandler implements OnItemClickListener{
  @Override
  public void onItemClick(AdapterView&lt;?&gt; adapter, View view, int position, long arg3) {
      // TODO Auto-generated method stub
      TextView category_text = (TextView) view.findViewById(R.id.textView1);
      String category = category_text.getText().toString();
      Intent intent = new Intent(MainActivity.this, WorkBook.class);
      intent.putExtra("selected-item", category);
      intent.putExtra("item-ID", position);
      startActivity(intent);
  }
  }
  </code></pre>
  
  <p>The code below is in WorkBook.java which is supposed to query another table basing on the category selected in the previous activity (MainActivity.java):</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.work_book);
      text = (TextView) findViewById(R.id.text);
  
      //get the intent from which this activity is called
      Intent intent = getIntent();
      //fetch value from key-value pair and make it visible on textView
      String category = intent.getStringExtra("selected-item");
              text.setText("You are here: "+category);
  
  }
  </code></pre>
  
  <p>Code for my database. The categories table is the one being queried in the main activity for a list of categories. Then if a user selects Greetings category, table greetings should be queried but in the next activity.</p>
  
  <pre><code>package database;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import com.ateso.learnateso.Category;
  
  public class FeedReaderDbHelper extends SQLiteOpenHelper {
  
  public static final String DATABASE_NAME = "LearnAteso.db";
  public static final int DATABASE_VERSION = 1;
  
  //Table Names
  public static final String CATEGORIES_TABLE = "categories";
  public static final String GREETINGS_TABLE = "greetings";
  
  //Common column name
  public static final String KEY_ID = "_id";
  
  //Categories table column names
  public static final String KEY_CATEGORY_NAME = "catname";
  
  //greetings table column names
  public static final String KEY_GREETING = "greeting";
  public static final String KEY_HINT = "hint";
  public static final String KEY_AUDIO = "audio";
  public static final String KEY_ANSWER = "answer";
  public static final String KEY_OPTION_1 = "option_1";
  public static final String KEY_OPTION_2 = "option_2";
  public static final String KEY_OPTION_3 = "option_3";
  
  //Table create statements
      //categories table create statement
      private static final String CREATE_TABLE_CATEGORIES = 
              "create table " + CATEGORIES_TABLE + "(" + KEY_ID + " integer primary key, "
                      + KEY_CATEGORY_NAME + " text);";
  
      //greetings create statement
      private static final String CREATE_TABLE_GREETINGS = 
              "create table " + GREETINGS_TABLE + "(" + KEY_ID + " integer primary key, "
              + KEY_GREETING+ " text, " + KEY_HINT + " text, " + KEY_AUDIO + " text, " + KEY_ANSWER + " text, "   
              + KEY_OPTION_1 + " text, " + KEY_OPTION_2 + " text, " + KEY_OPTION_3 + " text);";
  
  
    //constructor
      public FeedReaderDbHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
    //  @Override
      public void onCreate(SQLiteDatabase db) {
          //creating the tables
          db.execSQL(CREATE_TABLE_CATEGORIES);
          db.execSQL(CREATE_TABLE_GREETINGS);
      }
  
     // @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          //on upgrade drop old tables
          db.execSQL("DROP TABLE IF EXISTS " + CATEGORIES_TABLE);
          db.execSQL("DROP TABLE IF EXISTS " + GREETINGS_TABLE);
          //create new tables
          onCreate(db);
      }
    //-----------------categories table methods---------------//
   //getting the categories out of the db
   public  Cursor getCategories(){
      SQLiteDatabase db = this.getReadableDatabase();
      String sql = "SELECT * FROM categories;";
      Cursor c =db.rawQuery(sql, null);
      return c;   
  }   
  //getting greetings table content from the db
  public Cursor getAteso(){
      SQLiteDatabase db = this.getReadableDatabase();
  
      String sql = "SELECT * FROM " + GREETINGS_TABLE + ";";
  
      Cursor c = db.rawQuery(sql, null);
      if (c != null &amp;&amp; c.getCount()&gt;0){
          c.moveToFirst();
          do{
              String audio = c.getString(c.getColumnIndex(KEY_AUDIO));
              String greeting = c.getString(c.getColumnIndex(KEY_GREETING));
              String hint = c.getString(c.getColumnIndex(KEY_HINT));
              String answer = c.getString(c.getColumnIndex(KEY_ANSWER));
              String opt_1 = c.getString(c.getColumnIndex(KEY_OPTION_1));
              String opt_2 = c.getString(c.getColumnIndex(KEY_OPTION_2));
              String opt_3 = c.getString(c.getColumnIndex(KEY_OPTION_3));
          } while (c.moveToNext());
      }
      return c;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>So how do I track which category a user selects in MainActivity.java in order to query the respective table in WorkBook.java.</p>
  
  <p>Thanks for the help</p>