我的目标是通过回调从适配器激活Fragment的“ RefreshList()”功能。
但是Fragment文档告诉我照片中的以下消息:
我以为他要我发送回叫,但我不知道它是如何完成的,非常感谢您的帮助
我附上我的片段代码和适配器以防万一 AlertasFragment:
@Override
public void FragmentMethod() {
RefreshList();
}
我使用的方法是:
public class Alerts_Adapter extends RecyclerView.Adapter<Alerts_Adapter.MyViewHolder> {
private String idPet;
private String TipoAnuncio;
private Context mContext;
private List<MyAdsCard> anunciosList;
private StorageReference mStorageReference;
private String idUser;
//llamar funcion de fragment
AdapterCallback callback;
public interface AdapterCallback{
void FragmentMethod();
}
ArrayList<String> ChatIdList;
ArrayList<String> OtherUserIdList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, description, time;
public ImageView thumbnail;
public ImageButton btn_edit;
public TextView et_typeAd;
private TextView type_ad;
private RelativeLayout type_adLayout;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
description = (TextView) view.findViewById(R.id.description);
time = (TextView) view.findViewById(R.id.time);
thumbnail = (ImageView) view.findViewById(R.id.img);
btn_edit = view.findViewById(R.id.btn_edit);
et_typeAd = view.findViewById(R.id.et_typeAd);
type_ad = (TextView) view.findViewById(R.id.et_typeAd);
type_adLayout = view.findViewById(R.id.type_adLayout);
}
}
public Alerts_Adapter(Context mContext, List<MyAdsCard> anunciosList, AdapterCallback callback) {
this.mContext = mContext;
this.anunciosList = anunciosList;
this.callback = callback;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.myads_cardview, parent, false);
ChatIdList = new ArrayList<String>();
OtherUserIdList = new ArrayList<String>();
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
//aquí carga los anuncios, los actualiza con bbdd, las imagenes
final MyAdsCard anuncios = anunciosList.get(position);
idPet=anuncios.getId();//coge id anuncio
TipoAnuncio=anuncios.getTipoAnuncio();//coge tipo anuncio
holder.title.setText(anuncios.getName());
holder.description.setText(anuncios.getDescription());
holder.time.setText(anuncios.getTime());
if(anuncios.getTipoAnuncio().equals("Perdidos")) {
holder.type_ad.setText("PERDIDO");
holder.type_adLayout.setBackgroundResource(R.drawable.title_rounded_orange);
}
else if(anuncios.getTipoAnuncio().equals("Encontrados")){
holder.type_ad.setText("ENCONTRADO");
holder.type_adLayout.setBackgroundResource(R.drawable.title_rounded_green);
}
else if(anuncios.getTipoAnuncio().equals("Adopciones")){
holder.type_ad.setText("ADOPCIÓN");
holder.type_adLayout.setBackgroundResource(R.drawable.title_rounded_blue);
}
holder.thumbnail.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) { //FER INTENT QUE PASSI EL ID PER DESPRES A LA FITXA IMPRIMIR LANUNCI AMB AQUEST ID I TIPO ANUNCIO
//para hacer intent en el onclick
TipoAnuncio=anuncios.getTipoAnuncio();
idPet=anuncios.getId();
Intent intent = new Intent (v.getContext(), DataPetActivity.class);
intent.putExtra("TipoAnuncioFicha", TipoAnuncio);
intent.putExtra("IdAnuncioFicha", idPet);
mContext.startActivity(intent);
}
});
holder.btn_edit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
showPopupMenu(holder.btn_edit);
//agafa dades, i actualitza su pica.
TipoAnuncio=anuncios.getTipoAnuncio();
idPet=anuncios.getId();
}
});
//picasso agafa url i ho converteix amb imatge
Picasso.get()
.load(anuncios.getImg())
.resize(200, 200)
.centerCrop()
.into(holder.thumbnail);
}
/**
* Showing popup menu when tapping on 3 dots
*/
private void showPopupMenu(View view) {
// inflate menu
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_alerts, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
/**
* Click listener for popup menu items
*/
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
public MyMenuItemClickListener() {
}
@SuppressLint("StringFormatMatches")
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
/*
case R.id.action_editAd:
Intent intent = new Intent (mContext, EditDataPetActivity.class);
intent.putExtra("TipoAnuncio", TipoAnuncio);
intent.putExtra("IdAnuncio", idPet);
((MisAnunciosActivity) mContext).startActivityForResult(intent,1);
return true;*/
case R.id.action_deleteAd:
showAlert(R.string.alert_title,String.format(mContext.getString(R.string.alert_delete)), idPet);
//deleteAlerts();
return true;
default:
}
return false;
}
}
private void showAlert(int title, String message, final String idRoom){
android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(this.mContext,R.style.MyDialogTheme)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.chat_alert_button_delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
deleteAlerts();
}
})
.setNegativeButton(R.string.chat_alert_button_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
android.support.v7.app.AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
private void deleteAlerts() {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref2 = rootRef.child("Usuarios").child(userId).child("Alerts").child(idPet);
ref2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dataSnapshot.getRef().removeValue();
Toast.makeText(mContext, "Alerta borrada.", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
/*
//dir que refresqui llista myads SI FUNCIONA POSAR A MAPA I LLISTA QUAN ES CREA UN ANUNCI AMB ADDANUNCIO
final AlertasFragment fragment = new AlertasFragment();
fragment.RefreshList();
//((AlertasFragment)mContext).RefreshList(); */
//mCallBack.CallBackFragment();
/*
if(callback != null) {
callback.FragmentMethod();
}*/
callback.FragmentMethod();
}
@Override
public int getItemCount() {
return anunciosList.size();
}
}
Alerts_Adapter:
callback.FragmentMethod();
我用它来从片段中调用函数:
docRef.update({
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以传递当前的此对象,以实现您的callback
,或者实现匿名类,或者使成为AdapterCallback的对象< / strong>,并按如下所示进行传递:
传递片段对象并实现它。
adapter = new Alerts_Adapter(getActivity(), myAdsObjectList, this);
或创建新的匿名对象。
adapter = new Alerts_Adapter(getActivity(), myAdsObjectList, new AdapterCallback {
@Override
public void FragmentMethod(){
// Here is your callback
}
});
或制作一个AdapterCallback
的对象并像下面那样传递。
adapter = new Alerts_Adapter(getActivity(), myAdsObjectList, adapterCallback);