krtkush
krtkush

Reputation: 1528

How to display data after choosing an option on ListView?

My ListView shows data from a database and what I want is that when I click on a specific ListView option a new activity starts and in that activity the data related to the chosen option is shown.

I have no problem in opening the new activity but am unable to let that activity know which option has been chosen on the ListView.

Code for ListView:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    database = new projectdatabase(ProjectExplorer.this);

    openDatabase();
}

private void openDatabase() {

    database.open();
    cursor = database.getDataforDisplay();
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {"project_name"}, new int[] { android.R.id.text1 }, 0);
    setListAdapter(adapter);

}

@Override
public void onListItemClick(ListView parent, View view, int position, long id) {

    super.onListItemClick(parent, view, position, id);

            Intent intent = new intent(this, openactivity.class);
            startActivity(intent);


    //What else to add here?

}      

I just want to send the "project_name" to the other activity so that I query it and retrieve other info.

Upvotes: 0

Views: 127

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Get project_name on List item click as:

@Override
public void onListItemClick(ListView parent, View view, int position, long id) {

    super.onListItemClick(parent, view, position, id);

      Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
      c.moveToPosition(position);

      // get project name here

      String str_projectname= c.getString(c.getColumnIndex("project_name"));

      Intent intent = new intent(this, openactivity.class);

      intent.putExtra("project_name", str_projectname);

      startActivity(intent);


    //What else to add here?

}   

Upvotes: 2

Related Questions