I'm creating an android app. In this app, I have a ListView populated with data from SQLite. I am trying to add an onClickEventListener to each row of the ListView so that when a menu item is clicked it opens up another Activity with more information from the Database about the item clicked. I have succeeded in adding an event listener to the ListView, but I am not sure how to pass on database information depending on the list item clicked.
Here is my code:
public class MainActivity extends AppCompatActivity {
private DatabaseHelper dbh;
private ArrayList listItems = new ArrayList();
ArrayList<String[]> noteList;
private ArrayAdapter adapter;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbh = new DatabaseHelper(this);
dbh.open();
lv = (ListView) findViewById(R.id.noteListView);
noteList = dbh.selectAll();
String id = "";
String content = "";
for(int i = 0; i < noteList.size(); i++){
content = noteList.get(i)[1];
id = noteList.get(i)[0];
listItems.add(id + ", " + content);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent i = new Intent(MainActivity.this, EditNote.class);
//i.putExtra();
startActivity(i);
}
});
}
}