简体   繁体   English

将RecyclerView从JSON清空为片段

[英]Empty RecyclerView from JSON to Fragment

I'm very new to android development and this is my first real app. 我对android开发非常陌生,这是我的第一个真实应用程序。 I did lots of research but can't get it too work. 我做了很多研究,但做不到。

I'm trying to send some JSON data to a RecyclerView in a Fragment however it just displays a blank screen, no error. 我正在尝试将一些JSON数据发送到Fragment中的RecyclerView,但是它仅显示空白屏幕,没有错误。

Here's the code: 这是代码:

FRAGMENT

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    String endpoints = "{\"access\": {\"token\":....;
    ArrayList<HashMap<String, String>> jsonList;
    View rootView = inflater.inflate(R.layout.fragment_overview, container, false);
    jsonList = EndpointsParser.parseJSON(endpoints);
    RecyclerView recyclerView = (RecyclerView)rootView.findViewById(R.id.overviewRV);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    EndpointsAdapter endpointsAdapter = new EndpointsAdapter(getActivity(),jsonList);
    recyclerView.setAdapter(endpointsAdapter);
    return rootView;
    }


PARSER

public class EndpointsParser extends Activity{
public static final String PUBLICURL = "publicURL";
public static final String REGION = "region";
public static final String TYPE = "type";
public static final String NAME = "name";

public static ArrayList<HashMap<String, String>> parseJSON(String endpoints){
    ArrayList<HashMap<String, String>> jsonList = new ArrayList<HashMap<String, String>>();
    try {
        Endpoints endpoint = new Endpoints();
        JSONObject keystone = new JSONObject(endpoints);
        JSONObject access = keystone.getJSONObject("access");
        JSONArray serviceCatalog = access.getJSONArray("serviceCatalog");

        for (int i = 0; i < serviceCatalog.length(); i++) {
            JSONObject objsvc = serviceCatalog.getJSONObject(i);
            JSONArray endpointsArray = objsvc.getJSONArray("endpoints");
            endpoint.setName(objsvc.getString("name"));
            endpoint.setType(objsvc.getString("type"));
            for (int j = 0; j < 1; j++) {
                JSONObject objept = endpointsArray.getJSONObject(j);
                endpoint.setRegion(objept.getString("region"));
                endpoint.setPublicURL(objept.getString("publicURL"));
            }

            HashMap<String, String> map = new HashMap<String, String>();
            map.put(NAME, endpoint.getName());
            map.put(TYPE, endpoint.getType());
            map.put(REGION, endpoint.getRegion());
            map.put(PUBLICURL, endpoint.getPublicURL());
            jsonList.add(map);
        }
    } catch (JSONException e) {
        Log.d("ErrorInitJSON", e.toString());
        e.printStackTrace();
    }
    return jsonList;
}

  ADAPTER

public class EndpointsAdapter extends RecyclerView.Adapter<EndpointListRowHolder> {

ArrayList<HashMap<String, String>> endpointsList = new ArrayList<HashMap<String, String>>();
public static final String PUBLICURL = "publicURL";
public static final String REGION = "region";
public static final String TYPE = "type";
public static final String NAME = "name";
private Context mContext;

public EndpointsAdapter(Context context, ArrayList<HashMap<String, String>> endpointsList) {
    this.endpointsList = endpointsList;
    this.mContext = context;
}

@Override
public EndpointListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.endpoint_list, null);
    EndpointListRowHolder mh = new EndpointListRowHolder(v);
    return mh;
}

@Override
public void onBindViewHolder(EndpointListRowHolder endpointListRowHolder, int i) {

        endpointListRowHolder.name.setText(endpointsList.get(i).get(NAME));
        endpointListRowHolder.type.setText(endpointsList.get(i).get(TYPE));
        endpointListRowHolder.region.setText(endpointsList.get(i).get(REGION));
        endpointListRowHolder.url.setText(endpointsList.get(i).get(PUBLICURL));
}

@Override
public int getItemCount() {
    return (null != endpointsList ? endpointsList.size() : 0);
}
}

class EndpointListRowHolder extends RecyclerView.ViewHolder {
protected TextView name;
protected TextView type;
protected TextView region;
protected TextView url;


public EndpointListRowHolder(View view) {
    super(view);
    this.name = (TextView) view.findViewById(R.id.name);
    this.type = (TextView) view.findViewById(R.id.type);
    this.region = (TextView) view.findViewById(R.id.region);
    this.url = (TextView) view.findViewById(R.id.url);
}

}

From the debugger I can see the list is correctly sent to recyclerView.setAdapter but from then on it just gets Null. 从调试器中,我可以看到列表已正确发送到recyclerView.setAdapter,但是从那以后,它就变成了Null。 It seems it's 90% working, I'd appreciate a lot some help to make it to 100%. 看来90%都可以使用,我非常感谢您提供一些帮助,以使其达到100%。 Any ideas? 有任何想法吗?

上面的代码现在可以正常工作,问题是XML文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM