Is it possible to reduce number of pages in android? - java

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.

Related

Changing something in a view depending on the sequence of activities

Is it possible to change something in a view depending on the sequence of activities? For example, in an adapter I would like to highlight a text, corresponding to an element clicked in a list of elements of a previous activity. But this, only if this adapter is the result of a sequence of certain activities, given that it is used in a fragment, hosted by an activity that can in turn be called up by one of two other activities.
You can keep history of every activity opened by keeping a list/stack of ids (integer or string). First activity that starts in your app will make a new empty list and record itself into it. When another activity is opened you pass that list in the intent. Next Activity extracts the list and adds its own id in it then passes it to next Activity in intent. This way, any activity will have a history of all activities opened previously.
If you want to send additional information along with ids you can make a Parcelable class and pass that along.

How do I get a view from other activity?

Let's say I have two activities/classes. MainActivity and SecondActivity. For each of these activities I have individual XML files. In SecondActivity, I have a ListView with ID listview.
How can I retreive the ListView from SecondActivity in MainActivity?
I tried just plain and simple (in MainActivity.java):
ListView listview = (ListView) findViewById(R.id.listview);
But that crashes my app when I try to set an adapter on it (nullPointerException). I tried placing it directly inside my class, and inside the onCreate method. Both causes a crash.
The line that causes the crash looks like this:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listview.setAdapter(adapter); //crash (nullpointerexception pointing to nullobject)
What I want to do is get that ListView and use the "listview" object to add elements to the listview in my MainActivity.
In Android (with exceptions) you cannot have more than one activity running at the same time. You can have multiple Fragments, but a single activity is shown at a single time (ok, this is not 100% true anymore, but for all intents and purposes, this is still the case).
What you're trying to do is considered extremely bad practice in Android (and iOS for what is worth).
What can/should you do?
Your requirements appear to be some form of: I have a list of items, and I also have another activity where I want to add or remove things from said list.
This is fine, but you don't need "two activities" to do this.
Have one activity to show the list, and when you want to add the contents, either launch another activity to show the UI of the "edit/add" or use Fragments (one Fragment would have the list, the other would have the UI to edit the list).
Where is the data in all this?
Stored in a THIRD place (let's call it "YourListRepository"), which is a class where:
From the "List" you'd simply say: Repository, give me the latest list!
From the "Edit" screen, you'd say: Repository, give me the item "N" from the list. And then, you'd say: Repository, here's the updated item "N", refresh it.
Going back to 1 (to display the updated list) is automatic, since it asks "for the latest list".
Where to learn more?
Given the tenure of the question you posted, it appears that you're relatively new to Android development, and therefore I suggest you spend an hour of your time, reading Application Fundamentals from the official Google documentation.

How to put a recyclerview inside another and receive data from the row selected?

Hi guys i need some help in java im trying to make a recyclerview that have some items and i wanted whenever someone clicked on one item it took him to the other recyclerview but with the data i set it for ?? PLS HELP
if you want to send the data in this specific item.
in the recyclerview class
the list of the items presented in the recyclerview declare them as global variables.
and you will find this method
public void onBindViewHolder(Holderclass holder, final int position)
you can use this position variable to access that specific item in the list.
for example if you have arrayList "list" then.
list.get(position).
save the data in bundle and use putextra() to send it if it is a new intent or setarguments() if it is a new fragment, and then retrieve the data via getextra() or getarguments() in the new activity or fragment. repeat what you did with the first recyclerview
I hope this helps

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

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

Dynamic content for Android layout

Pardon the question title because I'm not really sure of the right term(s) to use.
I am just familiarizing myself with ListView and what I want to do is this:
-Suppose I have a Dog class which has String name, String breed, and int age.
-I would then make many instances of Dog and put them into a ListView which displays only their names.
-I would also have one layout, lets say doginfo.xml, that has the text "Name:", "Breed:", and "Age:".
So how can make the values of the clicked item (ie: name, breed, age) display on the next activity, which is doginfo.xml?
I'm presuming you have an array/arraylist of your dogs and you are adding them in order to your listview. Check out the onItemClickListener of this exmaple in the android doc:
http://developer.android.com/resources/tutorials/views/hello-listview.html
In your click listener use "int position" to get the object from your array/arraylist. Once you have this information, you can pass it easily between activities using "bundle.putString()" and "b.getString()".
Look at this simple tutorial for information on passing data between activities:
http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/
It's easy. Once you got your ListView, set an OnItemClickListener:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onClick(AdapterView<?> parent, View view, int position, long id) {
//Get your Dog object from the 'position' parameter you get here.
//You should save the 'Dogs' in an array, so you can access them here easily.
Dog d = mDogsArray[position];
Intent intent = new Intent(YourActivity.this, DogInfoActivity.class);
intent.putExtra("name", d.getName());
intent.putExtra("breed", d.getBreed());
intent.putExtra("age", d.getAge());
startActivity(intent);
}
}
If you want to add/remove dogs dynamically, you can use the method ListView.setListAdapter to change the views the list view is showing. Create an array adapter this way:
ArrayAdapter<String> dogs = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
and add dogs this way:
dogs.add(dogInfoString)
Let me reply the step that you need to follow:
Create an arraylist of dog class object ArrayList<DogClassObject>
Now in 1st activity, just take the name from every object from ArrayList and add one by one name in ArrayList<String>.
Use the ArrayList to display inside the ListView of 1st activity.
To implement this, go through this example: Android Simple ListView
Now, to implement on click listener and to display dog_info, perform the below steps:
Implement OnItemClickListener inside the ListView.
Follow the answer given by #Jong but pass the clicked object from the ArrayList (you can get the clicked object from ArrayList by position argument).
Get the dog information and display name, age, breed info inside the dog_info
To implement this, go through this example: Android - Custom ListView with 2 TextViews

Categories