【问题标题】:What is the wrong with this onPostexecute method in Asynctask?Asynctask 中的这个 onPostexecute 方法有什么问题?
【发布时间】:2016-11-22 11:43:26
【问题描述】:
public class ShopsList extends AppCompatActivity {

private RecyclerView listView;
private StoreListAdapter mAdapter;
private ArrayList<Stores> stores;
public static final String LOG_TAG = ShopsList.class.getName();
private String sampleURL = "http://104.199.230.125/stores/1.json/";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shops_list);
    StoresAsyncTask task = new StoresAsyncTask();

    task.execute(sampleURL);

    mAdapter = new StoreListAdapter(this, R.layout.list_item_layout, stores);
    listView = (RecyclerView) findViewById(R.id.store_list);

    RecyclerView.LayoutManager layoutManager = new       LinearLayoutManager(this);
    listView.setLayoutManager(layoutManager);

    listView.setAdapter(mAdapter);

        private class StoresAsyncTask extends AsyncTask<String, Void, List<Stores>> {
    @Override
    protected List<Stores> doInBackground(String... URLs) {


        if (URLs.length < 1 || URLs[0] == null) {
            Log.e("QueryUtils", "URL is is null");
            return null;
        }
        Log.e("QueryUtils", "URL is not null" + URLs[0]);

        return QueryHandler.fetchStoreData(URLs[0]);
    }
    @Override
    protected void onPostExecute(List<Stores> data) {

        mAdapter.notifyDataSetChanged();
        listView.setAdapter(mAdapter);
        super.onPostExecute(data);
    }
}


}

它不显示列表,它只显示一个空列表。我正在使用 recyclerview.adapter。还有一个问题是 getItemCount() 在使用 [return this.stores.size();] 并且应用程序没有打开时抛出空指针异常,当我将此行更改为 [return this.stores == null 时? 0 : stores.size();] 它打开但列表为空。

public int getItemCount() {

    Log.e(LOG_TAG, "stores size");
//       return this.stores.size();
    return this.stores == null ? 0 : stores.size();
}

当我使用列表视图时,postexecute 方法体是这样的,它可以工作。

    protected void onPostExecute(List<Quakes> data) {
        mAdapter.clear();
        if (data != null && !data.isEmpty()) {
            mAdapter.addAll(data);
        }
    }

如何正确执行与recyclerview.adapter相关的Asynctask中的postexecute方法? JSON解析没有错误,只是我无法将它加载到适配器中。

这是适配器

公共类 StoreListAdapter 扩展 RecyclerView.Adapter {

private ArrayList<Stores> stores = new ArrayList<>();
private int itemResource;
private Context context;

public StoreListAdapter(Context context, int itemResource, ArrayList<Stores> stores) {

    this.stores = stores;
    this.itemResource = itemResource;
    this.context = context;
}

@Override
public storeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.list_item_layout, parent, false);

    return new storeViewHolder(this.context, view);
}

@Override
public void onBindViewHolder(storeViewHolder holder, int position) {
    Stores stores = this.stores.get(position);
    holder.bindStoreData(stores);
}

@Override
public int getItemCount() {

    Log.e(LOG_TAG, "stores size");
   return this.stores.size();
}}

【问题讨论】:

  • 我想你忘了放 mAdapter.notifydatasetchanged();
  • 请看第一个代码onPostExecute方法,是不是mAdapter.notifydatasetchanged();去?注意:最后一个代码 onPostExecute 方法工作正常,与列表视图相关。上述与 recyclerview 相关的 onPostExecute 方法代码不起作用。请在那里提出任何更改建议。
  • @P.Vamsi 尝试评论 listView.setAdapter(mAdapter);在异步任务之前
  • 问题在于为适配器设置新数据并使用 notifyDataSetChange 进行刷新,检查 readyandroid 给出的答案
  • 我试过了,但还是一样,我想我的适配器有错误。请检查适配器是否正确,它在返回 this.stores.size() 时抛出空指针异常;当我用 return this.stores == null 替换它时? 0 : 商店.size();或返回 0;列表为空,当我用 return 1 替换时,它会在 bindviewholders [Stores stores = this.stores.get(position);] 行抛出空指针异常

标签: android android-asynctask android-recyclerview


【解决方案1】:

当你在 onPostExecute 中获取代码时,你应该在 List Adapter 中传递这些数据

@Override
    protected void onPostExecute(List<Stores> data) {
        mAdapter = new StoreListAdapter(this, R.layout.list_item_layout, data);
        listView.setAdapter(mAdapter);
        super.onPostExecute(data);
    }

其他方式,我认为更好,如果您正在制作自定义列表适配器,请制作 getter 和 setter 或 addAll 之类的方法来更新列表适配器中的数据。

修改后 创建ArrayList&lt;Stores&gt; stores的getter和setter,然后

@Override
        protected void onPostExecute(List<Stores> data) {
            mAdapter.setStores(data);
            mAdapter.notifyDataSetChanged();
        }

同样在shopList类private ArrayList&lt;Stores&gt; stores= = new ArrayList&lt;&gt;();可以避免空异常

【讨论】:

  • 适配器向 getItemcount 和 bindviewholder 抛出空指针。我正在使用上面上传的自定义适配器,如何更新方法中的数据。
  • 哇,谢谢伙计。你的最后一行“也在 shopList 类私有 ArrayList stores= = new ArrayList(); 可能避免空异常”成功了。我赞成你的回答,但它说它不算数,因为我有
【解决方案2】:

您已经以编程方式回答了自己的问题:

您的解决方案 1:

@Override
    protected void onPostExecute(List<Stores> data) {

        mAdapter.notifyDataSetChanged();
        listView.setAdapter(mAdapter);
        super.onPostExecute(data);
    }

您的解决方案 2:

@Override
protected void onPostExecute(List<Quakes> data) {
        mAdapter.clear();
        if (data != null && !data.isEmpty()) {
            mAdapter.addAll(data);
        }
    }

在解决方案 1 中,您再次将适配器添加到列表视图,但将列表数据更新到适配器。所以这是你在这里犯的错误,在解决方案2中得到解决。

结论:onPostExecute 没有问题。问题在于将更新的数据传递给适配器并使 notifyDataDetChange 将更新的数据应用于列表视图。

因此,通过创建自定义方法将数据添加到适配器,就像您对 mAdapter.addAll(data/new data/) 所做的那样,然后执行 notifyDataSetChange() 以使用 listview 刷新新更新的数据。

@Override
protected void onPostExecute(List<Quakes> data) {
        if (data != null && !data.isEmpty()) {
            mAdapter.addAll(data);
            mAdapter.notifyDataSetChange();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多