Android: how to make the items from listview opens to diff. activity/intent?

You can start different activities based on the position in the ListView of the item you just clicked: private static final int ACTIVITY_0 = 0; private static final int ACTIVITY_1 = 1; private static final int ACTIVITY_2 = 2; @Override protected void onListItemClick(ListView l, View v, int position, long id) { super. OnListItemClick(l, v, position, id); final Intent intent = new Intent(); // Set up different intents based on the item clicked: switch (position) { case ACTIVITY_0: intent. SetClass(this, com.company.merrill.

IntentIntegrator. Class); break; case ACTIVITY_1: intent. SetClass(this, com.company.merrill.SecondActivity.

Class); break; case ACTIVITY_2: intent. SetClass(this, com.company.merrill.ThirdActivity. Class); break; default: break; } // Pass the item position as the requestCode parameter, so on the `Activity`'s // return you can examine it, and determine which activity were you in prior.

StartActivityForResult(intent, position); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super. OnActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { // Perform different actions based on from which activity is // the application returning: switch (requestCode) { case ACTIVITY_0: // contents contains whatever the code was String contents = intent. GetStringExtra("SCAN_RESULT"); // Format contains the type of code i.e.

UPC, EAN, QRCode etc... String format = intent. GetStringExtra("SCAN_RESULT_FORMAT"); // Handle successful scan. In this example // I just put the results into the TextView resultsTxt.

SetText(format + "\n" + contents); break; case ACTIVITY_1: // TODO: handle the return of the SecondActivity break; case ACTIVITY_2: // TODO: handle the return of the ThirdActivity break; default: break; } } else if (resultCode == RESULT_CANCELED) { // Handle cancel. If the user presses 'back' // before a code is scanned. ResultsTxt.

SetText("Canceled"); } } Update For a bit more elegant result, you can create your own type of list item data: CustomerListItem. Java public class CustomerListItem { private String label; private Class activity; /** * @param label * @param activity */ public CustomerListItem(String label, Class activity) { super(); this. Label = label; this.

Activity = activity; } /** * @return the label */ public String getLabel() { return label; } /** * @param label the label to set */ public void setLabel(String label) { this. Label = label; } /** * @return the activity */ public Class getActivity() { return activity; } /** * @param activity the activity to set */ public void setActivity(Class activity) { this. Activity = activity; } /* (non-Javadoc) * @see java.lang.

Object#toString() */ @Override public String toString() { return this. Label; } } Using this class you are able to bind an Activity to a list item, so when it will be clicked, you will know which Activity to start Your Customer. Java class will look like: public class Customer extends ListActivity { TextView selection; CustomerListItem items = { new CustomerListItem("Start Trip", StartTripActivity.

Class), new CustomerListItem("Clock in", ClockinActivity. Class), new CustomerListItem("Customer Svc", CustomerSvcActivity. Class), new CustomerListItem("Independent Inspection", IInspectionActivity.

Class), new CustomerListItem("Pick Up", PickUpActivity. Class), new CustomerListItem("Log Out", LogoutActivity. Class)}; @Override public void onCreate(Bundle icicle) { super.

OnCreate(icicle); setContentView(R.layout. Main); setListAdapter(new ArrayAdapter( this, android.R.layout. Simple_list_item_1, items)); selection = (TextView) findViewById(R.id.

Selection); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super. OnListItemClick(l, v, position, id); final Intent intent = new Intent(this, itemsposition.getActivity()); startActivityForResult(intent, position); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super. OnActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { // Perform different actions based on from which activity is // the application returning: switch (requestCode) { case 0: // TODO: handle the return of the StartTripActivity break; case 1: // TODO: handle the return of the ClockinActivity break; case 2: // TODO: handle the return of the CustomerSvcActivity // and so on... break; default: break; } } else if (resultCode == RESULT_CANCELED) { resultsTxt.

SetText("Canceled"); } } }.

You can start different activities based on the position in the ListView of the item you just clicked: private static final int ACTIVITY_0 = 0; private static final int ACTIVITY_1 = 1; private static final int ACTIVITY_2 = 2; @Override protected void onListItemClick(ListView l, View v, int position, long id) { super. OnListItemClick(l, v, position, id); final Intent intent = new Intent(); // Set up different intents based on the item clicked: switch (position) { case ACTIVITY_0: intent. SetClass(this, com.company.merrill.

IntentIntegrator. Class); break; case ACTIVITY_1: intent. SetClass(this, com.company.merrill.SecondActivity.

Class); break; case ACTIVITY_2: intent. SetClass(this, com.company.merrill.ThirdActivity. Class); break; default: break; } // Pass the item position as the requestCode parameter, so on the `Activity`'s // return you can examine it, and determine which activity were you in prior.

StartActivityForResult(intent, position); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super. OnActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { // Perform different actions based on from which activity is // the application returning: switch (requestCode) { case ACTIVITY_0: // contents contains whatever the code was String contents = intent. GetStringExtra("SCAN_RESULT"); // Format contains the type of code i.e.

UPC, EAN, QRCode etc... String format = intent. GetStringExtra("SCAN_RESULT_FORMAT"); // Handle successful scan. In this example // I just put the results into the TextView resultsTxt.

SetText(format + "\n" + contents); break; case ACTIVITY_1: // TODO: handle the return of the SecondActivity break; case ACTIVITY_2: // TODO: handle the return of the ThirdActivity break; default: break; } } else if (resultCode == RESULT_CANCELED) { // Handle cancel. If the user presses 'back' // before a code is scanned. ResultsTxt.

SetText("Canceled"); } } Update For a bit more elegant result, you can create your own type of list item data: CustomerListItem. Java public class CustomerListItem { private String label; private Class activity; /** * @param label * @param activity */ public CustomerListItem(String label, Class activity) { super(); this. Label = label; this.

Activity = activity; } /** * @return the label */ public String getLabel() { return label; } /** * @param label the label to set */ public void setLabel(String label) { this. Label = label; } /** * @return the activity */ public Class getActivity() { return activity; } /** * @param activity the activity to set */ public void setActivity(Class activity) { this. Activity = activity; } /* (non-Javadoc) * @see java.lang.

Object#toString() */ @Override public String toString() { return this. Label; } } Using this class you are able to bind an Activity to a list item, so when it will be clicked, you will know which Activity to start. Your Customer.

Java class will look like: public class Customer extends ListActivity { TextView selection; CustomerListItem items = { new CustomerListItem("Start Trip", StartTripActivity. Class), new CustomerListItem("Clock in", ClockinActivity. Class), new CustomerListItem("Customer Svc", CustomerSvcActivity.

Class), new CustomerListItem("Independent Inspection", IInspectionActivity. Class), new CustomerListItem("Pick Up", PickUpActivity. Class), new CustomerListItem("Log Out", LogoutActivity.

Class)}; @Override public void onCreate(Bundle icicle) { super. OnCreate(icicle); setContentView(R.layout. Main); setListAdapter(new ArrayAdapter( this, android.R.layout.

Simple_list_item_1, items)); selection = (TextView) findViewById(R.id. Selection); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super. OnListItemClick(l, v, position, id); final Intent intent = new Intent(this, itemsposition.getActivity()); startActivityForResult(intent, position); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.

OnActivityResult(requestCode, resultCode, intent); if (resultCode == RESULT_OK) { // Perform different actions based on from which activity is // the application returning: switch (requestCode) { case 0: // TODO: handle the return of the StartTripActivity break; case 1: // TODO: handle the return of the ClockinActivity break; case 2: // TODO: handle the return of the CustomerSvcActivity // and so on... break; default: break; } } else if (resultCode == RESULT_CANCELED) { resultsTxt. SetText("Canceled"); } } }.

Thank you very much. I would like to ask you as for activity 2 do you have a sample for time/date as the user click on "clock in" in the listview it will send the current/date to the database right away. As for activity 3 I would like to know sample how to put in the hardcode number for "log out" user won't be able to log out but their manager would have to approve and logout themselves.

– merrill May 16 at 15:04 I would love to help you out, though I suggest you to open a new thread for these questions, so to keep StakOverflow clean, with answers focusing on a specific issue. This way others might find helpful them. Thank you!

– rekaszeru May 16 at 15:12 okay sounds like a good idea thanks again – merrill May 16 at 15:18 by the way the first 3 lines do I put that before or after textview selection string? – merrill May 16 at 15:35 As I am playing around with the coding Im still confuse on how to connect which activity(intent) to which item of the list. For example, how can I link customer svc, independent inspection, and pick up items to the activity_0 (for barcode scanning) and ensure the data is sent under that specific item?

– merrill May 16 at 15:44.

Return you can examine it, and determine which activity were you in prior. String contents = intent. // Format contains the type of code i.e.

String format = intent. // Handle successful scan. // before a code is scanned.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions