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.
Related
I have an app where a user can find an image from the gallery and it will be shown on the screen through a button click. The user can exit the app and the image they chose can be saved, so when they click the button again, it will also be shown. The image is shown the first time when it is picked, but the saving functionality does not seem to be working since the image is not being shown after the user closes and reopens the app.
I have tried looking up what camera permissions I may need, but I have not had any luck. I don't know what I would need since the app is able to load the image the first time.
//This is the method that shows the image
public void showImage(View view)
{
LinearLayout scroll = findViewById(R.id.imageViewer);
for(String i: imgDataList){
ImageView image = new ImageView(this);
image.setImageURI(Uri.parse(i));
scroll.addView(image);
}
}
//This is the method that works with the image that is gotten from the gallery
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
String imgData = data.getDataString();
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
imgDataList.add(imgData);
}
break;
case 1:
if(resultCode == RESULT_OK){
imgDataList.add(imgData);
}
break;
}
you may use sharedPreferences to store you image uri
please read more here about sharedPreferences and Save key-value data
UPDATED:
I am creating an app where onClickListener is used to convert speech to text and input the text to a List View field. Likewise once entered I would like to re-trigger the speech to text option and input the new text into another List View field whilst still retaining all the other List View fields which were already filled in.
The following is part of the Java file that calls the speech to text option with the various statements I want answered. Originally the prompts appeared exactly one after the next which is perfect but it didn't assign each text to the corrresponding edit text field. It only recorded the speech to text of the very last prompt only. Now with some help on this thread I have updated the code to the following, where checklv1 etc correspond to unique integers:
public void onClick(View v){
Intent i1 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i1.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i1.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the current time?");
startActivityForResult(i1, check);
Intent i2 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i2.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i2.putExtra(RecognizerIntent.EXTRA_PROMPT, "Is the status Confirmed or Unconfirmed?");
startActivityForResult(i2, checklv1);
Intent i3 = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i3.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
i3.putExtra(RecognizerIntent.EXTRA_PROMPT, "What is the Temp?");
startActivityForResult(i3, checklv2);
}
The following part of code shows to assign the text to the editText field lv1, lv2 and lv3:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == check && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == checklv1 && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == checklv2 && resultCode == RESULT_OK){
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
lv3.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));}
super.onActivityResult(requestCode, resultCode, data);
}
The code is now progressing and inputting the speech to text phrases into each of the list view fields. However my only issue is that lv3 contains the correct phrase, lv1 also contains the correct phrase but lv2 is a the same phrase as lv1 when it should be the phrase corresponding to lv2. Most likely there is something wrong in the check assignment.
If anyone can please help me with where I have gone wrong in the above code that will be greatly appreciated
The check field you are passing to startActivityForResult is what you should be using to distinguish the action you are performing. Pass 3 different values here and switch on them in onActivityResult to determine which part of the UI you want to update.
startActivityForResult(i2, checkLv1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode) {
case checkLv1:
update(lv1);
...
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);
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.
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?