title: Set onclick events on textview created programmatically
body: <p>I create clickable textview dynamically depending on the records retrieved from the database just like this.</p>
  
  <pre><code>DBAdapter db = new DBAdapter(this);
  
  db.open();
  Cursor c = db.getAllProfiles();
  if(c.moveToFirst())
  {
      do{
          TextView tv = new TextView(this);
          tv.setText(c.getString(1));
          tv.setTextSize(18);
          tv.setTextColor(Color.BLACK);
          tv.setClickable(true);
          tv.setPadding(0, 10, 0, 0);
          tv.setGravity(Gravity.CENTER);
          mainLayout.addView(tv);
  
          tv.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
          Toast.makeText(getBaseContext(), tv.getText().toString(), Toast.LENGTH_LONG).show();
              }
          });
  
      }while(c.moveToNext());
  }
  else
      Toast.makeText(getBaseContext(), "No Profiles", Toast.LENGTH_LONG).show();
  db.close();
  </code></pre>
  
  <p>How can I set click events on each texview independently? On the way I see it, if I create a click event on the loop, the click event might get triggered by all textviews.</p>
  
  <p>*Update tried adding events in every loop but got this error:</p>
  
  <blockquote>
    <p>"cannot refer to a non-final variable inside an inner class defined in a different method".</p>
  </blockquote>