How do I get a view from other activity? - java

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.

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.

Start ViewPager from RecyclerView item

Basically what the title says. I'm trying to start a ViewPager activity when a user clicks a RecyclerView item. I'm not having problems here, that works fine. The goal is to go from a grid based gallery to viewpager gallery. I'm also trying to send the List<> of data which is loaded over the network. This is where I'm having a lot of trouble. The data is sent through a singleton because it's too large to send through intent and if opened in the new activity too quickly upon app startup, more data is loaded in the original activity (Even if Call is cancelled, using Retrofit2 btw) and I need to notify the adapter, which I have a listener that does.
Another crash that is common is a null pointer when reading an item in viewpager activity, which doesn't make sense to me because the item is not null in the original activity before sending, but if I wait a few seconds it is not null. Some of the code pertaining to my issue can be found here, a question I asked yesterday with a solution I could not successfully implement.
Please point me in the right direction for starting a ViewPager of the same dataset from a RecyclerView, thanks
When you are "sending" feed data to the view pager activity, use a shallow copy of the list instead of directly referencing it as shown below
DataTransferer.get().storeItems(new ArrayList(feed));
This should prevent IllegalStateException raised by feed being updated on your RecyclerView activity.

Android: Different methods in a fragment for different calling activities

I'm currently writing an app to provide food information/a grocery list function. My current code uses a switch statement to determine which button (fruit, vegetables, etc.) was pressed and passes an array list to a fragment that is then displayed as a listview.
When its called by the information activity, users can click on items and be presented with a detail view. If the calling activity is the grocery list activity, users can swipe to add to an empty array list. Since the bulk of the code used is the same, is there a way to do this without using two separate fragments?
All I can think of is putting the activity hashcode in the bundle being passed
to the fragment and using if statements to determine which activity passed the bundle (and thus which event listeners are used), but this doesn't seem like a very good solution to me.

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.

Better Performance, SQLite queries vs ArrayList<Object> in memory

I'm developing an app that uses an internal sqlite database. The MainActivity contains a Fragment.This fragment has a ViewPager which contains other 3 fragments, each of these 3 fragments has a ListView in it. when you click an item of the list, a second Activity is opened containing another ViewPager that has fragments showing the complete details(a bitmap and Strings) of each list item.
My question is, what would be better for performance ?
To make constant calls to the database each time the user scrolls through the detail fragments(ViewPager).
Make only three calls to the database (one for each listview in the mainActivity), to create 3 ArrayList of objects and keep them in memory. The list could have lots of items.
Is there something else I'm not considering ?
3. Is there something else I'm not considering ?
Yes, use ViewPager.setOffscreenPageLimit instead.

Categories