简体   繁体   中英

RecyclerView - Use an Arraylist of second activity as data

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 Edidtext and 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.

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"); 
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM