为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用其他类的单击方法传递一个类的值

如何使用其他类的单击方法传递一个类的值

梦里花落0921 2024-01-17 21:07:08
我有 2 个适配器类及其对象类:第一个适配器类是:public class UpcomingAdpter extends  RecyclerView.Adapter<UpcomingAdpter.ItemRowHolder>  {private ArrayList<UpcomingObject> itemList;private Context context;public UpcomingAdpter(ArrayList<UpcomingObject> itemList, Context context){    this.itemList = itemList;    this.context = context;}@Overridepublic ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.upcominglayout, null);    ItemRowHolder mh = new ItemRowHolder(v);    return mh;}@Overridepublic void onBindViewHolder(ItemRowHolder itemRowHolder, int i) {    final String sectionName = itemList.get(i).getDate();    final ArrayList<SingleItemModelForUpcoming> singleSectionItems = itemList.get(i).getNamesList();    final ArrayList<SingleItemforPhoneNumbers> singleSectionItemsForPhoneNumber = itemList.get(i).getPhoneList();    itemRowHolder.date.setText(sectionName);    AdapterForNamesListInUpcoming itemListDataAdapter = new AdapterForNamesListInUpcoming(context, singleSectionItemsForPhoneNumber, singleSectionItems);   AdapterForNamesListInUpcoming itemListDataAdapterSecond = new AdapterForNamesListInUpcoming(context, singleSectionItemsForPhoneNumber, singleSectionItems);    itemRowHolder.recycler_view_list.setHasFixedSize(true);    itemRowHolder.recycler_view_list.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));    itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapter);   itemRowHolder.recycler_view_list.setAdapter(itemListDataAdapterSecond);    itemRowHolder.recycler_view_list.setNestedScrollingEnabled(false);    itemRowHolder.recycler_view_list.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            Log.d("dd","here is me");        }    });}@Overridepublic int getItemCount() {    return (null != itemList ? itemList.size() : 0);}
查看完整描述

2 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

用这个


public class AdapterForNamesListInUpcoming extends RecyclerView.Adapter<AdapterForNamesListInUpcoming.SingleItemRowHolder> {


private List<SingleItemModelForUpcoming> itemsList;

private List<SingleItemforPhoneNumbers> itemforPhoneNumbers;

private ArrayList<UpcomingObject> item;

private Context mContext;



public AdapterForNamesListInUpcoming(Context context,ArrayList<UpcomingObject> item,  List<SingleItemforPhoneNumbers> itemforPhoneNumbers , List<SingleItemModelForUpcoming> itemsList ) {

    this.itemsList = itemsList;

    this.item = item;

    this.itemforPhoneNumbers = itemforPhoneNumbers;

    this.mContext = context;

}



@Override

public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.upcomingclientlistlayout, null);

    SingleItemRowHolder mh = new SingleItemRowHolder(v);

    return mh;


}


@Override

public void onBindViewHolder(SingleItemRowHolder holder, int i) {


    SingleItemModelForUpcoming singleItem = itemsList.get(i);


   SingleItemforPhoneNumbers singleItemforPhoneNumbers = itemforPhoneNumbers.get(i);


    holder.nameofclient.setText(singleItem.getName());


    holder.phoneNumber.setText(singleItemforPhoneNumbers.getPhoneNumber());

}


@Override

public int getItemCount() {

    return (null != itemsList ? itemsList.size() : 0);

}


public class SingleItemRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


    protected TextView nameofclient , phoneNumber, date;

    public SingleItemRowHolder(View view) {

        super(view);


        this.nameofclient = (TextView) view.findViewById(R.id.nameofclient);

        this.phoneNumber = (TextView)view.findViewById(R.id.phoneNumber);

        view.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                      UpcomingObject up = item.get(getAdapterPosition);


                      Intent UpcomingDetailPage = new Intent(v.getContext(),          com.allwaseet.spaservshop.UpcomingDetailsPage.UpcomingDetailPage.class);

                      v.getContext().startActivity(UpcomingDetailPage);

                      String finalPhoneNumber = phoneNumber.getText().toString();

                      Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);


                      SharedPreferences.Editor PhoneNumberEditor;

                      SharedPreferences PhoneNumberSharedPreference;

                      PhoneNumberSharedPreference = mContext.getSharedPreferences("SelectedPhoneNumber", Context.MODE_PRIVATE);

                      PhoneNumberEditor = PhoneNumberSharedPreference.edit();

                          PhoneNumberEditor.putString("SelectedPhoneNumber",finalPhoneNumber);

            PhoneNumberEditor.commit();


                      Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);


                      LayoutInflater inflater = LayoutInflater.from(mContext);

                      View view = inflater.inflate(R.layout.upcominglayout, null, true);

                      TextView textView = (TextView)view.findViewById( R.id.date );

                      String finaldate = textView.getText().toString();

                      Log.d("ss","date     is "+finaldate);


                      SharedPreferences.Editor DateEditor;

                      SharedPreferences DateSharedPreference; 

                      DateSharedPreference = mContext.getSharedPreferences("DateInUpcoming", Context.MODE_PRIVATE);

                      DateEditor = DateSharedPreference.edit();

                      DateEditor.putString("DateInUpcoming",finaldate);

                      DateEditor.commit();

                      // return view;

                  }

                });

        }

      }

}



查看完整回答
反对 回复 2024-01-17
?
慕工程0101907

TA贡献1887条经验 获得超5个赞

您可以使用Intent在按钮或任何项目的单击事件上将数据从一个类传递到另一个类,因此您可以将第一类的数据存储到共享首选项中,然后以键值对的形式传递数据,如下所示:


public class AdapterForNamesListInUpcoming extends 

RecyclerView.Adapter<AdapterForNamesListInUpcoming.SingleItemRowHolder> {


private List<SingleItemModelForUpcoming> itemsList;

private List<SingleItemforPhoneNumbers> itemforPhoneNumbers;

private ArrayList<UpcomingObject> item;

private Context ctx;



public AdapterForNamesListInUpcoming(Context 

context,ArrayList<UpcomingObject> item,  List<SingleItemforPhoneNumbers> 

itemforPhoneNumbers , List<SingleItemModelForUpcoming> itemsList ) {

this.itemsList = itemsList;

this.item = item;

this.itemforPhoneNumbers = itemforPhoneNumbers;

this.ctx = context;

}



@Override

public SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View v = 

LayoutInflater.from(viewGroup.getContext())

.inflate(R.layout.upcomingclientlistlayout, null);

SingleItemRowHolder rh = new SingleItemRowHolder(v);

return rh;


}


@Override

public void onBindViewHolder(SingleItemRowHolder holder, int i) {


 SingleItemModelForUpcoming singleItem = itemsList.get(i);


SingleItemforPhoneNumbers singleItemforPhoneNumbers = 

itemforPhoneNumbers.get(i);


holder.nameofclient.setText(singleItem.getName());


holder.phoneNumber.setText(singleItemforPhoneNumbers.getPhoneNumber());

}


@Override

public int getItemCount() {

return (null != itemsList ? itemsList.size() : 0);

 }


public class SingleItemRowHolder extends RecyclerView.ViewHolder implements 

View.OnClickListener {


protected TextView nameofclient , phoneNumber, date;

public SingleItemRowHolder(View view) {

    super(view);


    this.nameofclient = (TextView) view.findViewById(R.id.nameofclient);

    this.phoneNumber = (TextView)view.findViewById(R.id.phoneNumber);

    view.setOnClickListener(this);

 }


 @Override

 public void onClick(View v) {

    // get data of first adapter

    UpcomingObject up = item.get(getAdapterPosition);


    Intent UpcomingDetailPage = new Intent(v.getContext(), 

  com.allwaseet.spaservshop.UpcomingDetailsPage.UpcomingDetailPage.class);

    v.getContext().startActivity(UpcomingDetailPage);

    String finalPhoneNumber = phoneNumber.getText().toString();

    Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);


    SharedPreferences.Editor PhoneNumberEditor;

    SharedPreferences PhoneNumberSharedPreference;


    PhoneNumberSharedPreference = 

 ctx.getSharedPreferences("SelectedPhoneNumber", Context.MODE_PRIVATE);

    PhoneNumberEditor = PhoneNumberSharedPreference.edit();

    PhoneNumberEditor.putString("SelectedPhoneNumber",finalPhoneNumber);

    PhoneNumberEditor.commit();


    Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);


    LayoutInflater inflater = LayoutInflater.from(ctx);

    View view = inflater.inflate(R.layout.upcominglayout, null, true);

    TextView textView = (TextView)view.findViewById( R.id.date );

    String finaldate = textView.getText().toString();

    Log.d("ss","date     is "+finaldate);


    SharedPreferences.Editor DateEditor;

    SharedPreferences DateSharedPreference;



    DateSharedPreference = ctx.getSharedPreferences("DateInUpcoming", 

    Context.MODE_PRIVATE);

    DateEditor = DateSharedPreference.edit();

    DateEditor.putString("DateInUpcoming",finaldate);

    DateEditor.commit();

   // return view;

    }

  }

 }



查看完整回答
反对 回复 2024-01-17
  • 2 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信