我有一个RecyclerView适配器,用于用户添加的收藏夹。在对话框片段中填充了recyclerview。
public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteViewHolder> implements ILatLong {
private Context context;
View view;
MainActivity mainActivity;
private List<Favorite> listFavorites;
private SQLiteDB mDatabase;
private int position;
private ILatLong mCallback;
public FavoriteAdapter(ILatLong mCallback){
this.mCallback = mCallback;
}
public FavoriteAdapter(Context context, List<Favorite> listFavorites) {
this.context = context;
this.listFavorites = listFavorites;
mDatabase = new SQLiteDB(context);
}
@Override
public void onBindViewHolder(final FavoriteViewHolder holder, final int mPosition) {
final Favorite singleFavorite = listFavorites.get(mPosition);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Clicked " + "'" + singleFavorite.getName() + "'", Toast.LENGTH_SHORT, true).show();
//mainActivity.onLatLngData(singleFavorite.getLat(), singleFavorite.getLong());
mCallback.onLatLngData(singleFavorite.getLat(), singleFavorite.getLong());
}
});
//other stuff
@Override
public void onLatLngData(Double lat, Double lng) {
try {
mCallback = (ILatLong) context;
}
catch (ClassCastException e) {
Log.d("MyDialog", "Activity doesn't implement the ILatLong interface");
}
}
}
接口:
public interface ILatLong {
void onLatLngData(Double lat, Double lng);
}
MainActivity方法:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, ILatLong {
@Override
public void onLatLngData(Double lat, Double lng) {
//use the values
}
}
正如您所见,我已经在onBindViewHolder中设置了onclicklistener,因此我知道单击了哪个项目,并且在单击recyclerview项目时,它将显示带有正确项目名称的吐司(或我想要的任何其他属性)
当我尝试通过取消注释onclick中的行将纬度和经度发送回我的mainactivity方法时,我得到 “ java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法... onLatLngData(java.lang.Double,java.lang.Double)'”。
如何将这些数据发送回我的mainactivity?我曾经尝试过使用接口回调到MainActivity的过程,但是运气不佳,但是也许我做得不好。
编辑:包含界面回调内容
答案 0 :(得分:0)
您必须执行这4个步骤才能使回调工作
public interface MyCallback{
public void onCallbackClicked(String param)
}
... implements MyCallback{
// Other stuff here
@Override
public void onCallbackClicked(String param){
// Code here
}
MyCallback myCallback;
在您的onBindViewHolder内部,您应该有类似的东西:
@Override
public void onBindViewHolder(final FavoriteViewHolder holder, final int mPosition) {
final Favorite singleFavorite = listFavorites.get(mPosition);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(myCallback != null){
myCallback.onCallbackClicked(/* your data here */);
}
}
});
}
public FavoriteAdapter(MyCallback myCallback){
this.myCallback = myCallback;
}
或使用二传手
public void setCallback(MyCallback myCallback){
this.myCallback = myCallback;
}
现在你很好。
答案 1 :(得分:0)
感谢您的帮助。 我通过传递MainActivity来解决了这个问题。
问题在于它进入了MainActivty-> FavoriteDialog-> FavoriteAdapter,我想我对这里的链感到困惑。 也许会帮助别人。
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, ILatLong {
MainActivity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
}
// Where I call my FavoriteDialog fragment, pass mActivity in constructor
FragmentManager fm = getSupportFragmentManager();
FavoriteDialog dialogFavorite = new DialogFavorite(context, mActivity);
dialogFragment.show(fm, "FavoriteDialog");
}
DialogFavorite
DialogFavorite(Context context, MainActivity mActivity) {
mainActivity = mActivity;
}
// Adapter, pass mainActivity
FavoriteAdapter mAdapter = new FavoriteAdapter(context, allFavorites, mainActivity);
favoriteView.setAdapter(mAdapter);
FavoriteAdapter:
public FavoriteAdapter(Context context, List<Favorite> listFavorites, MainActivity mActivity) {
this.context = context;
this.listFavorites = listFavorites;
mDatabase = new SQLiteDB(context);
this.mainActivity = mActivity;
}
现在在我的FavoriteAdapter onclick中调用mainActivity即可:
@Override
public void onBindViewHolder(final FavoriteViewHolder holder, final int mPosition) {
final Favorite singleFavorite = listFavorites.get(mPosition);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mainActivity.onLatLngData(52.246218, 20.970564);
}
});