将值从活动传递到适配器

时间:2019-08-12 03:30:42

标签: java android android-intent adapter android-arrayadapter

我有活动A和活动B。活动B使用回收视图Adpater显示信息列表。从活动A,我将意图发送到活动B,该活动用于工具栏标题。 但是我也需要在Adapter类中使用相同的意图。

因此,我如何将活动A中的意图值传递给B,并在适配器类中也使用相同的意图。

我的活动A有代码

Intent new = new Intent(itemView.getContext(), B.class);
                phy.putExtra("key","This is title");   itemView.getContext().startActivity(new);

在活动B中,我有

   getSupportActionBar().setTitle(getIntent().getStringExtra("key"));

我也想要适配器类中的键值。

活动B的代码为

public class RoomlistActivity extends AppCompatActivity {

    DatabaseReference dbreference,dbref;
    RecyclerView rv;
    ArrayList<Roompost> list;
    private RoomAdapter staggeredBooksAdapter;
    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_books);
        



        list = new ArrayList<>();
        staggeredBooksAdapter = new RoomAdapter(list);

        rv = (RecyclerView) findViewById(R.id.recyclerView);




        rv.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);
        rv.setAdapter(staggeredBooksAdapter);





}

我的适配器类具有

public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.MyViewHolder> {
    ArrayList<Roompost> bookslist;
    CardView cv;
    Roompost g;


    public RoomAdapter(ArrayList<Roompost> bookslist){
        this.bookslist = bookslist;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout,parent,false);
        return new MyViewHolder(v);

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView bookName,sellername,profile_details,update_info;
        Button mSolved;
        ImageView iv;


        MyViewHolder(final View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.my_card_view);

  

            update_info =(TextView) itemView.findViewById(R.id.update_info);

        }
    }




    @Override
    public int getItemCount() {
        return bookslist.size();
    }
}

谢谢。

2 个答案:

答案 0 :(得分:4)

只需在适配器中再添加1个参数并传递标题

public RoomAdapter(ArrayList<Roompost> bookslist, String title){
        this.bookslist = bookslist;
        this.title = title;
}

在活动B onCreate

String title = getIntent().getStringExtra("key");
staggeredBooksAdapter = new RoomAdapter(list, title);

答案 1 :(得分:1)

您可以像这样通过构造函数调用将Activity中的值传递给Adapter:

public class RoomlistActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ... 
        staggeredBooksAdapter = new RoomAdapter(list, getIntent().getStringExtra("key"));
       ... 
       }
       ... 
}

您的适配器应具有如下构造函数:

public class RoomAdapter extends RecyclerView.Adapter<RoomAdapter.MyViewHolder> {
    ArrayList<Roompost> bookslist;
    CardView cv;
    Roompost g;
    String key;

    public RoomAdapter(ArrayList<Roompost> bookslist, String key){
        this.bookslist = bookslist;
        this.key = key;
    }
   ... 
}
``