0

I have a Recyclerview wich is populating the data with a resource file that contains an Arraylist.

I have a second activity which contains an Edidtextand a Button that inserts values to another Arraylist set in this second activity.

What I need is to use the Arraylist on the second activity to populate the Recyclerview instead using the data of the resource file.

I've tried to pass the Arraylist of one activity to another using Intent but didn't get succeed.

ArrayList in the ViewHolder

public ListAdapter(Context context) {
    mContext = context;

    final String[] countryNames = context.getResources().getStringArray(R.array.country_names);
    mItems = new ArrayList<>();

    for (int i = 0; i < countryNames.length; i++) {
        mItems.add(new LineItem(countryNames[i]));
    }
}

Arraylist in the second activity that I want to use as data

public class DisplayDescActivity extends AppCompatActivity {

    private EditText descItem;
    public static ArrayList<String> descList = new ArrayList<String>();
...
}

Is there a way to do that?

Intent code tried:

  • DisplayDescActivity code:
    On the protected void onCreate(Bundle savedInstanceState) i've used:

    Intent intent1 = new Intent(this, ListAdapter.class);
    intent1.putStringArrayListExtra("list", descLista);
    startActivity(intent1);
    
  • ListAdapter code:
    I've tried to use the following code

    final ArrayList<String> resultArray = getIntent().getStringArrayListExtra("list");
    

But the massage Cannot resolve method'getIntent()' appears.

RooseveltJr
  • 71
  • 1
  • 10
  • You should pass it via Intent, why you didnt succeed? can you post the code you tried? – Nanoc Nov 25 '15 at 10:48
  • @Nanoc I've updated with the Intent code. Thanks in advance. – RooseveltJr Nov 25 '15 at 11:10
  • 1
    I dont know form where you are trying to call getIntent, but you need the activity reference, probably you are inside a fragment? then use getActivity().getIntent() – Nanoc Nov 25 '15 at 11:13
  • @Nanoc Actually, I don't know where to call getIntent. I don't have a fragment in my code. Here is the ViewHolder code: [link](https://gist.github.com/jorel89/105f65a0ea567d9f8ad6) , where should I put it? – RooseveltJr Nov 25 '15 at 11:36
  • 1
    As i see in your code you should be able to do ((Activity)mContext).getIntent() – Nanoc Nov 25 '15 at 11:41
  • @Nanoc it seems to work!! Thanks, but now I'm gettin error because i've passed an `ArrayList` and I need to make it an `Array`. – RooseveltJr Nov 25 '15 at 12:12
  • @Nanoc I've update the code according to the [New code](https://gist.github.com/jorel89/569f9c223b6cf08c98d4) but now i'm getting the following exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.junio.testearray3/com.example.junio.testearray3.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity – RooseveltJr Nov 25 '15 at 12:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96149/discussion-between-nanoc-and-rooseveltjr). – Nanoc Nov 25 '15 at 13:03

1 Answers1

0

You have to initialize the list in activity's onCreate event.

public class DisplayDescActivity extends AppCompatActivity {

    private EditText descItem;
    public static ArrayList<String> descList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        descList = getIntent().getStringArrayListExtra("list"); 
    }

}
Christian
  • 6,334
  • 9
  • 48
  • 76