title: Pass id of sqlite table from listview to another activity
body: <p>I get difficulties to get the id of a sqlite. I have listview, then I want to get the id of a sqlite table then i can use it later when I want to save the score after complete the quiz.</p>
  
  <p>The scenario is like this: user creates username (or click if ever created username), after he/she clicks one of the username then take the quiz, after complete it user will get score then score is saved and can be viewed later on.</p>
  
  <p>Database code</p>
  
  <pre><code>public class DatabaseUsername extends SQLiteOpenHelper {
  private static final String DATABASE_NAME="usernm.db";
  private static final int SCHEMA_VERSION=1;
  
  
  public DatabaseUsername(Context context) {
      super(context, DATABASE_NAME, null, SCHEMA_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE almag (_id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT, jekel TEXT);");
      db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT, score INTEGER, userId INTEGER NOT NULL, FOREIGN KEY (userId) REFERENCES almag(_id) ON DELETE CASCADE);"); //create table score
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // no-op, since will not be called until 2nd schema
      // version exists
  
  }
  
  public Cursor getAll() {
      return(getReadableDatabase()
                      .rawQuery("SELECT _id, nama, jekel FROM almag ORDER BY nama",
                                          null));
  }
  
  public Cursor getById(String id) {
      String[] args={id};
  
      return(getReadableDatabase()
                      .rawQuery("SELECT _id FROM almag WHERE _ID=?",
                                          args));
  
  }
  
  public void insert(String nama, String jekel) {
      ContentValues cv=new ContentValues();
  
      cv.put("nama", nama);
      cv.put("jekel", jekel);
  
      getWritableDatabase().insert("almag", nama, cv);
  }
  
  public void insertScore (int score, int userId) {
      ContentValues cv=new ContentValues();
  
      cv.put("score", score);
      cv.put("userId", userId);
      getWritableDatabase().insert("score", null, cv);
  }
  
  
  public String getNama(Cursor c) {
      return(c.getString(1));
  }
  
  public String getJekel(Cursor c) {
      return(c.getString(2));
  }
  
  public static int getId (Cursor c) {
      return(c.getInt(1));
  }}
  </code></pre>
  
  <p>UsernameList.java</p>
  
  <pre><code>public class UsernameList extends ListActivity {
  public final static String ID_EXTRA="com.rika.fyp.player";
  Cursor model=null;
  Cursor mode=null;
  AlmagAdapter adapter=null;
  EditText nama=null;
  RadioGroup jekel=null;
  DatabaseUsername helper=null;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.usernamelist);
  
      helper=new DatabaseUsername(this);
  
      nama=(EditText)findViewById(R.id.nama);
      jekel=(RadioGroup)findViewById(R.id.jekel);
  
      model=helper.getAll();
      startManagingCursor(model);
      adapter=new AlmagAdapter(model);
      setListAdapter(adapter);
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
  
      helper.close();
  }
  
  
   public void onListItemClick(ListView parent, View view, int position, long id) {
          Intent intent = new Intent(this, TheEndActivity.class);
          Cursor cursor = (Cursor) adapter.getItem(position);
          intent.putExtra("USER_ID", cursor.getInt(cursor.getColumnIndex("_id")));
          startActivity(intent);
  
          Intent i=new Intent(UsernameList.this, QuizAppActivity.class);
          startActivity(i);
      }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      new MenuInflater(this).inflate(R.menu.option, menu);
  
      return(super.onCreateOptionsMenu(menu));
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      if (item.getItemId()==R.id.add) {
          startActivity(new Intent(UsernameList.this, UsernameRegister.class));
  
          return(true);
      }
  
      return(super.onOptionsItemSelected(item));
  }
  
  private View.OnClickListener onSave=new View.OnClickListener() {
      public void onClick(View v) {
          String type=null;
  
          switch (jekel.getCheckedRadioButtonId()) {
              case R.id.pria:
                  type="Pria";
                  break;
              case R.id.perempuan:
                  type="Perempuan";
                  break;
  
          }
  
          helper.insert(nama.getText().toString(), type);
  
          model.requery();
      }
  };
  
  class AlmagAdapter extends CursorAdapter {
      AlmagAdapter(Cursor c) {
          super(UsernameList.this, c);
      }
  
      @Override
      public void bindView(View row, Context ctxt,Cursor c) {
          AlmagHolder holder=(AlmagHolder)row.getTag();
  
          holder.populateFrom(c, helper);
      }
  
      @Override
      public View newView(Context ctxt, Cursor c, ViewGroup parent) {
          LayoutInflater inflater=getLayoutInflater();
          View row=inflater.inflate(R.layout.usernamerow, parent, false);
          AlmagHolder holder=new AlmagHolder(row);
  
          row.setTag(holder);
  
          return(row);
      }
  }
  
  static class AlmagHolder {
      private TextView nama=null;
      private TextView alamat=null;
      private ImageView icon=null;
      private View row=null;
  
      AlmagHolder(View row) {
          this.row=row;
  
          nama=(TextView)row.findViewById(R.id.title);
          icon=(ImageView)row.findViewById(R.id.icon);
      }
  
      void populateFrom(Cursor c, DatabaseUsername helper) {
          nama.setText(helper.getNama(c));
  
          if (helper.getJekel(c).equals("Pria")) {
              icon.setImageResource(R.drawable.pria);
          }
          else if (helper.getJekel(c).equals("Perempuan")) {
              icon.setImageResource(R.drawable.perempuan);
          }
  
      }
  }}
  </code></pre>
  
  <p>TheEndActivity.java</p>
  
  <pre><code>public class TheEndActivity extends Activity implements OnClickListener {
  
   protected int userId;
  
  DatabaseUsername helper=null;
  Object almagId=null;
  int id;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.theendactivitylayout);
      final SetGame currentGame = ((TheApplication)getApplication()).getCurrentGame();
      String result = "Your Score is " + currentGame.getRight() + "/" + currentGame.getNumRounds() + ".. ";
      final String comment = Mark.getResultComment(currentGame.getRight(), currentGame.getNumRounds(), getDifficultySettings());
  
      userId = getIntent().getIntExtra("USER_ID", 0);
       SQLiteDatabase db = (new DatabaseUsername(this)).getWritableDatabase();
          final Cursor cursor = db.rawQuery("SELECT alm._id, alm.nama FROM almag alm  WHERE emp._id = ?", 
                                  new String[]{""+userId});
  
  
      TextView results = (TextView)findViewById(R.id.endgameResult);
      results.setText(result + comment);
  
      int image = Mark.getResultImage(currentGame.getRight(), currentGame.getNumRounds(), getDifficultySettings());
      ImageView resultImage = (ImageView)findViewById(R.id.resultPage);
      resultImage.setImageResource(image);
  
      //handle button actions
      Button finishBtn = (Button) findViewById(R.id.finishBtn);
      Button answerBtn = (Button) findViewById(R.id.answerBtn);
  
      finishBtn.setOnClickListener(new View.OnClickListener() {
  
      @Override
      public void onClick(View v) {
  
              finish();
  
              helper.insertScore(currentGame.getRight(), userId);
  
              Intent i = new Intent(TheEndActivity.this, MainMenu.class);
              startActivity(i);               
      }
      });
  
      answerBtn.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
                  Intent i = new Intent(TheEndActivity.this, AnsActivity.class);
                  startActivityForResult(i, AppRule.PLAYBUTTON);
  
          }
          });
  
  }
  
  private int getDifficultySettings() {
      SharedPreferences settings = getSharedPreferences(AppRule.SETTINGS, 0);
      int diff = settings.getInt(AppRule.DIFFICULTY, 2);
      return diff;
  }
  
  
      public boolean onKeyDown(int keyCode, KeyEvent event)
  {
      switch (keyCode)
      {
      case KeyEvent.KEYCODE_BACK :
          return true;
      }
  
      return super.onKeyDown(keyCode, event);
  }
  
  
      @Override
      public void onClick(View arg0) {
          // TODO Auto-generated method stub
  
      }}
  </code></pre>
  
  <p>I've tried these code above, but still can get the id. I hope someone can help me. </p>
  
  <p>These codes below to insert the score (in TheEndActivity.java)</p>
  
  <pre><code>finishBtn.setOnClickListener(new View.OnClickListener() {       
          @Override
          public void onClick(View v) {
  
                  helper.insertMark(currentGame.getRight(), userId );
                  Intent i = new Intent(TheEndActivity.this, MainMenu.class);
                  startActivity(i);
          }
          });
  </code></pre>
  
  <p>insertMark in Database.java</p>
  
  <pre><code>public void insertMark(int score, int userId) {
      ContentValues cv=new ContentValues();
  
      cv.put("score", score);
      cv.put("userId", userId);
  
      getWritableDatabase().insert("score", null, cv);
  }
  </code></pre>