gpt4 book ai didi

java - Volley - 从onResponse获取数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:03:33 25 4
gpt4 key购买 nike

我在使用 Volley 时遇到问题,无法从 OnResponse 方法中获取数据。我需要使用外部列表进行 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);
}

}

最佳答案

首先在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();
}

关于java - Volley - 从onResponse获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53214851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com