Passing variables to another class via Intent and Adapter - android

I'm trying to pass a bunch of variables to another Activity,
but in the receiving Activity it only got access to the first element.
My listView1 contains 3 Elements: a 2 TextViews and 1 ImageView...
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(parent.getContext(),
DisplayListEntry.class);
intent.putExtra("TOPIC", listView1.getAdapter().getItem(position).toString());
startActivityForResult(intent, 0);
Receiving Activity:
Intent intent = getIntent();
String s1 = intent.getStringExtra("TOPIC");
And i want to access them via the Intent...
Could somebody please be so kind and tell me how its done? :/
Thanks in advance!

Ok, you have three views and want to these data to another activity.
The first problem is you are starting another activity inside the onItemClick and at this point, you are using the item position to get current value. That is ok, but you are only getting a value for one specific view, whose is being clicked.
listView1.getAdapter().getItem(position).toString();
If you really want to pass the three values my suggestion is:
Create a field on your class for each view
Ex:
Textview t1, t2;
ImageView i1;
And onClick event you will use it to put inside the intent like that:
onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(parent.getContext(), DisplayListEntry.class);
intent.putExtra("key1",t1.getText());
intent.putExtra("key2",t2.getText());
intent.putExtra("key3",i2.get***());
startActivityForResult(intent, 0);
In you another activity you can access those values:
Intent intent = getIntent();
String s1 = intent.getStringExtra("key1");
String s2 = intent.getStringExtra("key2");
*** = intent.get****("key3");
You should change *** for the data type you want to pass.

you can Add PutExtra as many as you want with different key and position of your item and in another class get it using key.

First you need to correct in this in next activity
Bundle b = getIntent().getExtras();
if( b != null ){
String s1 = intent.getStringExtra("TOPIC");
}

Intent intent = getIntent();
String s1 = intent.getStringExtra("TOPIC");
here you s1 declare as string..
it will not work because there are u pass arraylist.....
declare arraylist variable and try to implement it..

Related

Issues with working with arraylists, listviews and intents

I'm working on android studio, it's an app that shows data of some vehicles in a list view, the data is stored on an arraylist and is of a class named Vehicle.
On the listview the idea is that is shows the licence plate of the car, color, etc. It works fine, what I need to do is that when you click on an item it sends you to another intent where you can edit the data of the car, here I ran into my problem, how do I tell that intent whose data is it editing?
listaVehiculos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent editar = new Intent(Lista.this,Editar.class);
//editar.putExtra("Placa",listaVehiculos.getItemAtPosition(i).toString());
editar.putExtra("Codigo",listaVehiculos.getItemIdAtPosition(i));
editar.putParcelableArrayListExtra("Vehiculos",Vehiculos);
startActivity(editar);
}
});
That's the code I have on the view where the listview is, listavehiculos being the listview itself and Vehiculos being the arraylist, what I need is to also send to the other activity is which listview item I selected so it'd know which element of the arraylist to edit.
On the other activity I have this
editar = (Button)findViewById(R.id.editar);
Bundle extras = getIntent().getExtras();
final ArrayList<Vehiculo> Vehiculos3 = extras.getParcelableArrayList("Vehiculos");
dos= extras.getString("Placa");
I've been messing around with different putExtra values but I don't know how to return me a position, or anything to tell me which object I selected.
Look Bundle
When you use putExtra , you did not use Bundle .So you just intent.getParcelableArrayListExtra("Vehiculos"); and intent.getStringExtra("Placa"); to get value .
And make sure that ArrayList<? extends Parcelable> value .
Try
Intent intent = getIntent();
final ArrayList<Vehiculo> Vehiculos3 = intent.getParcelableArrayListExtra("Vehiculos");
dos= intent.getStringExtra("Placa");
You don't need to use a bundle
Change ,
Bundle extras = getIntent().getExtras();
final ArrayList<Vehiculo> Vehiculos3 = extras.getParcelableArrayList("Vehiculos");
dos= extras.getString("Placa");
to
Intent i = getIntent();
final ArrayList<Vehiculo> Vehiculos3 = i.getParcelableArrayListExtra("Vehiculos");
dos= i.getStringExtra("Placa");

Listview and data

I have a ListView where I have some elements. Every item has one ListViewand two TextBoxes. Here is my question: When I clock on element from the list a new activity starts, where I have one ListView and two TextBoxes. How I can do this if I click first element in the new activity in ListView will be ListViewfrom this item and in TextBoxeswill be data from TextBoxes from the list.
You can pass extras to the Intent you use when starting the new Activity.
Let's say your current activity is MyActivity, and the one you want to start by clicking on a list item is MyNewActivity; Then in your MyActivity class, inside the list item click listener should be modified as:
Intent intent = new Intent(MyActivity.this, MyNewActivity.class);
intent.putExtra("my.picture.id", images[itemPosition]);
intent.putExtra("my.header.id", headers[itemPosition]);
intent.putExtra("my.text.id", texts[itemPosition]);
startActivity(intent);
and in your MyNewActivity class' onCreate method you are able to retrieve the passed extras, and fill the proper fields with the correct values:
final Intent intent = getIntent();
final int pictureId = intent.getIntExtra("my.picture.id", 0);
final int headerId = intent.getIntExtra("my.header.id", 0);
final int textId = intent.getIntExtra("my.text.id", 0);
((ImageView)findViewById(R.id.my_image)).setImageResource(pictureId);
((TextView)findViewById(R.id.my_header)).setText(headerId);
((TextView)findViewById(R.id.my_text)).setImageResource(textId);
the images, headers and texts arrays -I suppose- contain the resource ids for the images and strings you want to display. They are probably accessible via the data of your current item's renderer.
I would go about it a bit differently (in retrieving the information at least) than rekaszeru.
In your first activity you would use onListItemClick and put the information into the extras bundle to be passed with the intent to start the second activity. In this method, you use the view passed in to retrieve the info, so it doesn't matter what kind of adapter you are using and the position in the adapter doesn't matter.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent myIntent = new Intent(FirstClass.this, SecondClass.class);
myIntent.putExtra("ImageRef", v.findViewById(R.id.imageview)).getTag());
myIntent.putExtra("Text1", v.findViewById(R.id.TextView1).getText().toString());
myIntent.putExtra("Text2", v.findViewById(R.id.TextView2).getText().toString());
FirstClass.this.startActivity(myIntent);
}
Then in the second activity retrieve the info to be used:
private TextView NewTextView1;
private TextView NewTextView2;
private ImageViewView NewImageView;
Bundle extras = getIntent().getExtras();
NewTextView1 = (TextView)findViewBYId(R.id.newtextview1).setText(extras.getString("Text1"));
NewTextView2 = (TextView)findViewBYId(R.id.newtextview2).setText(extras.getString("Text2"));
NewImageView = (ImageView)findViewBYId(R.id.newimageview).setImageResource(extras.getInt("ImageRef"));

Passing and displaying the ArrayList result from Intent

I'm trying to pass arraylist object values from one activity to another, however after completing the process the result is blank screen. What am i doing wrong and how can I fix this?
Putting the values into the intent:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent in = new Intent(getApplicationContext(),
Mahad.class);
//
in.putExtra("identifier",identifier);
in.putExtra("price",price);
in.putExtra("bedrooms",bedrooms);
in.putExtra("address",address);
in.putExtra("property",propType);
//in.putExtra("Property url : " + image, 0);
startActivity(in);
}
Displaying the values from my other activity:
Intent in = getIntent();
identifier = in.getStringArrayListExtra("identifier");
TextView lbli = (TextView) findViewById(R.id.identifier_i);
lbli.setTag(identifier);
I also pass an image in the same way.
Thanks in advance
useIntent.putStringArrayListExtra("identifier") and access the data as you have done
Let suppose you have a list like this:
List<Identifires> identifire = new List<Identifires>();
make Identifires class serializable. and use in.getSerializableExtra to get value. however you can use parcelling too.
TextView lbli = (TextView) findViewById(R.id.identifier_i);
lbli.setTag(identifier);
i think u are doing a mistake. you cannot use "setTag" to settext and also you cannot set an array list to a textview only one item of the array list can be set like
lbli.setText(identifier[1]);

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

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

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