如何将数据从适配器发送到fragmnet。
我在这里使用Recyclerview显示我的联系人列表,所以这是适配器代码
RecyclerView适配器代码:
public class contactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<contactModel> getdetails =new ArrayList<contactModel>( );
Context con;
LayoutInflater layoutInflater;
Sendcontact sendcontact;
public contactAdapter(Context context, ArrayList<contactModel> getdetails) {
this.con = context;
this.getdetails = getdetails;
layoutInflater = LayoutInflater.from( con);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = layoutInflater.inflate( R.layout.contactlist, viewGroup, false );
contactLayout layout = new contactLayout(view );
return layout;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
contactLayout layout = (contactLayout) viewHolder;
final contactModel details = getdetails.get( i );
layout.contactname.setText( " "+details.getContactname() );
layout.contactnum.setText( " "+details.getContactnumber() );
layout.favcheck.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
String status = "true";
String id = details.contactid;
String favname = details.contactname;
String favnum = details.contactnumber;
sendcontact.OnItemclicked(id);
}else{
String status = "false";
String id = details.contactid;
String favname = details.contactname;
String favnum = details.contactnumber;
}
}
} );
}
@Override
public int getItemCount() {
return getdetails.size();
}
public interface Sendcontact{
void OnItemclicked(String id);
}
private class contactLayout extends RecyclerView.ViewHolder{
TextView contactname, contactnum;
CheckBox favcheck;
public contactLayout(View view) {
super(view);
contactname = (TextView) view.findViewById( R.id.name );
contactnum = (TextView) view.findViewById( R.id.phonenumber );
favcheck = (CheckBox) view.findViewById( R.id.favcheck );
}
}
}
这是我的第一个片段视图。因此,每当我选中复选框时,数据都应发送到下一个片段。
这是我下一个片段中的代码
最喜欢的片段类:
public class Favorite extends Fragment {
public Favorite() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate( R.layout.fragment_favorite, container, false );
return view;
}
contactAdapter.Sendcontact sendcontact = new contactAdapter.Sendcontact() {
@Override
public void OnItemclicked(String id) {
Log.e( TAG, id );
}
};
}
答案 0 :(得分:0)
添加到适配器回调界面:
private OnItemClickListener listener;
public interface OnItemClickListener {
void onItemChecked(ContactModel model);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
并使用该侦听器:
layout.favcheck.setOnCheckedChangeListener( new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(listener != null)
listener.onItemChecked(details)
}
} );
有两种方法:
在第二个片段中实现此侦听器:
public class Contacts extends Fragment implements contactAdapter.OnItemClickListener{
...
//assign listener to instance of adapter here
// use
@Override
onItemChecked(ContactModel model){
//send data to second fragment here
}
...
}
在第一个片段中实现此侦听器:
public class Contacts extends Fragment implements contactAdapter.OnItemClickListener{
...
adapter.setOnItemClickListener(this)
@Override
onItemChecked(ContactModel model){
//send data to second fragment here
}
...
}
答案 1 :(得分:0)
1。制作一个监听器类
public interface MyOnItemClickListener {
void onItemClick(View v, int position);
}
2。在要发送更新的片段中创建侦听器的对象
public class Favorite extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
.
.
MyOnItemClickListener myOnItemClickListener=new MyOnItemClickListener() {
@Override
public void onItemClick(View v, int position) {
selected_validity=position;
//do whatever you want here
}
};
contactAdapter ContactAdapter = new contactAdapter(your_list,this,myOnItemClickListener);
yourRecyclerview.setAdapter(ContactAdapter);
}
}
3。按如下所示更改适配器:
public class contactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
MyOnItemClickListener listener;
ArrayList<contactModel> getdetails =new ArrayList<contactModel>( );
Context con;
LayoutInflater layoutInflater;
public contactAdapter(ArrayList<contactModel> getdetails, Context context,MyOnItemClickListener listener ) {
this.con = context;
this.getdetails = getdetails;
layoutInflater = LayoutInflater.from( con);
this.listener = listener;
}
private class contactLayout extends RecyclerView.ViewHolder{
TextView contactname, contactnum;
CheckBox favcheck;
public contactLayout(View view) {
super(view);
contactname = (TextView) view.findViewById( R.id.name );
contactnum = (TextView) view.findViewById( R.id.phonenumber );
favcheck = (CheckBox) view.findViewById( R.id.favcheck );
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
Toast.makeText(context,"pos: "+getLayoutPosition(),Toast.LENGTH_SHORT).show();
mItemClickListener.onItemClick(v, getLayoutPosition());
notifyDataSetChanged();
}
}
}
}