后端Android工作室数据检索未在ListView中显示

时间:2017-12-19 14:06:53

标签: listview android-studio backendless

我在Backendless中设置了一个数据库,我的Android Studio应用程序正在访问它。连接成功并且成功检索了项目的名称(根据日志)但我的列表视图未显示它们。我在本地添加了一个条目“test”,正在显示。你能帮我吗?这是我的代码:

public class ListItems extends Activity {

ListView foodListView;
ArrayList<String> myFood;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set up listView
    setContentView(R.layout.list_items);
    foodListView = (ListView)findViewById(R.id.foodListView);
    myFood = new ArrayList();
    myFood.add("test");

    //connect to database
    Backendless.setUrl(Defaults.SERVER_URL);
    Backendless.initApp(getApplicationContext(),
            Defaults.APPLICATION_ID,
            Defaults.API_KEY);

    final IDataStore<Map> tableQuery = Backendless.Data.of( "FoodList" );
    final DataQueryBuilder queryBuilder = DataQueryBuilder.create();

    queryBuilder.setWhereClause( "Section = 'Dairy'" );
    queryBuilder.setSortBy( "name" );

    Backendless.Data.of( "FoodList" ).find( queryBuilder,
            new AsyncCallback<List<Map>>()
            {
                @Override
                public void handleResponse(List<Map>response ){

                    Iterator<Map> itr = response.iterator();

                    while(itr.hasNext()) {
                        Map<String, Object> contact = (Map<String, Object>) itr.next();
                        String name = (String) contact.get("Name");
                        myFood.add(name);
                    }

                    //test to show items retrived
                    for(String aa:myFood){
                        System.out.println("MYAPP " +aa);
                    }


                }
                @Override
                public void handleFault( BackendlessFault fault )
                {
                    Log.e("MYAPP", "Server reported an error - " + fault.getMessage());
                }
            });


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myFood);
    foodListView.setAdapter(arrayAdapter);


}

}

1 个答案:

答案 0 :(得分:0)

由于您是异步加载数据,因此您的ListView可能会比数据到达时更早显示 - 因此您无法在那里看到它。要解决此问题,您应该在更新数据源列表后立即致电notifyDataSetChanged() ListView,该列表位于handleResponse方法内。

因此,您的代码应如下所示:

public class ListItems extends Activity {

ListView foodListView;
ArrayList<String> myFood;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set up listView
    setContentView(R.layout.list_items);
    foodListView = (ListView)findViewById(R.id.foodListView);
    myFood = new ArrayList();

    //connect to database
    Backendless.setUrl(Defaults.SERVER_URL);
    Backendless.initApp(getApplicationContext(), Defaults.APPLICATION_ID, Defaults.API_KEY);

    final IDataStore<Map> tableQuery = Backendless.Data.of( "FoodList" );
    final DataQueryBuilder queryBuilder = DataQueryBuilder.create();

    queryBuilder.setWhereClause( "Section = 'Dairy'" );
    queryBuilder.setSortBy( "name" );

    Backendless.Data.of( "FoodList" ).find( queryBuilder,
            new AsyncCallback<List<Map>>()
            {
                @Override
                public void handleResponse(List<Map>response ){

                    Iterator<Map> itr = response.iterator();

                    while(itr.hasNext()) {
                        Map<String, Object> contact = (Map<String, Object>) itr.next();
                        String name = (String) contact.get("Name");
                        myFood.add(name);
                    }

                    // new data has been now added to the list - need to update view
                    foodListView.notifyDataSetChanged();
                }
                @Override
                public void handleFault( BackendlessFault fault )
                {
                    Log.e("MYAPP", "Server reported an error - " + fault.getMessage());
                }
            });


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myFood);
    foodListView.setAdapter(arrayAdapter);


}