How to make each ListItem move to another action

I am relatively new to the Android world, but I have a quick question.

I have a list on one page with a potentially unlimited number of rows to be populated by the database. They will move on to various activities based on their type (e.g. clients, etc.). Is it possible to send a user to a specific activity on the basis of which they click?

I have seen some switch / case based solutions, but this seems to be used if you have a list of finite elements and you know the names of these elements in advance.

So, I say, if I use the switch / case solution, how does the application know which buttons go in this case? Does this need to be defined in the list itself or in the database?

I cannot provide any code at the moment, because it is not so far developed, and I am also under the NDA about the project!

+4
source share
1 answer

You can do this in setonitemclicklistener() . This function is called whenever you click on any of the lines in the list. The position / line number of the item you clicked is also passed to this function.

Since you are using a database to populate your list, you can do this:

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); ListView lv = getListView(); lv.setTextFilterEnabled(true); //SEE THIS FUNCTION lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /* "position" is the position/row of the item that you have clicked Query your table. Then move the cursor to "position" and get the details. THen based on the details call a new activity. */ Cursor cur=db.query("your table name",null,null,null,null,null.null); cur.moveToPosition(position); // see the argument int position //Extract details from this row,process it and send to the respective activiy }); 
+3
source

All Articles