我对Android开发很新,这是我的第一个真正的应用程序。我做了很多研究,但不能把它做得太好。
我正在尝试将一些JSON数据发送到片段中的RecyclerView,但它只显示一个空白屏幕,没有错误。
以下是代码:
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);
}
}
从调试器中我可以看到列表已正确发送到recyclerView.setAdapter,但从那时起它就变为Null。它似乎有90%的工作,我很欣赏它有一些帮助,使它达到100%。有什么想法吗?
答案 0 :(得分:1)
上面的代码现在正在运行,问题是XML文件