Android - Add an item from one ListView to another ListView? - java

I have an app that shows news from a RSS feed. There is ListView-1 which loads the news and when you click on an item(news), it shows only the selected news in a seperated xml layout. On the ActionBar for the single news item layout, user can click on Add to Favourites.
I have another activity and layout file and a listview just for Favourites. How can I copy an item from one listview to another?
So, the user can add news from main ListView-1 to another list called ListView-2.
I cannot use intents for sending one list item.
I have been thinking of using SharedPreferences to share an ArrayList, but something else needs to be the solution.

You don't need intents, just separate view and data, keep news in the singleton class, for example NewsManager, and keep one list for all news, one for favourite news, and use favourite list in the ListView2 adapter

Related

How to set a specific text with the word in the list view?

I am creating a list view by adding specific courses on the main page in Android app.
When I click on one of the ListView items (courses), I will go to a new page using intent. In that new page I would like the text to be replaced with the name of the course that I clicked on.
Is there any example showing how I would do this? I cannot show all my code cause there are multiple pages and fragments.

User management UI

i am doing a project using android studio in which once user logins,images of the books in their cart should be displayed. But different users have different number of books in their account like one user may have one book in their account and other may have three books in their account. I can't do this using static xml as i don't know how many image view to put in xml design.so, if their are two books in user account, only two image view should be displayed.My friend suggested me to use stack overflow and list view. so , how can i do this??
You'll have to use ListView or RecyclerView which is advanced version of ListView.
To do so Create two XML files.
One that will hold the ListView. Something like this <LinearLayout><ListView/></LinearLayout>.
And other will have the contents of ListView, which in your case are Images.so create <LinearLayout><ImageView/></LinearLayout>.
with these two XML files you don't have to care about the number of images/books each user have.
In your main activity create a ListViewAdapter and assign this adapter to your first XML layout containing the ListView.
Now in you AdapterClass getView() Method, inflate the second layout and you can access the imageview. Here you'll set the Image to your ImageView.

Android Studio - Passing a title and description through ListView

I have created an activity which I need to update with posts made up of a "title" and "description". Earlier a ListView was suggested as an option but I do not know how to add two pieces of text (title and description) to each individual item of the ListView through a String array.
There is a "new post" button on the feed interface that opens a new activity containing 2 TextEdit boxes for adding the title and description.
When "post" is pressed on this second activity I am trying to move the title and description into an array that updates the ListView on the first activity with a new item containing the title and description stated within the TextEdit boxes.
Any help would be greatly greatly appreciated,
Aaron.
The best possible way to this is by using a data base. Android has a built in SQLite data base. You can store in all your data into the data base and query it to get a key/value pair array, which you can pass to your listView adapter class and get the data you need into the list view.
Hope this link helps you out-
androidsolution4u.blogspot.in/2013/09/android-populate-listview-from-sqlite.html?m=1

Is it possible to reduce number of pages in android?

I am making an android app. this app consist of list. by clicking each item of list a new page opens.
this list contains 50 items.I should make 50 activity and their corresponding XML file.
so, is there any way that make this process easier that don't force me to make all this 50 activity one by one?
my reputation is not enough. so I upload related picture.
http://uupload.ir/files/oh1o_1.png
http://uupload.ir/files/zam9_2.png
Not sure what you mean by creating 50 pages. You have NOT to create a new class for each item in the list, instead you will create a new instance of a class. This is what programming, and OOP (Object Oriented Programming) comes into play.. Basically what you need to do is:
Create a "main_activity" class which will contain your list
Create a "item_detail_activity" which will show the details about your clicked item
What you need to write is the logic of "passing" the correct data depending on the clicked item. When an item in the "main_activity" is clicked, you will create a new instance of "item_detail_activity", passing the correct data (through a bundle).
BTW there are a lot of tutorials out there that will help you understand better the logic of an Android application.
If you have a list of items probably means that you have a list of objects which are of the same kind with same structure.
Due to this, you will have 50 pages which show to the user the same item with different values to variables, with the same structure.
You can make the objects in your list parcelable (refer to this), then pass it with the Intent to a second Activity you create, receive it, and at last populate the screen with the item you passed.
If you have troubles or doubts feel free to ask :)
EDIT:
Imagine your listview is called ListView, and the List of items you used to fill the listview is called list in the MainActivity do this:
ListView listView = (ListView) findViewById(R.Id.ListView);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// position is the index of selected item
// Launching new Activity on selecting single List Item
Intent i = new Intent(MainActivity.this, SecondActivity.class);
// sending data to new activity
i.putExtra("item", list.get(position));
startActivity(i);
}
});
in your SecondActivity, with getParcelableExtra("item") you retreieve the item clicked. Here with the variables of this item you can populate the page.
here is the doc for intents.
In secondActivity, if something must disappear if a variable has a certain value or is null, play with visibility or fragment: create an Activity with all case shown, than work with visibility or with fragments and adjust it ;)
Change your activity to fragment activity and write code to display the details corresponding listview click. it easy to follow.

Android: modify item on Listview

I have a ListView.
I'm using anArrayAdapter and I'd like to:
when the user clicks any item on the list, its LinearLayout (is just a content for x information) fades out and is substituted by other LinearLayout (with y information) which fades in.
However, I do not know how to apply this on my ArrayAdapter. I've searched for a while but I'm not understanding how can I access a single item from the Adapter and make it's children fade out or fade in.
Any help would be very appreciated. Thank you very much for your time.
In general - adapters do not hold items the role of adapter is to get data model, and to produce item views when i.e. list view needs them.
The main concept of replacing item is to replace data inside the adapter and call notifyDataSetChanged(); - thats all.
I don't like ArrayAdapter as it's aimed for very simple scenarios. Usually I just create my own Adapter class extending BaseAdapter (just 4 methods to implement).
The transitions of items on ListView are described here: Adding animation to a List View in Android

Categories