如何使用Retrofit将数据设置为listview?

时间:2017-11-11 06:03:31

标签: java android retrofit2

首先这是代码。但这不是真正的代码。

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(如“Apple”)。 但是,我无法将数据设置为listviewbuzz为空)。 我知道原因是enqueue是异步的,所以终端显示:

I/System.out: null
I/System.out: Apple

那我该怎么办? 感谢。

2 个答案:

答案 0 :(得分:3)

function extend_admin_search( $query ) { // Extend search for document post type $post_type = 'document'; // Custom fields to search for $custom_fields = array( "_file_name", ); if( ! is_admin() ) return; if ( $query->query['post_type'] != $post_type ) return; $search_term = $query->query_vars['s']; // Set to empty, otherwise it won't find anything $query->query_vars['s'] = ''; if ( $search_term != '' ) { $meta_query = array( 'relation' => 'OR' ); foreach( $custom_fields as $custom_field ) { array_push( $meta_query, array( 'key' => $custom_field, 'value' => $search_term, 'compare' => 'LIKE' )); } $query->set( 'meta_query', $meta_query ); }; } add_action( 'pre_get_posts', 'extend_admin_search' ); 内向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

答案 1 :(得分:0)

在我看来,你的问题是双重的:

  1. 考虑在使用onCreate()方法打印之前添加延迟。更好的是,在继续之前验证buzz是否已实际设置。
  2. 由于enqueue()是异步的,无论发生什么,执行打印的父线程都可能看不到buzz。相反,请考虑使用AtomicReference类包装buzz字段。这样,无论线程如何,任何线程都会看到buzz的最新值。