Java 将Json对象从ViewHolder传递到Fragment
我有一个Java 将Json对象从ViewHolder传递到Fragment,java,android,json,android-fragments,Java,Android,Json,Android Fragments,我有一个回收视图和一个片段。我想将JSONObject从RecyclerView传递到Fragment。因此,我创建了一个接口,并在Fragment和RecyclerView上实现了它。我初始化了变量并访问了片段中的方法,将JSONObject传递给它,但是在尝试访问该方法时,我得到了NPE: public class ProductResultsListFragment extends Fragment implements ProductResultAdapterInterface{ //
回收视图
和一个片段
。我想将JSONObject
从RecyclerView
传递到Fragment
。因此,我创建了一个接口,并在Fragment
和RecyclerView
上实现了它。我初始化了变量并访问了片段中的方法,将JSONObject
传递给它,但是在尝试访问该方法时,我得到了NPE
:
public class ProductResultsListFragment extends Fragment implements ProductResultAdapterInterface{
//code here
@Override
public void showResultsInMap(JSONObject mapObject)
{
openMapFragment(mapObject);
}
在我的RV课程中,我有以下内容:
public class ProductSearchAdapter extends RecyclerView.Adapter<ProductSearchAdapter.ViewHolder> {
public ProductResultAdapterInterface mProductResultsListener;
.....
if (mapObjects.length()>0)
{
mProductResultsListener.showResultsInMap(mapObjects);
}
简单评论一下:mapObject(JsonObject)是在ViewHolder中按钮内的onClick方法上创建的,我无法在OnCreate方法上传递bundle
this.btnMarkItemMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
switch (v.getId())
{
case (R.id.markProductinMap):
String mapObj=null;
if (mapObjects !=null)
{
String positionDescription=String.valueOf(position);
String productLatLngValue=txtProductPrice.getText().toString()+";"+ txtItemLanLong.getText().toString();
//tring mapPosition=
mapObj=mapObjects.optString(String.valueOf(position));
if (mapObj!="") //Button not click remove from map
{
btnMarkItemMap.setColorFilter(Color.rgb(0, 0, 0));
btnMarkItemMap.setTag(Color.rgb(0, 0, 0));
mapObjects.remove(positionDescription);
}
else //Button Click add to Map
//
{
btnMarkItemMap.setColorFilter(Color.rgb(255, 51, 102));
btnMarkItemMap.setTag(position);
try {mapObjects.put(positionDescription, productLatLngValue);}catch (JSONException ex){}
if (mapObjects.length()>0)
{
mProductResultsListener.showResultsInMap(mapObjects);
}
}
}else{
mapObjects=new JSONObject();
}
break;
}
}
});
您在哪里分配了变量
mpProductResultsListener
?
您需要通过将其传递到适配器,将其分配给片段的对象。否则,它的值为null,如果调用null对象上的函数,将得到一个nullpointeeexception
如果您有,请使用该代码更新问题。您可以使用如下捆绑包传递数据
SomeFragment someFragment = new SomeFragment();
Bundle bundle = new Bundle();
bundle.putString("value1", "2");
bundle.putString("value2", "23");
bundle.putString("value3", "276");
bundle.putString("value4", "27");
bundle.putBoolean("flag", true);
someFragment.setArguments(bundle);
在某个片段中
Bundle bundle = getArguments();
strValue1 = bundle.getString("value1");
strValue2 = bundle.getString("value2");
依此类推,为了在片段适配器init上从bundle获取所有传递的数据,我添加了:
ProductResultsListFragment yourFragment = new ProductResultsListFragment();
Bundle bundle = new Bundle();
bundle.putString("yourJsonObject", yourJsonObject.toString);
yourFragment.setArguments(bundle);
public class ProductResultsListFragment...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle savedInfo = this.getArguments();
String savedJsonString = savedInfo.getString("yourJsonObject");
JSONObject myJsonObject = new JSONObject(savedJsonString);
//....
ProductResultAdapterInterface mProInterface=(ProductResultAdapterInterface) this;
mAdapter = new ProductSearchAdapter(R.layout.product_results_cardlist,getActivity(),productListFeed,mProInterface);
在适配器构造函数上,我更改了签名,以接受接口作为从片段传递的参数
public ProductSearchAdapter (int rowLayout, Context context,List<Product> feedList,ProductResultAdapterInterface mProductResultsListener) {
this.feedItemLists=feedList;
this.rowLayout = rowLayout;
this.mContext = context;
this.mProductResultsListener=mProductResultsListener;
}
publicProductSearchAdapter(int行布局、上下文上下文、列表提要列表、ProductResultAdapter接口MPProductResultsListener){
this.feedItemList=feedList;
this.rowLayout=rowLayout;
this.mContext=上下文;
this.mpProductResultsListener=mpProductResultsListener;
}
然后,我能够访问该方法,而不获得NPE。谢谢所有的都可能是一个评论。我想你错过了评论的一部分@Rohit5k2:我把它作为一个答案贴了出来,因为它通常会自己把一个简短的答案写进评论中。否则,如果我在写评论时不小心按enter键,我就会推送不完整的评论。@ImRickJames:你为什么需要强制转换?如果片段实现了ProductResultAdapterInterface,则只需将其分配给variable@Mo1989从我所读到的内容来看,当你需要使用回调函数时,你需要初始化它,这样你就可以在其中使用接口的方法。在我的例子中,片段实现接口,视图持有者访问方法。我更新了我的代码。谢谢@这是正确的,但您可以说mpProductResultsListener=myProductResultsListFragment。如果只是将其作为片段而不是ProductResultsListFragment传递,则可能需要将其强制转换为一个片段。但我建议直接将其作为ProductResultAdapterInterface在适配器的构造函数或setter中传递。在这种情况下不适用。OP使用的是
接口
。最基本的事情是,您应该从一开始就检查,比如Mo1989 saidCheck,检查侦听器是否正确声明和定义,以及您传递的数据是否正确null@Aziz数据不为null,但NPE来自未初始化的MPProductResultsListener。然后初始化它,您得到了答案:)片段已经创建,我想在片段出现在屏幕上后传递一个JsonObject,只想传递在ViewHolder中的Onclick方法上创建的对象
public ProductSearchAdapter (int rowLayout, Context context,List<Product> feedList,ProductResultAdapterInterface mProductResultsListener) {
this.feedItemLists=feedList;
this.rowLayout = rowLayout;
this.mContext = context;
this.mProductResultsListener=mProductResultsListener;
}