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.
Related
This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 6 years ago.
I am making one Android Application and I am Stuck at One Point and need help for it.
I have the List Of Doctors. each list row contains only DoctorName and Speciality.
I want to show each Doctors Information Like his Address,Phone number etc.on its ItemClick in new Layout.
How to Pass this two Parameters(DocName,Speciality) through onItemClickListner() according to Position using Intent to Open my New Activity(DoctorInfo)
How to retrive that text value of DoctorName of each row which i Select using setOnItemClickListner()
how to resolve it???
this is my listner method
lstDoctorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String DocName = view.findViewById(R.id.txtDoctorName).;// I want Solution for this Line
Intent i = new Intent(DoctorsActivity.this,DoctorInfo.class);
i.putExtra("DocName",DocName);
startActivity(i);
}
});
How I will get DoctorName to Pass my New Activity???
in your setOnItemClickListener()
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Doctorname", DocName);
startActivity(intent);
get intent in your second activity:
Intent intent = getIntent();
String dn = intent.getStringExtra("Doctorname");
This is how you pass data from one activity to another activity :
ListView listView = getListView();
// listening to single list item on click
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
HERE YOU GOT POSITION FOR PARTICULAR ITEM
// Launching new Activity on selecting single List Item
Intent intent = new Intent(getBaseContext(), ActivityToLaunch.class);
intent.putExtra("DOCTOR_NAME", here get the doctor name from your list using the position arg);
startActivity(intent);
}
});`
And in ActivityToLaunch
Intent intent = getIntent();
String getDoctorName = intent.getStringExtra("DOCTOR_NAME");
In my activity I have two listviews with some data. On first listview selected row will be highlighted on click and on click on 2nd listview new activity starts.
I want to send the highlighted row data of first listview and clicked row of 2nd listview data on next activity. How can Iachieve that?
supposing your lists are named list1 & list2, add to your list2 onItemClickListener that should looks something like this :
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
String highlightedItem = (String)list1.getSelectedItem();
String clickedItem = (String)list2.getItemAtPosition(position);
Intent intent = new Intent(FirstAcivity.this, SecondActivity.class);
intent.putExtra("highlightedItem", highlightedItem);
intent.putExtra("clickedItem", clickedItem);
startActivity(intent);
}
and then in your Second Activity you can receive the items like this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
if (intent != null){
String highlightedItem = intent.getStringExtra("highlightedItem");
String clickedItem = intent.getStringExtra("clickedItem");
}
}
I have assumed your list items are of type String but you can apply the same logic for any other type.
In case your want to send object not just primitives you need to make your objects implements Serializable or Parcelable interface.
i am displaying only three details from my database in 1 row of the listview after the user clicks on this list item all the details should be made visible in another activity in a list view.i tried but m getting a blank activity to open instead of a list..
ListViewDetails.java
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
int appno=cursor.getInt(cursor.getColumnIndexOrThrow("appln_no"));
Intent objintent=new Intent(getApplicationContext(),DisplayDetails.class);
objintent.putExtra("countryCode", countryCode);
startActivity(objintent);
}
});
here m passing an appno parameter to the next intent so that details related to this appno are displayed in DisplayDetails.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listdisplay);
Intent intentobj=getIntent();
int appno=intentobj.getIntExtra("appln_no", 0);
WayDataBase way=new WayDataBase(getApplicationContext());
ArrayList<String> listvalues=way.getListDetails(appno);
if(listvalues.size()!=0)
{
ListView lv=getListView();
ListAdapter adapter=new ArrayAdapter<String>(DisplayDetails.this, R.layout.view_animal_entry, R.id.animalName, listvalues);
lv.setAdapter(adapter);
}
}
but the screen is just balnk..
whats the issue??? please help! thanks!
Shiv,
You have fetched the values in variable "appno" but set values from variable "countryCode" instead of "appno".
In your DisplayDetails.java, you are trying to fetch it from the the variable "appln_no" which is incorrect.
If i look at your code then it seems that you want to pass appno value to another activity
so should keep it like this:
ListViewDetails.java
objintent.putExtra("countryCode", appno);
DisplayDetails.java
int appno=intentobj.getIntExtra("appln_no", "countryCode");
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"));
I am working on android applications. In my project I need to create Listview. My layout i.e info.xml contains topbar with one image view, footer with other imageview. Also in the center of the layout I kept an imageview and on that I have created the Listview. Also I have created row.xml for the textview to display listitems. Now when I click on each listitem a new intent should be called...i.e when I click on 1st listitem page1 should open. Similarly if I click on 2nd listitem page2 should open and so on. So how could I do that. I am struggling for this since 3 days but didnt find any correct solution. Please hgelp me regarding this.
My Code:
public class Information extends Activity
{
private String[] Countries;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
Countries = getResources().getStringArray(R.array.countries);
ListView list = (ListView)findViewById(R.id.listnew);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row, Countries);
list.setAdapter(adapter);
registerForContextMenu(list); }
}
All you have to add is
list.setOnItemClickListener(this);
Then you let your class implement the OnItemClickListener interface and create this method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
Intent firstIntent = new Intent(this, MyClass.class);
startActivity(firstIntentIntent);
break;
case 1:
Intent secondIntent = new Intent(this, MySecondClass.class);
startActivity(secondIntentIntent);
break;
[... etc ...]
}
}
In this case, if the first item is clicked, it will launch the MyClass Activity, if the second item is clicked, the MySecondClass Activity will be launched, etc.
Yes, it is a bit tedious but it is the best way.