title: Setting On Click Listener For A Dynamic ListView
body: <p>thanks for always being available...</p>
  
  <p>I have been able to populate my ListView with information i have in the database. </p>
  
  <p>Now, I want setOnClickListener for the items to go to different pages. Currently, I am using a "switch(position)" construct, but it doesn't function appropriately since I can't hard code how many items would be on the ListView. </p>
  
  <p>Please, kindly help me out on this one.</p>
  
  <p>public class SurveyActivity extends AppCompatActivity {</p>
  
  <pre><code>ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  SQLiteDatabase db;
  DatabaseHelper helper = new DatabaseHelper(this);
  
  String listItem = "";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_survey);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Add new survey", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
  
              PopupMenu pop = new PopupMenu(SurveyActivity.this, fab);
              pop.getMenuInflater().inflate(R.menu.addsurvey_menu, pop.getMenu());
              pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  
                  public boolean onMenuItemClick(MenuItem item) {
  
                      Toast.makeText(getApplicationContext(), "Your choice is to " + item.getTitle().toString().toUpperCase() + " a survey form", Toast.LENGTH_LONG).show();
  
                      switch (item.getItemId()) {
  
                          case R.id.c:
                              //Takes admin to page where survey is created
  
                              Intent i = new Intent(getApplicationContext(), CreateSurvey.class);
                              startActivity(i);
  
                          case R.id.d:
                              //gets ready created XML forms from server
  
                          case R.id.r:
                              //refreshes and updates survey list from db
  
                      }
  
                      return true;
                  }
              });
  
              pop.show();
  
          }
      });
  
      db = helper.getReadableDatabase();
  
      //Cursor crs = db.rawQuery("SELECT * FROM tbNames", null);
      Cursor crs=db.query("tbNames",new String[]{ "names"},null,null,null,null,null);
  
      if(crs.moveToFirst()){
          do {
              listItem =crs.getString(0);
              list.add(listItem);
  
          }while(crs.moveToNext());
      }
  
      ListView surveyList = (ListView) findViewById(R.id.surveylist);
  
      ArrayAdapter&lt;String&gt; aAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
      surveyList.setAdapter(aAdapter);
      registerForContextMenu(surveyList);
  
      surveyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
              switch (position) {
  
                  case 0:
                      Intent i = new Intent(getApplicationContext(), hivActivity.class);
                      startActivity(i);
                      break;
                  case 1:
                      Intent intent = new Intent(SurveyActivity.this, CreatedSurveyActivity.class);
                      startActivity(intent);
              }
          }
      });
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My app would allow users create multiple surveys which would show up in the LIstView.</p>
  
  <p>The problem is how to set Listeners for the items dynamically too.</p>