My problem is follow (I know that selected structure of my classes looks like bizarre, but ...).
I have the MainActivity with inner class, that extends AsyncTask to parse HTML from internet.
This activity parses HTML-page and fills the ListView in AnotherActivity.
AnotherActivity must call the inner AsyncTask and PostExecute must fill the ListView by another data (the sense of this actions that AnotherActivity is results of search and another search means the going to new page of search's results of YouTube)
I wrote the next code in PostExecute of AsyncTask:
where I get Activity with ListView, create adapter and assign it to ListView.
ResultsQueryActivity resultsActivity = new ResultsQueryActivity();
ResourceDataAdapter resourceDataAdapter = new ResourceDataAdapter(resultsActivity,MainActivity.listVideosData);
resultsActivity.setListAdapter(resourceDataAdapter);
After execution of this I get the message
*02-01 07:40:56.784: E/AndroidRuntime(525): java.lang.IllegalStateException: System services not available to Activities before onCreate()*
I really have no idea how to resolve it. Thanks for help!
Why do you have two activities, when both of them seem need to be done on the same activity?
Just use one activity. When parsing is done, update the ListView with your data, then show the ListView. Initially the ListView would be hidden. Your other views (if any) would be hidden as well.
Or use a ListActivity, doesn't really make a difference.
I would guess it has something to do with the way you are creating ResultsQueryActivity... Typically you would start it via an Intent and pass all your video data to it by putting it into a Bundle. Try replacing your code in postExecute() with something like this (assuming your video data is a String array):
Intent intent = new Intent(MainActivity.this, ResultsQueryActivity.class);
intent.putExtra("videodata", MainActivity.listVideosData);
startActivity(intent);
Then in the ResultsQueryActivity class, you can retrieve the data in the onCreate(Bundle b) method:
Bundle extras = getIntent().getExtras();
String[] videodata = extras.getStringArray("videodata");
You cannot instantiate a activity, activities is managed by system.
The flow of the two activity is not very well.
If MainActivity is only used to parse html, then it should not exist. Put the the asynctask in AnotherActivity and fill the list in postExecute.
If MainActivity must exist, then you should not fill data in the task. Try to parse the data to AnotherActivity, and let it fill data by itself.
Related
I want to fill ("calculate") a ListView in a Loading Activity and when it's ready, start the new Activity with the ListView filled. So I don't have to wait to fill it in the second activity. I already have the code to fill it, but it's in the second activity.
Thanks
First, this is not really possible. You cannot readily transfer widgets or adapters to another activity.
Second, this is not really necessary. Creating a ListView is fairly cheap. If you are experiencing performance anxiety, use Traceview to determine where the problem is, then scope your plan to deal with the problem. For example, perhaps it is your model data, not the ListView itself, that you need to load in advance. Or, perhaps you just need to move some work to background threads, such as loading images to go into the ListView rows.
Third, if these two bits of UI are this closely coupled, they should not be separate activities, but rather one activity. Whether you use fragments, or hide/show widgets, or whatever, is up to you.
Your code for Activities, User Interface that may contain creating custom views , AND code for adapters must be independently defined or in separate packages if u prefer to go for clean coding having done that u will not face above problem and also u can REUSE your code .
What you really want is to transfer the data from one activity to another activity. Instead of passing the "ListView", you should be passing the data that you need to populate in the other activity
Intent intent = new Intent(CurrentActivity.this, OtherActivity.class);
intent.putExtra("SOME_KEY", mySerializableData);
startActivity(intent)
You can pass any type of serializable data using intents.
So you should ideally:
Create your adapter somewhere outside of your CurrentActivity and
OtherActivity
Pass the mySerializableData from CurrentActivity to OtherActivity
Use the same adapter to populate the ListView using the data passed in the intent using getIntent().getSerializableExtra()
Use instanceof to ensure that what getExtras() returned is of the type MySerializableData
So for example if we want to pass a ArrayList<MySerializableData>
Object obj = getIntent().getSerializableExtra("SOME_KEY");
if (obj instanceof List) {
//Do stuff
}
I'm an Android newbie and I'm trying to update my (working!) code that uses a cursor so that it uses a CursorLoader. My app begins with an Activity with a ListView with three choices, Beginning, Intermediate and Advanced. Depending on which item is chosen, I want to open a second Activity with another ListView. That ListView will be populated from a SQLite database where all the items have a level of e.g., Beginning. Elsewhere on stackoverflow, it was suggested to me that I insert an Activity between these two activities to handle the actual data retrieval. So now I have three Activities. One has a ListView with three choices. That one sends a String to the second Activity via an Intent. The second Activity queries the database and retrieves one column from all the records matching the level chosen into a CursorLoader. And here's my question. I want to pass all this data to my third Activity so that the data can be displayed in a ListView. But how do I get the data out of the CursorLoader? I'm thinking I want to do something like:
Intent data = new Intent(getApplicationContext(), com.MyKnitCards.project.StitchList.class);
Bundle dataBundle = new Bundle();
dataBundle.getStringArrayList(myData);
where myData is an ArrayList of data from the CursorLoader. I have no idea how to do this. I'm wondering if I want to use LoaderManager to accomplish this, but I have no idea how to do this either.
Thanks in advance!
I would get rid of the middle man Activity that does the data retrieval and just do it in the Activity that displays the data. CursorLoader that automatically loads in the background so it won't freeze your UI, and that way you don't have to worry about passing data between activities.
I have two activities an "EventsList" which extends ListView and a "CreateEvent" Activity. The user can pick "Events" from the list and edit them in the CreateEvent activity. When the user clicks save CreateEvent than spawns an AsycTask to communicate the data to a webservice, gets a response and parses the response into the database.
My problem is I dont know how to communicate the result from the AsycTasks onPostExecute() to the EventsList activity. I tried refreshing the list on StartActivityForResult but the thread often hasn't completed itself by this point.
So, how do I communicate the result of the AsyncTask (spawned by CreateEvent) to the EventsList activity? I had no luck trying to use a custom callback.
Edit
I'm really not keen on using a global flag or adapter. I'm sure this is best done using a callback/Listener somehow. That way the list is only changed or notified when absolutely necessary.
from EventsList startActivityForResult "CreateEvent" .
inside async task put data as putExtra's in case large or complex dataType you want to transfer use application level data storage or static instance .
in onActivityResult of EventsList read data by putExtra or static intent .
I think if you fill data of list adapter of EventsList activity (As adapter has a global reference) in AsyncTask and onPostExecute() of AsyncTask just finish() the CreateEvent activity which lends you to EventsList's onActivityResult() now in this just call notifydatasetChanged() for adapter.
You could always use a BroadcastReceiver, and broadcast a completion action from your AsyncTask's onPostExecute() with whatever extras you want your activity to receive. If you need to pass an object in the result, create a model object that implements Parcelable, and send the Parcelable as an extra in your intent.
I have a TabActivity with two Activities. One ListView for the inbox and another for the outbox.
When I click on an item(a thread) on the ListActivityThreads, I get the messages of that thread, in another ListActivityThreadDetail. When I am on that activity and answer a message I want to modify that single thread and passing it back to the ListActivityThreads.
Following the normal way to set results back to the previous activity I do this:
I use startActivityForResult in ListActivityThreads and in ListActivityThreadDetail I set a result for the intent. But I cant get any data on onActivityResult in ListActivityThreads.
I have tried different ways to start activities and passing values on a TabActivity. But no luck so far...
I cant use a BroadcastReceiver as my class already extends ListActivity.
Also I have been trying this example using TabgroupActivity and startChildActivity. But cant receive anything using the method onActivityResult, neither implementing finishFromChildActivity on my parent acitivty.
I am really stuck with this, any help would be really appreciate!
There is one alternative for this problem is that you can take one static variable in parent activity and you can access that static variable in child activity and in child activity you can modify that variable and so you will get modified value in parent activity
This is a newbie question. What is the best way to pass an object from an Activity to its View? I know the View has access to its context. Are Intents still the way to go? Or is there something else?
EDIT: Did not know the details about the data mattered :)
The data is user information obtained from a server in the Activity stored in an object. It contains userid, name and other details (mix of longs and strings).
The idea is to use/show the data in the view and possibly modify some of the fields and pass it back to the Activity which will send it to the server. Also I create and launch the View after I have the User information object with me. So after the View is launched it has to somehow get the object when it wants to use it.
P
You can use setTag: http://developer.android.com/reference/android/view/View.html#setTag%28int,%20java.lang.Object%29
I don't know what you exactly mean with passing data form an activity to a view as a view is normally part of an activity.
So I would suggest you start an AsyncTask in the onCreate() method of your activity which downloads the data from the server. When the AsyncTask finished you pass the data pack to a method of you activity which takes the data and fills the corresponding view elements. To get a reference to the views like TextView, EditText and so on you can use the method findViewById().
When the finished editing the data he must press a Button which triggers an OnClickListener that fetches the edited data from the views and starts another AsyncTask to upload the data back to the server.