Android – 如何在不使用Intent的情况下发送一个ArrayList?

huangapple go评论71阅读模式
英文:

Android - How to send an ArrayList without using Intent?

问题

我有一个 Activity 和一个 Adapter 类。在我的 Adapter 类中,我有一个 ArrayList,我想在 Activity 类中使用它。并且 Activity 类的 Recycler view 附加到 Adapter 类,所以我不能使用 intent 来发送它。所以,有没有其他方法可以访问这个 ArrayList

英文:

I have an Activity and an Adapter Class. In my Adapter class, I have an ArrayList that I want to use in the Activity class. And Recycler view of Activity class is attached to the adapter class So, I cannot use intent to send it. So, Is there any other way I can access the ArrayList?

答案1

得分: 1

  1. 就我的观点而言,你有三种方法可以做到这一点:
  2. 将你的 ArrayList 移动到 ViewModel,然后将它传递给你的适配器或在活动中使用它。
  3. 将 ArrayList 放在活动中,然后将它传递给适配器。
  4. 在你的适配器中创建一个 getter 方法,并在活动中使用它。
英文:

In my opinion, you have three ways to do it:

  1. move your ArrayList to ViewModel, then pass it to your adapter or use it in the activity.
  2. place ArrayList just in the activity and pass it to the adapter.
  3. create a getter in your adapter and use it in the activity.

答案2

得分: 1

你可以按照以下方式创建适配器 -

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.CustomViewModel> {

    private Context context;
    private List<String> stringList;

    public RecycleViewAdapter(Context context, List<String> stringList) {
        this.context = context;
        this.stringList = stringList;
    }

    @NonNull
    @Override
    public CustomViewModel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CustomViewModel(LayoutInflater.from(context).inflate(R.layout.recycle_view_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewModel holder, int position) {
        //放置你的绑定代码
    }

    @Override
    public int getItemCount() {
        if (stringList!= null)
            return stringList.size();
        else
            return 0;
    }

    //适配器的getter方法返回字符串列表
    public List<String> getStringList() {
        return stringList;
    }

    public class CustomViewModel extends RecyclerView.ViewHolder {
        //这里放你的内部类代码
    }
}

现在你可以像下面这样从你的适配器中获取 List

recycleViewAdapter.getStringList();

愉快编码!

英文:

You can create your adapter as follow -

public class RecycleViewAdapter extends RecyclerView.Adapter&lt;RecycleViewAdapter.CustomViewModel&gt; {

    private Context context;
    private List&lt;String&gt; stringList;

    public RecycleViewAdapter(Context context, List&lt;String&gt; stringList) {
        this.context = context;
        this.stringList = stringList;
    }

    @NonNull
    @Override
    public CustomViewModel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CustomViewModel(LayoutInflater.from(context).inflate(R.layout.recycle_view_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewModel holder, int position) {
        //put your code of binding of code
    }

    @Override
    public int getItemCount() {
        if (stringList!= null)
            return stringList.size();
        else
            return 0;
    }

    //getter method of Adpater to return string
    public List&lt;String&gt; getStringList() {
        return stringList;
    }

    public class CustomViewModel extends RecyclerView.ViewHolder {
        //your inner class code here
    }
}

Now you can get List<String> from your adapter as follows-

recycleViewAdapter.getStringList();

Happy coding !

huangapple
  • 本文由 发表于 2020年8月9日 18:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63325288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定