ListView pass data to another ListView - android

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.

Related

How to update the UI in android

I have an activity A with a listview and upon selecting some items in the list, and navigating to another activity B with the selected items in the list of Activity A. And made some modifications. After return to A. My screen is not updating with the latest modifications.
Please guide me how to do this. Thank you in advance.
If you have unified data storage for both activities (so that you know that this problem is purely UI issue, because the data itself is updated upon returning from activity B), then you should call this method on your ListView adapter:
yourListView.getAdapter().notifyDataSetChanged()
In order to display what you want, you need to have some TextViews in your other activity and then just add a text to them to display what you need.
When you get the data, just do like this in the onCreate method:
TextView tv = (TextView)inflate.findViewById(R.id.myTextView);
tv.setText(theDataStringYouReceived);

What is the best pattern to sync data between two recycler view with two activities?

I have two activities where they have almost same data, I am using same Adapter, But the problem is how to sync data.
E.g.
Activity A has recyclerview where it has like buttons in each row with unique id. Activity B also have same recyclerview but with some filter mechanism so it is not going to show all row, my question is how to handle like button state in Activity A and Activity B, such that if I click on activity B's like button than activity A's like button will automatically gets checked and vice versa.
You should use common data source for both the recycler views.
Get data from API
Save data in your local db
Feed the data to your Activity A recycler view
Feed the same data to your Activity B recycler view
All the changes you make to data should be in local db so both the
views can update automatically.
you can use static modifier
define BaseRecyclerView which has a static list of the items
extend CustomRecyclerView's from BaseRecyclerView
when you manipulate the items in list such as setting like flag the list in two CustomRecyclerView's will be changed because of static modifier
Manage Locally:
Your same list of data should be accessible in both of the activities. You can save that list in your application class. And if you don't want to save in Application class then on every switch of Activity you have to pass that list data between both activities.
Next thing in Model class you have to make one more variable to save a state for like button, once you make it like then I should save that value and when you went to retrieve that same list data it will give you updated data for like button event.
Through code you can achieve:
class MyApplication extends Application{
public static ArrayList<MyModel> myModelList = new ArrayList<>();
}
class MyModel{
public boolean isLikeSelected;
}
Activity A:
onLikeClick(int position){
MyApplication.myModelList.get(position).isLikeSelected = !MyApplication.myModelList.get(position).isLikeSelected;
adapter.notifyItemChanged(position);
}
Activity B:
onLikeClick(int position){
MyApplication.myModelList.get(position).isLikeSelected = !MyApplication.myModelList.get(position).isLikeSelected;
adapter.notifyItemChanged(position);
}
Here your application class is having static ArrayList which can be accessible throughout the app and have a stable and updated record of list data.
Using Server:
On every click at like button you will update to the server for the event and next time on Activity switch you will get updated list from the server, so no need to worry in case of server management.

ListView dynamic onClickListener USING Database ID

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.

Advice needed with context menu

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.

How to get a selected contacts?

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

Categories

Resources