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!
Related
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");
I've an issue with onItemclicklistener.
I'm getting real time data from firebase
What I want is, getting real time orders and when I click any specific order, it will go to another activity where specific details of that specific order should be shown.
How I get this?
Switch Statements will not work in this scenario.
How I get some id that will match in database, so that I can get that particular details.
I tried this method
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String name=(String)adapterView.getItemAtPosition(i);
Intent i=new Intent(order.this,details.class);
i.putExtra("CATEGORY", name);
startActivity(i);
}
});
and in details class
Intent i = new Intent();
String name=i.getStringExtra("CATEGORY");
databaseReference=FirebaseDatabase.getInstance().getReference(Constans.CATEGORIES).child(name);
But it is crashing.
Assuming that the ListView contains the order names and the item on which the user click is the name of the order, to get that name of the order you need to use this code inside onItemClick() method:
String orderName = (String) adapterView.getItemAtPosition(position);
Hope it helps.
You can pass the item_id with firebase notification and store it in the listview adapter.
Later you can pass this id to the new activity by putting in extras.
I am kind of new to android studio, I have an array of strings for my list and when i click on an item from that list it takes me to the correct page. However, there are 100 items on the list and it's not logical to create 100 java files for all of them to set the text on that page.
My question is: is there a way to set the text to the correct item of list just after or before clicking that item?
Assuming you have in your FirstActivity a ListView please use the following code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String item = (String) adapterView.getItemAtPosition(position);
Intent intent = new Intent(YourActivity.this, SecondActivity.class);
intent.putExtra("item", item);
startActivity(intent);
}
});
To get the item in your SecondActivity please use this code:
String item = (String) getIntent().getExtras().get("item");
yourTextView.setText(item);
Hope it helps.
How to display the details of any of the items clicked in a listview in another activity?
I referred to this. But unfortunately couldn't understand as to how to go about in displaying the details. Could anyone please help me on this?
you can use putExtra of intent and can pass any value or any serialized class object
yourActivity.java
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Intent intent = new Intent(yourActivity.this,item.class);
intent.putExtra("item",parent.getSelectedItem().toString);
startActivity(intent)
}
item.java
Intent intent = getIntent();
String itemText = intent.getStringExtra("item");
textView.settext(itemText);
hope u got the point..:
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..