Android异步任务从网站读取json数据并填充listview(太慢)

时间:2015-11-25 20:34:58

标签: java android android-asynctask

我对异步任务和通过HTTPReaders收集数据有疑问。我正在使用Android Studio制作一个小型Android应用程序,该应用程序具有本地SQLLite数据库并使用API​​来收集其他数据。

所以我有这个本地数据(具有名称和村庄对象的商店)和外部json数据(具有邮政编码和名称的村庄)。所以我想用商店名称填写ListView,并在括号内填写村名。问题是,当我调用HTTPReader时,它加载速度慢,所以我的村庄名称已加载但括号之间为空。我想等到加载完所有数据然后显示它,我该怎么做?

我从外部网站收集村庄的Json数据。我使用带有结果就绪侦听器的httpReader。当我有我的数据时,我解码json并将一个对象的数组列表发送到我的ListViewAdapter填充

Store上课:

public class Store implements Serializable {
private int id;
private String name;
private Village village= new Village();

public Store() {
}

public Store(int id, String name, Village village) {
    this.id = id;
    this.name = name;
    this.village= village;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Village getVillage() {
    return village;
}

public void setVillage(Village village {
    this.village = village;
}


@Override
public String toString()
{
    return this.getName() + " (" + this.getVillage().getName() + ')';
}

public void setJsonVillageByID(int id)
{
    HttpReader httpReader = new HttpReader();
    httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
        @Override
        public void resultReady(String result) {
            JsonHelper jsonHelper = new JsonHelper();
            setVillage(jsonHelper.getVillage(result));
        }
    });
    httpReader.execute("http://storevillages.com/villages?search=id&value=" + id);
}}

填写列表视图的方法:

 private void fillListViewStores() {

    ArrayList<Store> stores = dbHelper.getStores();
    ListView listViewStores = (ListView) findViewById(R.id.stores_listViewStores);


    ArrayAdapter<Store> listViewStoresAdapter = new ArrayAdapter<Store>(this, android.R.layout.simple_list_item_1, stores);


    listViewStores.setAdapter(listViewStoresAdapter);
}

数据库getStore:

    public Store getStore(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(
            "Store",     
            new String[]{"id", "name", "villageID"}, 
            "id = ?",  
            new String[]{String.valueOf(id)}, 
            null,           // groupby
            null,           // having
            null,           // sorting
            null);          // ??

    Store store = new Store();
    if (cursor.moveToFirst()) {
        store.setId(cursor.getInt(0));
        store.setName(cursor.getString(1));
        store.setJsonVillageByID(cursor.getInt(2));
    }
    cursor.close();
    db.close();
    return store;
}

所以我想等到填充旋转器,直到所有的村庄都被装上。

1 个答案:

答案 0 :(得分:0)

您需要重新构建代码,以便在根据adapter.notifyDataSetChanged()数据设置村庄值时调用HttpReader

也许这样的事情会起作用:

private void fillListViewStores() {

    ArrayList<Store> stores = dbHelper.getStores();
    final ArrayAdapter<Store> listViewStoresAdapter = new ArrayAdapter<Store>(this, android.R.layout.simple_list_item_1, stores);

    for (Store s : stores) {
        final Store store = s;
        HttpReader httpReader = new HttpReader();
        httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
            @Override
            public void resultReady(String result) {
                JsonHelper jsonHelper = new JsonHelper();
                store.setVillage(jsonHelper.getVillage(result));
                listViewStoresAdapter.notifyDataSetChanged();
            }
        });
        httpReader.execute("http://storevillages.com/villages?search=id&value=" + store.getId());
    }

    ListView listViewStores = (ListView) findViewById(R.id.stores_listViewStores);
    listViewStores.setAdapter(listViewStoresAdapter);
}