I'm creating a restaurant menu app with categories and items loaded from database and put them in a listview. Every category has it's own items. I would like to following behaviour: when I click on some category it should go to another activity and list it's items of the category id that listed in DB.
How do I create a dynamic onItemClickListener to send that ID to another activity to load it's items?
Its not about dynamic listener. Listener should be only one which would send chosen category id to somewhere where you could use further. Like sending it in intent's bundle to some activity.
Related
I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks
I have two activities with List View's:
Activity A --> ListView person
Activity B --> ListView favouritePerson
In person ListView there is a button i every item of the list
person --> Button likeButton
I would like to add person to favouritePerson list when the like button is clicked. I was trying to achieve this by adding data to singleton and then trying to get it from there, but without success. I have also tried to use SharedPreferences, but could not get it right either. What would be the best approach for this workflow?
I also forget to mention, that I'm populating the first listView by querying the Realm database.
I would like to add person to favouritePerson list when the like button is clicked
To get this, onClick like button, you need to write a query to insert favourite bit in data base against that record. And when you load activity B updated data will be populated. No singleton, sharedpreference or Parcelable is required. This is the best approach. Update database record on the fly and get updated data in Activity B.
So I have this problem, my listview is populated from Sqlite DB and I can click on any item in the listview. When I click on an item I want to display, in a new activity these "name, surname, age etc" details but these should be retrieved from my database.
Can anyone help me?
Thank you.
Use onItemClickListener method in your listview. When user clicks an item pass the item into next activity and in next activity retrieve data based on the item click. This tutorial will help you to get a good idea.
Import and Use External Database in Android.
Set an onItemClickListener for your ListView and when an item is clicked, you could either:
Place into a Bundle the details you want to be given to the Activity intent
Or
Place as an extra/bundle to the Activity intent the ID of the item you retrieved, then retrieve the details you want from the DB using the given ID inside of the next Activity
I have a XML layout which has two edit text fields, one for "title" and the the other for "story". When the user enters his data in these text fields and presses the back button the entry gets saved in a list view as the title set. The list view is present in an activity say A1. Now A1 extends Activity.
Whenever an item in the list is "long clicked" a context menu appears with edit, delete and read buttons. If the edit button is pressed I need to open another activity which can edit the data entered in the text fields corresponding to the item clicked. Also I'd be needing the id and the position of the item clicked in the list.
I am using list variable of type ListView to add my adapter. Also I am checking the edit, delete and read options of the context menu in the `public boolean onContextItemSelected(MenuItem item)' method.
How can I get the id and the position of the item clicked from here?
in adapter you make a getter and setter of your item. When long click listener, put setter your item in there.
You should store your "title" and "story" in database and you can get it form database in a new activity
I would think the best way to do this is to create an instance variable (declared after class definition and before onCreate) for the listview and assign the list view to it in onCreate(). Then you will be able to access the listview from your onContextItemSelected() method and pass them to your new activity
If you could post some of your code we should be able to help more.
I second Th0rndike's comments above. It is much easier to help someone if the question is readable and there is a good chance the answer will be accepted.
I'm new to Android. I have two activities. First activity shows selected contact list from second activity. Second activity shows contact list from database with checkbox. Now I can read a contact list from database and shows at checkbox. Therefore I don't know how to pass selected contacts from first to second activity and how to display selected contact list. Is there any easy way to do it?
Thank you for every advice and guide.
Thanks,
Zeck.
First you have to get Data from your database
then create a CursorAdapter or any Adapter you want and
fill it with data and override getView() there.
Here http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
and here http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
Set your adapter into a ListView then
implement onclicklistener on this ListView
where you will pass the data to Bundle and start a new Activity
listView onclick goes to a new activity
Here
http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/
and here
http://geekswithblogs.net/bosuch/archive/2011/01/17/android---passing-data-between-activities.aspx
There're many other ways how to share the data between activities
like SharedPreferences or Sqlite database. But it's not the topic for this answer.
hope it helps abit