简体   繁体   中英

Use ArrayList of a RecyclerViewAdapter in another Activity

I'm trying to use an Arraylist created and filled in a Recyclerviewadapter, to go through another Activity. I tried using Intent, Getters, matching one Arraylist to another and nothing works for me.

Code of the adapter:

public class MyLenguajeRecyclerViewAdapter extends RecyclerView.Adapter<MyLenguajeRecyclerViewAdapter.ViewHolder> {

private final List<Lenguaje> lenguajeList;
private final OnLenguajeInteractionListener mListener;
private Context ctx;
final ArrayList<String> lenguajes = new ArrayList<>();


public MyLenguajeRecyclerViewAdapter(Context context, List<Lenguaje> items, OnLenguajeInteractionListener listener) {
    ctx = context;
    lenguajeList = items;
    mListener = listener;
}

Code inside the adapter in which I fill the Arraylist:

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.mItem = lenguajeList.get(position);
    holder.textViewNombre.setText(holder.mItem.getTitulo());
    holder.logo.setImageResource(holder.mItem.getIdImagen());
    holder.checkBoxLenguaje.setChecked(holder.mItem.isChecked());


    if (position % 2 == 0) {
        holder.mView.setBackgroundColor(ContextCompat.getColor(ctx, R.color.azulLigero));
    } else {
        holder.mView.setBackgroundColor(ContextCompat.getColor(ctx, R.color.azulCeleste));
    }


    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            CheckBox cb = v.findViewById(R.id.checkBoxLenguaje);
            cb.setChecked(!cb.isChecked());
            TextView tv = v.findViewById(R.id.textViewLenguaje);

            if (cb.isChecked()) {
                if (!lenguajes.contains(tv.getText().toString())) {
                    lenguajes.add(tv.getText().toString());
                }
            } else if (!cb.isChecked()) {
                if (lenguajes.contains(tv.getText().toString())) {
                    lenguajes.remove(tv.getText().toString());                      
                }
            }
        }
    });
}

Activitylenguajes code in which I want to tour the Arraylist:

            Empleado emp = new Programador(nombre, email, password, genero, fechaNacimiento, lenguajes);

            //for (int i = 0; i < lenguajes.size(); i++) {
            AlertDialog.Builder ad = new AlertDialog.Builder(LenguajesActivity.this);
            ad.setTitle("Lenguajes elegidos");
            ad.setMessage(emp.toString());
            ad.setCancelable(true);
            ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(LenguajesActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
            });
            ad.show();
        }
        //}

This is the image shown when I run the Activitylenguajes. Contains a fragment in which I show the list of languages as can be observed

https://i.postimg.cc/v8Pwxs17/lenguajes.png

And in this I show the alert in which I show the data of the registered programmer and its supposed languages. This alert is displayed when you press a button in LanguagesActivity, and when you click OK it goes to LoginActivity.

https://i.postimg.cc/rwdn6cpj/seleccionar.png

I add capture of the Debugger at the moment I am selecting the languages.

What I want is to pass that ArrayList (which at that moment has a size of 3), to the LanguagesActivity, which is its Context, to be able to go through it when I click on the select button of LanguagesActivity itself.]

https://i.postimg.cc/ZKWFnHhK/debugger.png

To do the same but with languages, I used a Listview and it works for me correctly, but in this case I need to use the Recyclerview.

Thank you.

Change the list to a string array and pass the string array, after you are done:

ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override

public void onClick(DialogInterface dialog, int which) {

//change list to a string array
String[] array = new String[lenguajes.size()];
for(int i=0 ; i<lenguajes.size() ; i++){
array[i] = lenguajes.get(i);
}


//open new activity and pass the array
Intent intent = new Intent(LenguajesActivity.this, LoginActivity.class);
intent.putExtra("array", array);
startActivity(intent);

}
});

In the LoginActivity in onCreate() :

class LoginActivity .........{

private ArrayList<String> lenguajes = new ArrayList<>();

//in onCreate() method:

//get the passed string array
String[] array = getIntent().getStringArrayExtra("array");

//change string array back to a list
for(String s : array){
lenguajes.add(s);
}

//at this point its like you just passed 'lenguajes'


}

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