簡體   English   中英

片段中的Android Volley JSONObjectRequest不輸出任何內容

[英]Android Volley JSONObjectRequest in Fragment outputs nothing

我正在嘗試通過從外部URL獲取數據來構建ListView(位於Fragment內)。

我正在使用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);
    }
}

這是NewsFeed類和適配器:

    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;
    }
}

另外,當我在沒有Volley請求的情況下添加元素時,它就可以正常工作。

原始評論

onResponse( )您需要更新listView。 在填充newsFeed之后newsFeed執行adapter.notifyDataSetChanged( ) 試試看 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM