Passing extra data through the Storage Access Framework - android

I have a ListView and from a click on an item of the ListView I call the Storage Access Framework (SAF).
Just before starting the SAF, I want to pass the position of the ListView like this:
intent.putExtra("Position", position);
startActivityForResult(intent, WRITE_REQUEST_CODE);
But how do I get the position back in the onActivityResult() method so I know which item is clicked?
I tried these lines, but with no success:
Bundle extras = resultData.getExtras();
Integer i = getIntent().getIntExtra("routeDataPosition", 42);
Integer i2 = resultData.getIntExtra("routeDataPosition", 42);

Just before starting the SAF, I want to pass the position of the ListView like this:
That does not work. You cannot force extras into another app, and you cannot force another app to return those extras.
But how do I get the position back in the onActivityResult() method so I know which item is clicked?
You could store it in a field in your activity, fragment, or viewmodel.
If you wanted to get elaborate, you could use a different request code based on the position. So, instead of WRITE_REQUEST_CODE, use WRITE_REQUEST_CODE + position. You would need to have smarts in onActivityResult() to get the position back out of the result code (e.g., position = resultCode - WRITE_REQUEST_CODE).

Related

ANDROID. Send request to database from two RecyclerViews and receive data in third RecyclerView

There is a fragment which contains three RecyclerViews (all of three contain CardViews). One recycler displays city names and second displays fuel types. The third recycler is supposed to display filling stations and corresponding prices for selected city and fuel type.
All data is stored in MySQL database on web-server and query is written in a PHP file. I have adapters for all 3 recyclers, and the question is how to display data in the third recycler when user clicks on the first or the second recycler, all of that in one fragment. Data is sent to query from first and second recycler, while the result should be displayed in third.
If needed, I will post my code (fragment, adapter or anything else you need).
I am relatively new in 'Android programming' and I would really appreciate some help with this case.
I have already searched for the solution, but there was no complete or correct solution for it.
Thank you!
If you want to pass the data from the fragment to an activity you can pass it with intent.putExtra.
from a fragment to an activity, in the adapter you will do:
Intent intent = new Intent (context, NameOfActivity.class);
intent.putExtra ("nameFromArgument", value);
context.startActivity (intent);
in the activity that will receive the fragment data you will do, within onCreate:
Extra bundle = getIntent (). GetExtras ();
String received = extra.getString ("nameFromArgument");
after that you will get the data from the received string that came from the adapter and will pass it in your request, if you want to pass more than one parameter to the activity, just repeat intent.putExtra always changing the nameFromArgument and receiving the same way in the activity, you too you can pass an object if you want

How to get value from another activity and bring back to previous fragment?

I have an Activity that have 3 fragment(FragmentA, FragmentB, FragmentC) like sliding tab. From FragmentB call another activity (lets call ActivityBB). After get item from Activity BB, How I can get value from ActivityBB and bring back to previous FragmentB ???
Well there are three ways that just comes in my mind. There may be more. But for now let me tell you those.
On ActivityBB put the values that you want to save in SharedPreferences. And then restart your activity. Well yes this might only work if you have values that can be arranged in key-value pairs. And is also not the proper way to do things. But will get your job done.
To restart an activity use this code. and then get your values from SharedPreferences.
Intent intent = getIntent();
finish();
startActivity(intent);
You can implement interfaces. This method is the best way to communicate between fragments. For more details check the google's documentation.
http://developer.android.com/training/basics/fragments/communicating.html
You can use Bundles. For that check this link.
How to pass a value from one Fragment to another in Android?
You can try some like this..
Pass your value into intent.
This code in your ActivityBB
Intent intent = new Intent(ActivityBB.this,ActivityBB.class);
intent.putExtra("yourDataKey",yourData)
startActivity(intent);
After that get your value into ActivityAA and load your Fragment with desired data
Intent intent = getIntent();
String yourValue = intent.getExtra("yourDataKey");
I solved this use intent and bundel with this flow :
MainActivity(FragmentA, FragmentB, FragmentC)
This activty(eq : from FragmentB) pass data using intent to ActivityBB
ActivityBB
at onClick ListItem in this activity, I pass data using bundle and call MainActivity (because I want to back to my previous fragment with item value from ActivityBB)
I make a condition from bundle at onCreate method at MainActivity to display currentItem(viewPager)
Actually its work, but I think this is not the proper way. I hope there is a solution with proper way from anyone to solve this.

How to repopulate an arrayadapter when database is modified in a different activity?

I am working on an array adapter which uses a master detail flow layout in order to show list of items that are stored within the apps database.
From this activity/fragment, I call a different activity to add a new record to the database. When the new record is added, this activity is finished and the user is returned to the master detail flow layout.
I then want this view to be shown with the updated database that was created in the new activity. I'm not 100% sure what would be the best way to implement this. I was thinking of in the onResume method, I call a function that repopulates the array adapter with the new data but this seems like the way wrong way as it would always repopulate even if no data was changed in the database.
You can start the activity for adding items using startActivityForResult()
and implement the callback onActivityResult() in the list activity.
You can repopulate the list items in the onActivityResult() in case there was a successfully added element to the database.
See this http://developer.android.com/training/basics/intents/result.html
For example in listActivity
private void addItem(){
...
startActivityForResult(intent, ADD_REQUEST);
...
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_REQUEST && resultCode == RESULT_OK) {
// here you can repopulate the list
}
}
and the addItem activity
private void addingItem(){
...
// when the item saved successfully you can set the results to ok
setResult(RESULT_OK);
...
}
How about if you save a value to shared prefrances that would act as a flag :
let us say the value is x : 1 if it is changed and 0 if not.
By default it should be zero off course. whenever the user accesses the new activity and the contents of the database are changes the value should be changed back to 1. (If not it would stay zero).
Then onResume you check this value if it is zero then you do not re populate the list. However, if it is 1, you change it back to zero, update the list from the database, and call notifydatasetchanged() on the adapter.
Good Luck
You could always use startActivityForResult() and inform the calling activity whether or not data was changed.

How to get clicked Image from ImageView and send to other activity

I am statring programming in Android and i have problem. I must create something like this : https://www.google.pl/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0CAcQjRw&url=http%3A%2F%2Fwww.technotalkative.com%2Fandroid-asynchronous-image-loading-in-listview%2F&ei=R3UKVdTXHoOGzAPO2oKQBw&bvm=bv.88528373,d.bGQ&psig=AFQjCNFkuC6H_DmyQz44Xy2xYZOnb7fAtA&ust=1426835140929053
but after clicked i need to go to second activity with bigger clicked image and descriptiont (e.g On the image is some place and under is description this place)
For this moment i have first activity ImageView+TextView(title) but i do not know how i can get something what let me identifiers clicked image and send send to second activity.
Any ideas ?
I found topic like this :
How can i pass image view between activities in android but this not resolve my problem.
Edit:
I have 2 xml activity for now :
First main activity with listview and second with linearlayout+Imageview+TextView. I use this tutorial for create firs screen :
http://javastart.pl/static/programowanie-android/wlasny-widok-listowy/
Create an OnItemClickListener and set it on your listview. The listener's onClick method is then called whenever you click an entry in the list. It has a parameter 'position' which tells you which row of the list got clicked. Now you can get the URL of the clicked image using imageUrl[position] (following your example).
Now start your second activity and pass that URL along like so:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("imageUrl", imageUrl[position]);
startActivity(intent);
In your second Activity, put this in the onResume method:
String imageUrl = getIntent().getStringExtra("imageUrl");
Voila, you've passed the url of the clicked image along to the second activity. Now just load it into your ImageView and you're done.
you can send bitmap of image in list via intent to other activity , so you will not need to load it again check attached answer
Or Try to use cached image library like that leocadiotine/WebCachedImageView
and pass link string via intent

Ways to refresh a ListView

I have an Android app with a ListView. Every row is generated dinamically by SQLIte query results. When users click on a item, the app should refresh the list with the "content" of that item, generated by another SQLite query, and so on.
I tried to "refresh" the activity creating a new intent and sending it to the same activity. Activity name is ShopActivity
ListView list_content = ((ListView)findViewById(R.id.listforshop));
list_content.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ID and text of selected item
long selected_id = id;
String selected_name = (String) ((TextView) view.findViewById(R.id.element)).getText();
// New intent
Intent intent = getIntent();
intent.setClassName(ShopActivity.this, ShopActivity.class.getName());
// Passing data and refreshing activity
intent.putExtra("id", selected_id);
intent.putExtra("name", selected_name);
intent.putExtra("type", "");
startActivity(intent);
}
});
In this way LogCat shows me a lot of warnings about Bundle. It seems that app cannot start activity.
Is there another way to "refresh" ListView? Is this implementation correct?
// New intent
Intent intent = getIntent();
intent.setClassName(ShopActivity.this, ShopActivity.class.getName());
This does not make a new Intent, this reuses the existing one... Re-using an Intent like this will throw a lot of warning and exception when you resume this Activity. In order to create a new Intent you need to use the keyword new.
But you don't really need to reload your entire Activity, simply fetch a new Cursor and use CursorAdapter#swapCursor() to refresh your ListView.
When trying to refresh your ListView you should think of using notifyDataSetChanged() of your adapter. this method tells your adapter that the provided data has changed and that it should update the list view.
So you should add the new data to your adapter and then call this method.
Sounds like you're viewing the details, don't use the same activity to view them. Add a new ShopDetailActivity and direct the user there on click, that way the user can press back to go to the original list (plus get the nice transition animation).
Another way of handling lists and details is to use Fragments. The advantage of this is that on large-screen devices, both the list and the detail can be visible at the same time.
Using one Activity to show both list and detail is messy. At the very least, use two Activities.

Categories

Resources