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..:
Related
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.
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");
I have a listview and when it clicks on a certain option I want it to create a new intent. Like a menu item. This is my first time working with list views, so I'm probably just missing the right way to do this, but is there an easy fix?
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.mainlist);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
TextView textView = (TextView) viewClicked;
Toast.makeText(main.this, position, Toast.LENGTH_SHORT).show();
if(textView.getText().toString() == "my Profile"){
Intent intent = new Intent(this.getApplicationContext(), myProfile.class);
startActivity(intent);
}
}
});
cannot resolve method 'getapplicationcontext'
You can't use this.getApplicationContext() since you're inside a new AdapterView.OnItemClickListener() and there,this doesn't have that method.
You should create a class field containing the application context and use it or use viewClicked.getContext() as suggested by #codeMagic.
Also, as you might have been read in the comments, you should use .equals for string comparison. The == operator is not enough.
Try -
Intent intent = new Intent(MainActivity.this, myProfile.class);
startActivity(intent);
Where MainActivity is he activity where you implemented onItemClick().
You should replace it by your activity name.
I want to go from one fragment to another fragment via click on a widgets.
I used this code in the main activity and used onclick in wedget property:
public void goOther(View v) {
Intent go_To_FragmentTwo = new Intent(this, FragmentTwo.class);
startActivity(go_to_FragmentTwo);
}
please with sample code. and please introduce some articles.
You would need to do the following:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) view.findViewById(R.id.pid)).getText().toString();
Intent intent = new Intent(view.getContext(), EventDetails.class);
intent.putExtra(pid, ID);
view.getContext().startActivity(intent);
}
The code below is actually also passing the id of the selected item from one fragment to another, to then display the details according to the value in the ID.
Hope this helps :)
How to implement an ExpandableList in a ViewPager in Android?
My question is the exact opposite of the above question, I want to display a list view (preferably an expandable one) and when an item is clicked I want display a bunch of images (different ones for each item of course).
P.S: Please dont tell me to launch a different activity for each list item.
This is some possible guidance not solution. (I am not exactly clear on what the problem is)
I would probably just create one activity. Each time you get a click on the listview it creates a intent with some stored flags. Then you start the activity with that intent. Unpackage the intent to find out what you want you activity to do.
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(this, myclass.class);
intent.putExtra(KEY, v.getId())
intent.putExtra(KEY2, position)
intent.putExtra(KEY3, id)
startActivity(intent);
}
};
And then get the intents flags in the new activity onCreate()
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
resId = intent.getIntExtra(KEY, 0);
...
super.setTitle(title);
Not sure if that what you were looking for hope it helps