Error: Activity result fragment index out of range: 0x2fffe - android

When im trying to delete a show with an AsyncTask. I want to call finish() after the AsyncTask has been completed and return an Intent with the result.
from the activity:
new DeleteShowTask().execute();
Intent intent = new Intent(SeasonActivity.this, FragmentShows.class); // I'm not sure if this works
intent.putExtra("tvdbid", tvdbId);
setResult(DELETECODE, intent);
finish();
then in the fragment i have this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("RESULTCODE", resultCode + "");
Log.d("REQUEST CODE", requestCode +"");
if (resultCode == SeasonActivity.DELETECODE)
{
if (requestCode == SeasonActivity.SHOW)
{
String tvdbid = data.getStringExtra("tvdbid");
for (int i = 0; i < adapter.getCount(); i++) {
SickbeardSerie serie = adapter.getItem(i);
if (serie.getTvdbId().equals(tvdbid))
{
adapter.remove(serie);
adapter.notifyDataSetChanged();
}
}
}
}
}
But it seems like it doenst run through this onAcitivityResult().
I have logged the onActivityResult() as you see but i dont get any logs.
Only thing i get is: 10-19 16:21:44.631: W/FragmentActivity(27672): Activity result fragment index out of range: 0x2fffe

I fixed it for now by using a work around.
I now reload my AsyncTask in the onResume() instead of just removing an item from the arraylist and calling adapter.notifyDataSetChanged(). But its not the best solution.

if you using a Fragment in a another Fragment. You should be call
getParentFragment().startActivityForResult(i, SELECT_PICTURE);

Related

maximum image selection limit from gallery Android

I am trying to get image's Uri in the Gallery built-in app from inside my application.
so, I was using the Intent below, but it selected many more image.
i want to set limitation. less than 3
#Override
public void onClick(View v) {
Intent intent = new Intent( );
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "select images"), PICK_IMAGE_MULTIPLE);
}
how do i fix this.
Do you have any suggestions?
Unfortunately, as stated by http://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE, this is not possible.
This is a boolean extra; the default is false. If true, an implementation is allowed to present the user with a UI where they can pick multiple items that are all returned to the caller.
You'll have to manually check the returned data to see if it's more than 3 items, and if so, show a Toast and let them try again.
As Daniel stated, there is no possibility with Intent.EXTRA_ALLOW_MULTIPLE. An alternative though, is using the MultipleImageSelect library. Not only can you select multiple images, but the extra ability to set a limit on images selected by the user.
Check out the repository or a sample.
STEPS:
Step 1:
Add MultipleImageSelect library, together with jitpack.io to your build.gradle like this:
repositories {
maven {
url "https://jitpack.io"
}
}
dependencies {
implementation 'com.github.darsh2:MultipleImageSelect:v0.0.4'
}
Step 2:
In project's AndroidManifest.xml, add the following under application node:
<activity
android:name="com.darsh.multipleimageselect.activities.AlbumSelectActivity"
android:theme="#style/MultipleImageSelectTheme">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Step 3:
In the activity from where you want to call image selector, create Intent as follows:
mSelectImagesBtn.setOnClickListener(view -> {
Intent intent = new Intent(ListingImages.this, AlbumSelectActivity.class);
intent.putExtra(Constants.INTENT_EXTRA_LIMIT, 3); //set desired image limit here
startActivityForResult(intent, Constants.REQUEST_CODE);
});
Step 4:
and override onActivityResult like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data !=null) {
ArrayList<Image> images =data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
imagePathList.clear();
StringBuffer stringBuffer = new StringBuffer();
//loop to retrieve the paths of each image and display to TextView
for (int i = 0; i < images.size(); i++) {
stringBuffer.append(images.get(i).path + "\n");
}
textView.setText(stringBuffer.toString());
}
}
DONE
Alternatively,
If you're using an Adapter to inflate images to display, you can instead have this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
imagePathList.clear();
for (int i = 0; i < images.size(); i++) {
imagePathList.add(images.get(i).path);
}
imageAdapter.notifyDataSetChanged();
}
}
Within ImageAdapter, display images to populate recyclerView, like follows:
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String path = imagePathList.get(position);
Picasso.with(mContext)
.load(new File(path))
.placeholder(R.drawable.ic_house_placeholder)
.into(holder.image);
}

Correctly load data in listview from startactivityforresult

GOAL
What I am trying to do:
Click on my search button and my database is queried with the results passed in. If nothing is found, we are taken to an activity which says so, but if results are found they are loaded into a list.
What I have done
When I click on the search button I call startActivityForResult then this intent calls an activity (whose layout consist of a list_view). The search button also pass along my parameters and query my database.
if there are no results then an activity saying "No Records" is displayed"
and if there are records the else condition is true and the records are loaded in the list
PROBLEM
The problem I am experiencing is, when the list is loaded, if I want to go back to my search form, I must press the back button a total of three times. I am not entirely sure but I believe this strange behavior is stemming from me not returning a result to the started activity when the else clause is invoked.
I have placed what I think is the important part of my code below, would appreciate any assistance
Main Activity
private void startStudentQuery() {
Intent intent = new Intent(getBaseContext(), retrieveStudentData.class);
intent.putExtra("firstname", firstname);
intent.putExtra("lastname", lastname);
//startActivity(intent);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
Intent X = new Intent();
X.setClass(getBaseContext(),NotFound.class);
startActivity(X);
}
}
retrieveStudent Activity
//this activity is a listview
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_student)
}
//Left out some code, just showing the main parts
public class StudentAsynTask extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... params) {
try {
//return result to show Activity if no records are found
if (jsonArray.length() == 0) {
Intent intent=new Intent();
setResult(2,intent);
finish();
} else {//Show list if records are found
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jRealObject = jsonArray.getJSONObject(i);
Student student = new Student ();
student.setFirstname(jRealObject.getString("f_name"));
student.setLastname(jRealObject.getString("l_name"));
student.setImage(jRealObject.getString("image"));
studentList.add(student);
}
}
In startStudentQuery, you should call startActivityForResult only, but now you have called retrieveStudentData twice.

Updating ExpandableListView in OnActivityResult

In my fragment I have an ExpandableListView, which has Categories as groups and Base_Items as children, they're coming from a DB. From here I open an Intent for result to enter new Items. OnActivityResult I want the new item to appear immediately in the ExpandableListView and then enter it to the DB. I tried that:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK){
switch(requestCode)
{
case Constants.Request_Codes.REQUEST_CODE_CREATE_NEW_ITEM:
String new_item_name = data.getExtras().getString("ITEM_NAME");
int new_item_category = data.getExtras().getInt("ITEM_CATEGORY");
Base_Item bi = new Base_Item(-1, new_item_category, new_item_name);
ArrayList<Base_Item> arr_bi = arr_all_categories.get(new_item_category).getCategory_items();
arr_bi.add(bi);
adapter.notifyDataSetChanged();
manager.add_new_base_item(bi);
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
The new item is being inserted into DB just fine, but the ListView doesn't update. I have to exit the app and reopen it to see the new item in the list.
How can I update the ExpandableListView and see the new item immediately? Thank you!
Well, I ended up just repopulating the ListView again in my onActivityResult method. I'm not sure it's the best way (speaking of performance) and perhaps I should've been keep looking for the answer, but for now it just works.

onactivityforresult not called after contactscontract action_edit

I have a ListActivity which displays all the aggregate contacts. When the user clicks one, my code calls startActivityForResult. All this works properly. When the user finishes editing, I want my ListActivity to be displayed again. Instead, the "people" activity gets displayed. Similarly, my onActivityResult function never gets called.
Here is the code handling the click:
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
Cursor cur = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
cur.moveToPosition (position);
String key = cur.getString (2); // "2" is the col containing the LOOKUP_KEY
System.out.println ("clicked " + key);
// make intent to edit contact
Intent intent = new Intent (Intent.ACTION_EDIT);
intent.setData (Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + key));
startActivityForResult (intent, 2);
}
And I also have an onActivityResult function:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
System.out.println ("request " + requestCode + ", result " + resultCode);
}
Any suggestions?
I filed a bug to android about this. Someone looked at it and responded that there is an undocumented workaround. From the bug report:
The undocumented workaround is to call putExtra("finishActivityOnSaveCompleted", true); on the ACTION_EDIT Intent.
However, as this is undocumented, I have no idea which Android version(s) will use it.
I tried it and it works for the version of Android I'm using: 4.1.2. See issue 39262 for more info.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 2) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
Replace your onActivity result to this. for request code get after
they will work
Add
super.onActivityResult(requestCode, resultCode, data);
in OnActivtyResult().
Make sure you are calling startActivityForResult from an activity, then only your onActivityResult will be called. For example, if you have the similar code in an fragment, the onActivityResult will never be called.

notifyDataSetChanged() doesn't update ListView

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
deserializeQuotes();
quotesAdapter.notifyDataSetChanged();
}
}
My array clearly has been updated, and I can see the change when my app starts, but why doesn't it update in this method? The code enters this method.
this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);
quotesAdapter.notifyDataSetChanged();
listView.setAdapter(quotesAdapter);
Works, but why do I have to create a new adapter? Why can't I use my existing one?

Categories

Resources