Passing and displaying the ArrayList result from Intent - android

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]);

Related

Trying to pass values from listview to another activity

this question probably has been asked quite a lot but even after all the time i spent searching for a solution i couldnt make it work.
First i found this code which was very useful
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Product selectedProduct = arrayList.get(position);
Intent intent = new Intent(getApplicationContext(), NuevaActivity.class);
intent.putExtra("nombre", selectedProduct.getName());
intent.putExtra("contenido", selectedProduct.getContent());
intent.putExtra("extra1", selectedProduct.getExtra());
startActivity(intent);
}
});
In which Product selectedProduct = arraylist.get(position); didnt work as it asked for an object from my arraylist
So i changed it to Object whatevername = myarraylist(position);
Next i figured out how the putExtra worked and now i have a doubt about it. So i need to get the Strings from the Array(As taken from this example) with 3 methods called getName() getContent() and get Extra() ?
The problem is the following, How do i create this methods ?
In my actual list view i get my data from a PHP and put the JSONdata in 3 different arrays, should i use this for the 3 methods mentioned above?(Only this time do one for each array)
And if so do i need to write 3 Object whatevername = eachdifferentarray(position); in my code?
Thanks in advance for the help, this is new for me so im trying my best to understand it.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Product selectedProduct = arrayList.get(position);
Common currentClick = selectedProduct;
Intent intent = new Intent(getApplicationContext(), NuevaActivity.class);
startActivity(intent);
}
});
Here Common is an class which stores the current click item and you can use it on next class
Eg. Common currentClick.getYoueyc();
Solved it
1-Changed Product to Object to get position of my array
2-Had to pass that position to a string
3-Finally pass that string to a put extra
If you want an example it would be something like this:
Object ArrayPosition = nameofarray.get(i);
String ValuePosition = ArrayPosition.toString();
intent.putExtra("NameExample",ValuePosition);
If you wanted to pass more than one you just have to do one for each
Object ArrayPosition1 = nameofarray1.get(i);
Object ArrayPosition2 = nameofarray2.get(i);
String ValuePosition1 = ArrayPosition1.toString();
String ValuePosition2 = ArrayPosition2.toString();
intent.putExtra("NameExample1",ValuePosition1);
intent.putExtra("NameExample2",ValuePosition2);
and to pass the putExtra to another activity
Intent intent = getIntent();
String NameExample = intent.getStringExtra("NameExample");
//*Do whatever logic you need here*
Hope this helped someone!

Android ListView Populated from XML won't send data to new activity

I have an array located in my strings.xml file that looks like the following.
<string-array name="array123">
<item>test1</item>
<item>test2</item>
<item>test3</item>
<item>test4</item>
<item>test5</item>
</string-array>
My listview is properly displayed on Activity 1, here is my onItemClick code
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent activityIntent = new Intent(this, Activity2.class);
String selectedItem = adapter.getSelectedItem().toString();
activityIntent.putExtra("my.package.dataToPass", selectedItem);
startActivity(activityIntent);
As soon as I tap an item from the list the screen just goes black and the application crashes. Here is my code for Activity 2
Bundle recdData = getIntent().getExtras();
String myVal = recdData.getString("my.package.dataToPass");
TextView word = (TextView) findViewById(R.id.word);
word.setText(myVal);
I'm not sure why this isn't working. I'm just trying to pass the string that was selected to show as text on the 2nd activity. I've been searching for a day and a half now.
I did try
activityIntent.putExtra("my.package.dataToPass", id);
but this just seemed to pass an empty screen.
Any help would be much appreciated. Obviously I'm not passing the right data in the intent.
As I can see, you are implementing the Click listener (onItemClick) rather than Select listener(onItemSelected). By default when you click on a ListView item it doesn't change its state to "selected"
Plz use adapter.getItem(position) instead of adapter.getSelectedItem()
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
String selectedItem = adapter.getItem(position).toString();
}

Passing variables to another class via Intent and Adapter

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..

an int value passed trhought intent return 0 on new activity

on the caller activity I have:
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class);
myIntent.putExtra("categoriaId", String.valueOf(id));
MainActivity.this.startActivity(myIntent);
// Toast.makeText(this, String.valueOf(id), Toast.LENGTH_LONG).show();
}
the toast return the right value of the id of the item clicked on the list.
on the reciver activity i have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maindomanda);
int category = getIntent().getExtras().getInt("categoriaId");
TextView text = (TextView) findViewById(R.id.domanda);
text.setText(String.valueOf(category));
}
a very straight forward... the text field returns always 0... what i'm doing wrong? is the String.ValueOf() somehow deprecated? or is better to use something else...
and another side question... i pass the long id like i know what i'm doing, it's quite false... is it the so called "_ID" field? i get those values from a dbtable, is that that table ID?
Change your code as:
String strcategory = getIntent().getExtras().getString("categoriaId");
because you are passing String from onListItemClick to sondaggioActivity Activity and trying to receive as Int
NOTE :
Second and important point is id is long and you are trying to parsing it to Int which is also not valid .
see this post: How can I convert a long to int in Java?
Solution is just send id as long from onListItemClick as
myIntent.putExtra("categoriaId",id);
and receive it as in sondaggioActivity Activity :
long category = getIntent().getExtras().getLong("categoriaId");
You are getting a int getInt("categoriaId"), you passed in a string putExtra("categoriaId", String.valueOf(id));
String.valueOf(); returns a STring representation of the int.
pass categoriaId as long myIntent.putExtra("categoriaId", id);

Get specific item property on onListItemClick() using custom ArrayAdapter<>

I have a ListActivity that displays a list of search results I grab from a webservice off the internet. I parse the XML I receive into an ArrayList<MyObjects> which I then bind to a ListView using my own adapter (as in MyObjectAdapter extends ArrayAdapter<MyObject>).
So then I want the user to be able to click on one of the items in the list. Each item has an identifier which will then be put into an intent and sent to a new activity (which then triggers a new webservice request based on this, downloads the rest of the data). But I don't know how to get this one property of the MyObject that was selected.
Here's the onListItemClick() method as it stands:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String myObjectId;
// I want to get this out of the selected MyObject class here
Intent i = new Intent(this, ViewObject.class);
i.putExtra("identifier_key", myObjectId);
startActivityForResult(i, ACTIVITY_VIEW);
}
If you are using ArrayAdapter you must initialize that adapter with an array, right? So, the better you can do is to use the postition that you get from onListItemClick and take the object from the original array.
For instance:
// somewhere you have this
ArrayList<MyObjects> theItemsYouUsedToInitializeTheArrayAdapter;
// and inside onListItemClick....
String myObjectId = theItemsYouUsedToInitializeTheArrayAdapter.get(position).getObjectId();
try this getIntent().getExtras().getString(key);

Categories

Resources