如何使用Retrofit将数据设置为listview?
首先这是代码。但这不是真正的代码。如何使用Retrofit将数据设置为listview?
API_Interface.java
public interface API_Interface{
@GET("/api/foo")
Call<Foo> foo_API();
}
foo.java
public class foo{
@SerializedName("foo")
@Expose
private String foo;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity{
String buzz;
Retrofit foo_retro;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view);
ArrayList<foo> fooList = new ArrayList<foo>();
FooAdapter fooAdapter = new FooAdapter(this, 0, fooList);
listView.setAdapter(fooAdapter);
...
foo_retro = new Retrofit.Builder()
.baseUrl("http://api.foo.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
API_Interface foo_service = foo_retro.create(API_Interface.class)
Call<foo> foo_call=foo_service.foo_API();
foo_call.enqueue(new Callback<foo>() {
@Override
public void onResponse(Call<foo> call, Response<foo> response) {
buzz = response.body().getFoo();
System.out.println(buzz);
}
@Override
public void onFailure(Call<zaifExchange> call, Throwable t) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT);
}
});
fooList.add(new foo(buzz));
System.out.println(buzz);
}
}
当我运行这段代码,我可以和打印buzz
(如“苹果“)在onResponse
方法中。 但是,我无法将数据设置为listview
(buzz
为空)。 我知道原因是enqueue
是异步的,所以终端显示:
I/System.out: null
I/System.out: Apple
那么,应该怎么办? 谢谢。
增加应答到fooList
内onResponse
否则要添加null
值
@Override
public void onResponse(Call<foo> call, Response<foo> response) {
buzz = response.body().getFoo();
fooList.add(new foo(buzz));
System.out.println(buzz);
fooAdapter.notifyDataSetChanged();
// ^^^^ notify changes to display data
// you might want to declare references outside oncreate to avoid local variables
}
删除
fooList.add(new foo(buzz));
System.out.println(buzz);
(外enqueue
),因为响应之前buzz
将null
感谢您的帮助!我可以显示数据! (需要notifyDataSetChanged()) – NooNoo
@NooNoo我很高兴它有帮助,快乐的编码 –
依我之见它,你的问题是双重的:
- 考虑在打印
onCreate()
方法本身之前延迟添加。更好的是,在继续之前确认buzz
已经设置好。 - 因为
enqueue()
是异步的,所以无论如何,buzz
都可能不会被执行打印的父线程看到。相反,考虑使用AtomicReference
类包装buzz
字段。这样,无论线程如何,最新的值buzz
都会被任何线程看到。
'考虑加入一个延迟'坏主意,如果响应来得早和迟,而不是加延迟和2点完全不需要,只添加不需要的非工作复杂性 –
@Pavneet_Singh这是一个公平的点,以及为什么我在最后说“更好的是,验证在继续之前实际上已设置了嗡嗡声。” – entpnerd
,你是对的,虽然建议的解决方案只会带来更多的代码和意外的复杂性,在这种情况下,伙计 –
你的API响应是什么? – Kuls
onResponse将响应数据设置为您的数组列表“fooList”,并设置为适配器 –