How to pass the selectedListItem's object to another activity? - android

I have designed an activity in which there is a listView. I want that on click of the list item a new Activity should start and it should contain detail data related to the ListItem clicked.Please help me out in doing this.Thanks in advance.

While starting an activity use putExtra to send data to another activity
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("listitemselected", listitemvalue);
startActivity(i);
In SecondActivity.java use getStringExtra to get the values.
Intent i = getIntent();
String listItem = i.getStringExtra("listitemselected");

In the action handler start the new activity with putextra you can add some data
Intent intent = new Intent(ShowLocation.this, ListLocationActions.class);
intent.putExtra("infoName1", myData);
intent.putExtra("infoName2", MyData2);
startActivity(intent);
in the new activity:
String myData = getIntent().getStringExtra("infoName1");
String myData2 = getIntent().getStringExtra("infoName2");

In your onItemClick. Try something like this;
you can send id of the item if your are using Sqlite database. or your can send the Object (If Serializable) from the Object array at specific postion. The postion will be the index of the selected List Item as well as index of the Object in that specific array, Or it may be a String.
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
{
Intent status = new Intent(CurrentActivity.this,NextActivity.class );
status.putExtra("_ID", id);
//Or
status.putExtra("OBJECT", mArray[position]);
startActivity(status);
}

String temp = listView(v.getId()).getText().toString();
Intent i1 = new Intent(currentAct.this,nextAct.class);
i1.putExtra("listitem",temp);
startActivity(i1);
listView() ==>It is having all the listItem that you are displayed on listItem

Related

how to send intent.putExtra("getid",id) without displaying it in listview custom adapter?

i am receiving JSON string which contains id, and other string, now i want to pass id to another activity without displaying id in listview, id should be pass when i click specific icon then that icon's id should be pass from activity(A) to another actvity(B)
here is some my code just took relevant part
id = jsonObject.getString("id");
platformno = jsonObject.getString("platform");
listitem.add(new Latest_list(icon,name,date,id,platformno));
here above i no want to display id in listview
String id=Integer.toString(listitem.get(arg2).getNews_id());
Intent intent = new Intent(getApplicationContext(), Webview_news.class);
intent.putExtra("getid",id);
Toast.makeText(LatestNews.this, "platformno="+strPlatform+", id="+id, Toast.LENGTH_LONG).show();
startActivity(intent);
Your question is unclear. Depending on what type of value, here i have an example with String in an ArrayList<String> arrList.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked on an item, pass it to another activity
Intent i = new Intent(this, AnotherActivity.class);
i.putExtra("key", arrList.get(position));
startActivity(i);
}
});

show the listItem related information from database on ListItemclick on new Activity [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 6 years ago.
I am making one Android Application and I am Stuck at One Point and need help for it.
I have the List Of Doctors. each list row contains only DoctorName and Speciality.
I want to show each Doctors Information Like his Address,Phone number etc.on its ItemClick in new Layout.
How to Pass this two Parameters(DocName,Speciality) through onItemClickListner() according to Position using Intent to Open my New Activity(DoctorInfo)
How to retrive that text value of DoctorName of each row which i Select using setOnItemClickListner()
how to resolve it???
this is my listner method
lstDoctorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String DocName = view.findViewById(R.id.txtDoctorName).;// I want Solution for this Line
Intent i = new Intent(DoctorsActivity.this,DoctorInfo.class);
i.putExtra("DocName",DocName);
startActivity(i);
}
});
How I will get DoctorName to Pass my New Activity???
in your setOnItemClickListener()
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Doctorname", DocName);
startActivity(intent);
get intent in your second activity:
Intent intent = getIntent();
String dn = intent.getStringExtra("Doctorname");
This is how you pass data from one activity to another activity :
ListView listView = getListView();
// listening to single list item on click
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
HERE YOU GOT POSITION FOR PARTICULAR ITEM
// Launching new Activity on selecting single List Item
Intent intent = new Intent(getBaseContext(), ActivityToLaunch.class);
intent.putExtra("DOCTOR_NAME", here get the doctor name from your list using the position arg);
startActivity(intent);
}
});`
And in ActivityToLaunch
Intent intent = getIntent();
String getDoctorName = intent.getStringExtra("DOCTOR_NAME");

How do i get the rowID of a list item in another activity class?

In my main activity I have a list view of database items, when i long press on an item in a list it opens into a new activity that allows you to edit the item clicked.
The problem I'm having is that i'm not quite sure how to get the information of the rowID/primary key to the other activity java class so that i can start manipulating in that class e.g. set the initial values as the current row information and then being able to apply an update to it.
Incase this is of use to helping you help me here is what i have to take me to my new activity
dialog.setNegativeButton(R.string.dialogEdit,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
long id = idInDB;
Intent intent = new Intent(MainActivity.this, edit_expense.class);
startActivity(intent);
}
});
onClick send the row id using putExtra
intent.putExtra("rowid", rowid);
On second activity get the extra string in onCreate()
String rowid = getIntent().getExtras().getString("rowid");
I'm assuming id is the id you want to pass to the other activity, use Intent extras to pass it.
intent.putExtra("key", id);
and to get the value in the other activity:
Intent intent = getIntent();
long id = intent.getLongExtra("key", -1);
Intent reference: https://developer.android.com/reference/android/content/Intent.html
Hope fully in your list row you have a textView that contains the ID.
So when you load the listView, every row will add its own unique ID in the textView. Now when you click/hold the any specific listItem, it will trigger the click event and will retrieve the ID into the String rowID. Now you can pass this ID using intent.
list.setOnLongClickListener(new AdapterView.OnLongClickListener() {
#Override
public void onLongClick(AdapterView<?> adapterView, View view, int i, long l) {
String rowID =((TextView) view.findViewById(R.id.rowID)).getText().toString();
Intent nextActivity = new Intent();
nextActivity.putExtra("id",rowID);
startActivity();
}

Sending Intent from Listview onItemClickListener error

Just a quick question, I'm guessing I've missed something really easy and obvious. The following code is called when a listview item is accessed, and it passes the id to the next activity. I'm getting a runtime error, log cat is showing me this
error:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tim.apps.list/com.tim.apps.list.Challengeview}: java.lang.NumberFormatException: Invalid int: "null"
Heres the code for my first activity:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String index = (String)adapter.getItemAtPosition(position);
//Intent myIntent = new Intent(ChallengesList.this, Challengeview.class);
//ChallengesList.this.startActivity(myIntent);
Intent i = new Intent(getApplicationContext(), Challengeview.class);
i.putExtra(index, position);
startActivity(i);
}
});
And in my second activity..
Bundle extras = getIntent().getExtras();
String value = extras.getString("index");
//Convert the passed id to a integer
int intvalue = Integer.parseInt(value);
Thanks heaps for any help you guys can give!
you are giving the item data as the key and you are retrieving the data with key as "index"
Change
Intent i = new Intent(getApplicationContext(), Challengeview.class);
i.putExtra(index, position);
to
Intent i = new Intent(getApplicationContext(), Challengeview.class);
i.putExtra("index", position);
While Retrieving , give the same key "index" to get the data..
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("index");
// Convert the passed id to a integer
int intvalue = Integer.parseInt(value);
}
You are doing wrong on both end (Sending/receiving). you can use this as i.putExtra(String,String).In your case if u want to receive String then u need to send string only as i am adding "" to position.
Intent i = new Intent(getApplicationContext(), Challengeview.class);
i.putExtra("index", ""+position);
startActivity(i);
and at receiving end u have to do like this.
String value =getIntent().getStringExtras("index");
The problem is you are using different keys in each activity:
String index = (String) adapter.getItemAtPosition(position);
will return a string representation of your item.
So what you are putting into your intent is: ItemString -> position
What you want in your intent is: "index" -> position

passing data from list view to another activity

i want to create an activity, in which, i would like to have a listview, there can be about 20-30 items in a list view,on tapping any particular value in listview, it should move to another activity, with the data of list view.
Just wanna pass data from listview to another activity.
Suggestion plz
Regards
Implement ListView's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked.
Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.
Now use this code:
Intent anotherActivityIntent = new Intent(this, AnotherActivity.class);
anotherActivityIntent.putExtra("my.package.dataToPass",dataFromClickedRow);
startActivity(anotherActivityIntent);
and when the anotherActivityIntent starts the AnotherActivity class, use following code to access the value that you had passed:
Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("my.package.dataToPass");
Now you have your data in myVal variable. you can use any other data type, whichever you like.
You can pass data from one activity to another activity:
see this link
and to get data for ListView you have to first implement getListView.setOnItemClickListener(),
and have to get position of item in ListView and use the index to get data form your adapter from where you are binding data to ListView.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object obj = this.getListAdapter().getItem(position);
String value= obj.toString();
Intent intent= new Intent(CurrrentClass.this,NextClass.class);
intent.putExtra("value", value);
startActivity(intent);
}
Hope this will help you.
String selectedItem =arrayAdapter.getItem(position);
Intent intent = new Intent(getApplicationContext(),
Your_Second_Activity.class);
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
Second_Activity.Class
Bundle bundle = getIntent().getExtras();
String yourItem = bundle.getString("selectedItem");
Now! your selected item is inside in the yourItem Variable...
Use Bundle in onClickListner of the listview .
Bundle will pass data from one activity to Next.
There are two ways:
Pass it into Intent
intent.putExtra("jobNo", item.jobNo);
Use Application scope
((MyApplication) getApplication()).setJobNo(item.jobNo);

Categories

Resources