简体   繁体   English

Volley-从onResponse获取数据

[英]Volley - get the data from onResponse

I have a problem using Volley, I cant get data out from OnResponse mehod. 我在使用Volley时遇到问题,我无法从OnResponse方法获取数据。 I need to use the List outside , for Fragment operations, but i couldn`t get it out from there. 我需要将List用作Fragment的操作,但是我无法从中获取它。 Maybe im doing something wrong, but i was unable to find a solution on other sites. 也许我做错了事,但是我无法在其他站点上找到解决方案。

Can anyone help me find a solution please? 谁能帮我找到解决方案?

Here is my code: 这是我的代码:

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

} }

Firstly in onCreate() : 首先在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);

Then in onResponse() : 然后在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();
   }

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

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