0
我試圖建立一個ListView(誰是在片段內)與 從外部網址獲取數據。Android Volley JSONObjectRequest在片段輸出什麼都沒有
我使用Volley來做這個請求,但是當我嘗試運行這個應用程序時,它什麼也沒有顯示。
這裏的片段代碼:
public class DashboardFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final List<NewsFeed> newsFeed = new ArrayList<>();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, "http://webservice_url/", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject obj = response.getJSONObject("data");
NewsFeed feed = new NewsFeed(obj.getString("title"), obj.getString("description"), obj.getString("date"));
newsFeed.add(feed);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(request);
ListView listView = (ListView) getActivity().findViewById(R.id.newsFeed);
NewsFeedAdapter adapter = new NewsFeedAdapter(getActivity(), (ArrayList<NewsFeed>) newsFeed);
listView.setAdapter(adapter);
}
}
這裏是新聞源類和適配器:
public class NewsFeed {
public String title;
public String description;
public String date;
public NewsFeed(String title, String description, String date) {
this.title = title;
this.description = description;
this.date = date;
}
}
public class NewsFeedAdapter extends ArrayAdapter<NewsFeed> {
// View lookup cache
private class ViewHolder {
TextView title;
TextView description;
TextView date;
}
public NewsFeedAdapter(Context context, ArrayList<NewsFeed> newsFeedArrayList) {
super(context, R.layout.fragment_dashboard_news, newsFeedArrayList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
NewsFeed newsFeed = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fragment_dashboard_news, parent, false);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.description = (TextView) convertView.findViewById(R.id.description);
viewHolder.date = (TextView) convertView.findViewById(R.id.date);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(newsFeed.title);
viewHolder.description.setText(newsFeed.description);
viewHolder.date.setText(newsFeed.date);
return convertView;
}
}
此外,當我添加而不排球元件請求它的工作。
在'onResponse()'你需要更新listView。在填充'newsFeed'後立即執行'adapter.notifyDataSetChanged()'。試試吧:) – rafid059
先生,你做了我的一天!這是現在完美的工作,謝謝! – Manu
我將添加此作爲答案然後:) – rafid059