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

我有 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;

}




@Override

public ItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

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

    ItemRowHolder mh = new ItemRowHolder(v);

    return mh;

}





@Override

public 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");

        }

    });






}




@Override

public int getItemCount() {

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

}



梦里花落0921
浏览 98回答 2
2回答

缥缈止盈

用这个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,&nbsp; List<SingleItemforPhoneNumbers> itemforPhoneNumbers , List<SingleItemModelForUpcoming> itemsList ) {&nbsp; &nbsp; this.itemsList = itemsList;&nbsp; &nbsp; this.item = item;&nbsp; &nbsp; this.itemforPhoneNumbers = itemforPhoneNumbers;&nbsp; &nbsp; this.mContext = context;}@Overridepublic SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {&nbsp; &nbsp; View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.upcomingclientlistlayout, null);&nbsp; &nbsp; SingleItemRowHolder mh = new SingleItemRowHolder(v);&nbsp; &nbsp; return mh;}@Overridepublic void onBindViewHolder(SingleItemRowHolder holder, int i) {&nbsp; &nbsp; SingleItemModelForUpcoming singleItem = itemsList.get(i);&nbsp; &nbsp;SingleItemforPhoneNumbers singleItemforPhoneNumbers = itemforPhoneNumbers.get(i);&nbsp; &nbsp; holder.nameofclient.setText(singleItem.getName());&nbsp; &nbsp; holder.phoneNumber.setText(singleItemforPhoneNumbers.getPhoneNumber());}@Overridepublic int getItemCount() {&nbsp; &nbsp; return (null != itemsList ? itemsList.size() : 0);}public class SingleItemRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {&nbsp; &nbsp; protected TextView nameofclient , phoneNumber, date;&nbsp; &nbsp; public SingleItemRowHolder(View view) {&nbsp; &nbsp; &nbsp; &nbsp; super(view);&nbsp; &nbsp; &nbsp; &nbsp; this.nameofclient = (TextView) view.findViewById(R.id.nameofclient);&nbsp; &nbsp; &nbsp; &nbsp; this.phoneNumber = (TextView)view.findViewById(R.id.phoneNumber);&nbsp; &nbsp; &nbsp; &nbsp; view.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpcomingObject up = item.get(getAdapterPosition);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent UpcomingDetailPage = new Intent(v.getContext(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; com.allwaseet.spaservshop.UpcomingDetailsPage.UpcomingDetailPage.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v.getContext().startActivity(UpcomingDetailPage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String finalPhoneNumber = phoneNumber.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences.Editor PhoneNumberEditor;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences PhoneNumberSharedPreference;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PhoneNumberSharedPreference = mContext.getSharedPreferences("SelectedPhoneNumber", Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PhoneNumberEditor = PhoneNumberSharedPreference.edit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PhoneNumberEditor.putString("SelectedPhoneNumber",finalPhoneNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PhoneNumberEditor.commit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater inflater = LayoutInflater.from(mContext);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.upcominglayout, null, true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView textView = (TextView)view.findViewById( R.id.date );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String finaldate = textView.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("ss","date&nbsp; &nbsp; &nbsp;is "+finaldate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences.Editor DateEditor;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharedPreferences DateSharedPreference;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateSharedPreference = mContext.getSharedPreferences("DateInUpcoming", Context.MODE_PRIVATE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateEditor = DateSharedPreference.edit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateEditor.putString("DateInUpcoming",finaldate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DateEditor.commit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // return view;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }}

慕工程0101907

您可以使用Intent在按钮或任何项目的单击事件上将数据从一个类传递到另一个类,因此您可以将第一类的数据存储到共享首选项中,然后以键值对的形式传递数据,如下所示:public class AdapterForNamesListInUpcoming extends&nbsp;RecyclerView.Adapter<AdapterForNamesListInUpcoming.SingleItemRowHolder> {private List<SingleItemModelForUpcoming> itemsList;private List<SingleItemforPhoneNumbers> itemforPhoneNumbers;private ArrayList<UpcomingObject> item;private Context ctx;public AdapterForNamesListInUpcoming(Context&nbsp;context,ArrayList<UpcomingObject> item,&nbsp; List<SingleItemforPhoneNumbers>&nbsp;itemforPhoneNumbers , List<SingleItemModelForUpcoming> itemsList ) {this.itemsList = itemsList;this.item = item;this.itemforPhoneNumbers = itemforPhoneNumbers;this.ctx = context;}@Overridepublic SingleItemRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {View v =&nbsp;LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.upcomingclientlistlayout, null);SingleItemRowHolder rh = new SingleItemRowHolder(v);return rh;}@Overridepublic void onBindViewHolder(SingleItemRowHolder holder, int i) {&nbsp;SingleItemModelForUpcoming singleItem = itemsList.get(i);SingleItemforPhoneNumbers singleItemforPhoneNumbers =&nbsp;itemforPhoneNumbers.get(i);holder.nameofclient.setText(singleItem.getName());holder.phoneNumber.setText(singleItemforPhoneNumbers.getPhoneNumber());}@Overridepublic int getItemCount() {return (null != itemsList ? itemsList.size() : 0);&nbsp;}public class SingleItemRowHolder extends RecyclerView.ViewHolder implements&nbsp;View.OnClickListener {protected TextView nameofclient , phoneNumber, date;public SingleItemRowHolder(View view) {&nbsp; &nbsp; super(view);&nbsp; &nbsp; this.nameofclient = (TextView) view.findViewById(R.id.nameofclient);&nbsp; &nbsp; this.phoneNumber = (TextView)view.findViewById(R.id.phoneNumber);&nbsp; &nbsp; view.setOnClickListener(this);&nbsp;}&nbsp;@Override&nbsp;public void onClick(View v) {&nbsp; &nbsp; // get data of first adapter&nbsp; &nbsp; UpcomingObject up = item.get(getAdapterPosition);&nbsp; &nbsp; Intent UpcomingDetailPage = new Intent(v.getContext(),&nbsp;&nbsp; com.allwaseet.spaservshop.UpcomingDetailsPage.UpcomingDetailPage.class);&nbsp; &nbsp; v.getContext().startActivity(UpcomingDetailPage);&nbsp; &nbsp; String finalPhoneNumber = phoneNumber.getText().toString();&nbsp; &nbsp; Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);&nbsp; &nbsp; SharedPreferences.Editor PhoneNumberEditor;&nbsp; &nbsp; SharedPreferences PhoneNumberSharedPreference;&nbsp; &nbsp; PhoneNumberSharedPreference =&nbsp;&nbsp;ctx.getSharedPreferences("SelectedPhoneNumber", Context.MODE_PRIVATE);&nbsp; &nbsp; PhoneNumberEditor = PhoneNumberSharedPreference.edit();&nbsp; &nbsp; PhoneNumberEditor.putString("SelectedPhoneNumber",finalPhoneNumber);&nbsp; &nbsp; PhoneNumberEditor.commit();&nbsp; &nbsp; Log.d("PhoneNumber from databse ","phone Number is "+finalPhoneNumber);&nbsp; &nbsp; LayoutInflater inflater = LayoutInflater.from(ctx);&nbsp; &nbsp; View view = inflater.inflate(R.layout.upcominglayout, null, true);&nbsp; &nbsp; TextView textView = (TextView)view.findViewById( R.id.date );&nbsp; &nbsp; String finaldate = textView.getText().toString();&nbsp; &nbsp; Log.d("ss","date&nbsp; &nbsp; &nbsp;is "+finaldate);&nbsp; &nbsp; SharedPreferences.Editor DateEditor;&nbsp; &nbsp; SharedPreferences DateSharedPreference;&nbsp; &nbsp; DateSharedPreference = ctx.getSharedPreferences("DateInUpcoming",&nbsp;&nbsp; &nbsp; Context.MODE_PRIVATE);&nbsp; &nbsp; DateEditor = DateSharedPreference.edit();&nbsp; &nbsp; DateEditor.putString("DateInUpcoming",finaldate);&nbsp; &nbsp; DateEditor.commit();&nbsp; &nbsp;// return view;&nbsp; &nbsp; }&nbsp; }&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java