title: Android table row onClickListener
body: <p>I am having some problems when trying to trigger events on table layout in Android. These are the codes I used to populate the table layout dynamically:</p>
  
  <pre><code>private void BuildTable() {
      try {
          DatabaseAdapter mDbHelper = new DatabaseAdapter(Category.this);
          mDbHelper.open();
          CategoryController cc = new CategoryController(mDbHelper.open());
          Cursor mCur = mDb.rawQuery(cc.getAllCat(), null);
  
          if (mCur.getCount() != 0) {
              if (mCur.moveToFirst()) {
                  // Setting table header
                  TableRow row = new TableRow(this);
                  row.setLayoutParams(new TableLayout.LayoutParams(
                          TableLayout.LayoutParams.MATCH_PARENT,
                          TableLayout.LayoutParams.WRAP_CONTENT));
  
                  do {
                      int cols = mCur.getColumnCount();
                      row = new TableRow(this);
  
                      for (int j = 1; j &lt; cols; j++) {
                          // Dynamically load data fetch from database
                          // into table layout
                          TextView tv = new TextView(this);
                          tv.setLayoutParams(new TableRow.LayoutParams(
                                  TableRow.LayoutParams.MATCH_PARENT,
                                  TableRow.LayoutParams.WRAP_CONTENT));
                          tv.setPadding(5, 5, 10, 5);
                          tv.setGravity(Gravity.LEFT);
                          tv.setText(mCur.getString(j));
                          row.addView(tv);
                          row.setId(j);
                      }
  
                      row.setClickable(true);
  
                      row.setOnClickListener(tablerowOnClickListener);
                      table_layout.addView(row);
                  } while (mCur.moveToNext());
              }
          }
      } catch (SQLException mSQLException) {
          throw mSQLException;
      }
  }
  </code></pre>
  
  <p>And here is my table row onClickListener:</p>
  
  <pre><code>private OnClickListener tablerowOnClickListener = new OnClickListener() {
      public void onClick(View v) {
          //Highlight selected row
          v.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
          final int k = v.getId();
          Toast.makeText(Category.this, Integer.toString(k),
                  Toast.LENGTH_SHORT).show();
      }
  }; 
  </code></pre>
  
  <p>My problem is when I highlighted certain row and I select another row again, the previous highlighted row remains the same highlighted background color. I wonder how to solve it?</p>
  
  <p>Thanks in advance.</p>