简体   繁体   English

在另一个 Activity 中使用 RecyclerViewAdapter 的 ArrayList

[英]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.我正在尝试通过另一个活动使用创建并填写在 Recyclerviewadapter 中的 Arraylist 到 go。 I tried using Intent, Getters, matching one Arraylist to another and nothing works for me.我尝试使用 Intent、Getters,将一个 Arraylist 与另一个匹配,但对我没有任何作用。

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:我在其中填充 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:我想在其中游览 Arraylist 的 Activitylenguajes 代码:

            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.这是我运行 Activitylenguajes 时显示的图像。 Contains a fragment in which I show the list of languages as can be observed包含一个片段,我在其中显示可以观察到的语言列表

https://i.postimg.cc/v8Pwxs17/lenguajes.png 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.当您在 LanguagesActivity 中按下一个按钮时会显示此警报,当您单击 OK 时它会转到 LoginActivity。

https://i.postimg.cc/rwdn6cpj/seleccionar.png 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.]我想要的是将 ArrayList (此时其大小为 3)传递给作为其上下文的 LanguagesActivity,以便在我单击 LanguagesActivity 本身的 Z99938282F04071859941E 按钮时能够通过它 go]。

https://i.postimg.cc/ZKWFnHhK/debugger.png 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.为了做同样的事情,但使用语言,我使用了 Listview,它对我来说是正确的,但在这种情况下,我需要使用 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() :onCreate()LoginActivity中:

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'


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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