ArchersTH177
ArchersTH177

Reputation: 5

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.

Upvotes: 0

Views: 60

Answers (2)

ArchersTH177
ArchersTH177

Reputation: 5

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!

Upvotes: 0

Vaibhav pandey
Vaibhav pandey

Reputation: 109

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

Upvotes: 1

Related Questions