-1

I have an activity that's runs an startactivityforresult and a onactivityresult. in the onactivityresult I can set the returned data to a variable and then put to add those variables to my arraylist, what I can't seem to do is use myAdapter.notifydatasetchanged() after this, just get a cannot resolve symbol. Is it because I created and declared the array in oncreate and the onactivityresult is not in oncreate?

Code snippets below...

Below done in oncreate

/*create array adapter and set to listview*/
    final ArrayAdapter<String> myadapter = new ArrayAdapter<>(ListView_Activity.this, R.layout.simple_list_item_1, R.id.row_item_text_view, mylistarray);
    final ListView mylistview = findViewById(R.id.mylistview);
    mylistview.setAdapter(myadapter);
    mylistarray.add("Test");
    myadapter.notifyDataSetChanged();

    final FloatingActionButton additembutton = findViewById(R.id.floatingActionButton);
    additembutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent additem = new Intent(ListView_Activity.this, Create_Item_Activity.class);
            startActivityForResult(additem, 1);
        }
    });

Then data retrieved here outside of oncreate;

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == ADD_NEW_ITEM){
            if (resultCode == Activity.RESULT_OK) {
                tempdescr = data.getStringExtra("tempdescr");
                tempname = data.getStringExtra("tempname");
                mylistarray.add(tempdescr);
                mylistarray.add(tempname);
                myadapter.notifyDataSetChanged();


            }
            if (resultCode == Activity.RESULT_CANCELED) {
                Toast replacewithcode = Toast.makeText(ListView_Activity.this, "replace with code", Toast.LENGTH_SHORT);
                replacewithcode.show();
            }
        }
    }
Ravi
  • 881
  • 1
  • 9
  • 23
Adam
  • 13
  • 4

2 Answers2

1

You're losing the array reference. Create the array as a global variable and add elements directly. Check this out: ArrayAdapter.NotifyDataSetChanged() is not working?

Ana
  • 11
  • 2
  • Looked at the link, I presume the global variable needs to be built in a singleton class? – Adam Dec 20 '17 at 14:50
  • No, just on top of your activity. Something like this: – Ana Dec 20 '17 at 14:51
  • The suggested line is ArrayList myArrayList=new ArrayList(); and my current one is ArrayListmylistarray = new ArrayList<>(); so I dont see the difference in my array and the one suggested? – Adam Dec 20 '17 at 14:55
0

make sure of two things:

1 does ADD_NEW_ITEM equals 1?

2 did you setResult(RESULT_OK, intent); before you finish it(Create_Item_Activity)?

Relish Wang
  • 385
  • 1
  • 8
  • Yes for add new item it is: static final int ADD_NEW_ITEM = 1; – Adam Dec 20 '17 at 15:05
  • Yes in the intent in "create item activity" I have: setResult(Activity.RESULT_OK,returnentry); within the – Adam Dec 20 '17 at 15:06
  • that's odd. Because your code above seems no error. It can work. – Relish Wang Dec 20 '17 at 15:13
  • well, did you declare the `myadapter` out of the `onCreate` method? – Relish Wang Dec 20 '17 at 15:15
  • when I remove the notifyDataSetChanged() from my onActivityResult the code does work, it happily passes the 2 x data fields from one activity to another and updates the array, and displays the values in the listview, I am not sure how it is refreshing the array without this line in, but it is doing it somehow?! – Adam Dec 20 '17 at 15:19
  • That's impossible. I cannot update the array without `notifyDataSetChanged`. There must be some mistakes.Mmmmmm......But anyway, you solve the problem. :D – Relish Wang Dec 20 '17 at 15:27