Listview and data - android

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

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

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

ListView with a dynamic intents that change a TextView and WebView on each call

Activity that is sending the putExtra()
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent();
// Set up different intents based on the item clicked:
switch (position)
{
case ACTIVITY_0:
intent.putExtra("Value1", "Display this text!");
intent.setClass(this, com.a.someclass.class);
}
startActivityForResult(intent, position);
}
Activity receiving the putExtra()
#Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.information);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
String value1 = extras.getString("Value1");
if (value1 != null) {
informationTitle = (TextView)findViewById(R.id.informationTitle);
informationText = (WebView)findViewById(R.id.informationText);
informationTitle.setText(value1);
}
Original Message:
i have been searching everywhere for a good tutorial on this, i have posted my code online so people can look at it but have not found the help i needed.
I am new to this and what i am basically trying to do is just have a list of items which are all linked to one class that has a dynamic TextView that will be used for the title and a WebView for content, And so basically when a item is clicked on the list it will open up the new activity/intent and it will also pass arguments to change the TextView and WebView accordingly.
I know how to open a new activity by making a new class for each item on the list but i am pretty sure there is an easier way where i can reuse one class and just keep changing the TextView and WebView. The reason i say this is because i have 15 items on my list, but that will expand overtime so i dont want to be making 50-60 different classes to open up each new item.
If some could point me to the right tutorial or give me some insight on here i will really really appreciate it!
Thank you
To accomplish that, you would, instead of using a different Intent for each list item, call the same Activity with the same Intent, but pass extras along with it.
Let's say, for instance, that you want to pass a different String depending on which list item is clicked. You would want to
myIntent.putExtra(String key, String value);
startActivity(myIntent);
The Activity that you start with this Intent will then be able to grab these extras in its onCreate() using
Bundle extras = getIntent().getExtras();
and access the extras you put in the Intent using the methods outlined here. This way you can use a single Activity for all list items but have the Activity display different information based on the extra values.
I'll give you a link where you can get the best tutorial for that...
http://www.vogella.de/articles/Android/article.html

Passing listView row positions through Intents to another class

I'm wondering how to pass a row position(pos) value from, say, a CHOICE_MODE_SINGLE list Activity(A) to another Activity(B) using Intents? (I want to change ActivityB to show another list depending on what row in ActivityA is clicked). Here's my code:
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(
new android.widget.AdapterView.OnItemClickListener(){
#Override
public final void onItemClick(AdapterView<?> listView, View cell, int position, long id) {
Intent Courses = new Intent(this, ExpandableList.class);
Courses.putExtra(//I'm not sure what to put in here//)
});
private static final String[] GENRES = new String[] {"Barre","Buffumville","Hodges","Newton Hill"};
}
THANKS :)
Courses.putExtra("position",position);
Then to get the position in the next activity:
getIntent.getExtras().getInt("position");
You can pass the position through the intent putExtra.
please see below Code.
Intent Courses = new Intent(this, ExpandableList.class);
Courses.putExtra("position",position)
startActivity(Courses);
Now you can get this value in another activity like this.
getIntent.getExtras().getInt("position");
It will return the integer position you passed from the first activity.

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