Volley-从onResponse获取数据

时间:2018-11-08 19:29:17

标签: java android android-volley

我在使用Volley时遇到问题,我无法从OnResponse方法获取数据。 我需要将List用作Fragment的操作,但是我无法从中获取它。也许我做错了事,但我无法在其他站点上找到解决方案。

有人可以帮我找到解决方案吗?

这是我的代码:

public class MainActivity extends AppCompatActivity
{

private static final String JSON_URL = "http://Something/v1/Api.php?apicall=gettopics";

ListView listView;
List<Topic> topicList;
List<Topic> topicList2;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    listView = (ListView) findViewById(R.id.listViewTopics);
    topicList = new ArrayList<>();
     loadTopics();
    if(topicList.isEmpty())
    {
        TextView t = findViewById(R.id.text);
        t.setText("Empty");
    }
}


class TopicAdapter extends ArrayAdapter<Topic>
{
    List<Topic> topicList;

    public List<Topic> getTopicList() {
        return topicList;
    }

    public TopicAdapter(List<Topic> topicList)
    {
        super(MainActivity.this, R.layout.layout_topic_list, topicList);
        this.topicList = topicList;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = getLayoutInflater();
        View listViewItem = inflater.inflate(R.layout.layout_topic_list, null, true);

        TextView textViewName = listViewItem.findViewById(R.id.textViewTitle);

        final Topic topic = topicList.get(position);
        textViewName.setText(topic.getTitle());
        return listViewItem;
    }

}

public void loadTopics() {
    StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject obj = new JSONObject(response);
                        JSONArray topicArray = obj.getJSONArray("topics");
                        for (int i = 0; i < topicArray.length(); i++) {
                            JSONObject topicObject = topicArray.getJSONObject(i);
                            Topic topic = new Topic(topicObject.getInt("id"),topicObject.getInt("ordering"),topicObject.getString("title"));
                            topicList.add(topic);
                        }
                        TopicAdapter adapter = new TopicAdapter(topicList);
                        listView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });


    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

}

1 个答案:

答案 0 :(得分:0)

首先在onCreate()

topicList = new ArrayList<>();
listView = (ListView) findViewById(R.id.listViewTopics);
adapter = new TopicAdapter(topicList); //note:define 'adapter' as a class field as 'listView' 
listView.setAdapter(adapter);

然后在onResponse()中输入

 topicList.clear();
 try{
     JSONObject obj = new JSONObject(response);
     JSONArray topicArray = obj.getJSONArray("topics");
     for (int i = 0; i < topicArray.length(); i++) {
         JSONObject topicObject = topicArray.getJSONObject(i);
         Topic topic = new Topic(topicObject.getInt("id"),topicObject.getInt("ordering"),topicObject.getString("title"));
         topicList.add(topic);
     }
     adapter.notifyDataSetChanged();
   } catch (JSONException e) {
     e.printStackTrace();
   }