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.
Related
I've implemented a clickable List View using an ArrayAdaptor:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_tasks);
myTasksListView = (ListView) findViewById(R.id.mytasks_listView);
myTasksListView.setOnItemClickListener(this);
tasks = getTasks(); //of type Task[]
ArrayAdapter<Task> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, tasks);
myTasksListView.setAdapter(adapter);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Intent intent = new Intent(this, TaskDetail.class);
intent.putExtra("position", position);
intent.putExtra("id", id);
startActivity(intent);
}
Now, in the new activity I'm starting on item click, I can get the position of the item that was clicked; however, I'm not sure what would be the best method of getting the actual array. Can I
Find the Activity that launched the new activity and then access the ArrayAdaptor from there?
make my class Task parcelable and then just call intent.putExtra() on my object itself?
or is this another approach that's considered "best practice"? I was trying to follow along this helpful comment but it looks like the array in question was hard coded?
The best approach would be to make the Task class implement the Parcelable interface. If you did so, you'd be able to use Intent.putExtra(String, Parcelable[]) and simply call
intent.putExtra("tasks", tasks);
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 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 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..:
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