简体   繁体   English

在Android中的片段上更新listView

[英]update listView on a fragment in Android

I can't get a listView updated on Android. 我无法在Android上更新listView。 I have a fragment with a listView, and its items are returned by a REST call. 我有一个带listView的片段,它的项目由REST调用返回。

Here is the AsyncTask: 这是AsyncTask:

 private class GetMenuPizzeria extends AsyncTask<String, Void, Object[]> {
    private final SelectPizzaFragment.ContentAdapter mListAdapter;
    private final Context appContext;
    private final String pizzeriaId;

    private GetMenuPizzeria(ContentAdapter mListAdapter, Context appContext, String id) {
        this.mListAdapter = mListAdapter;
        this.appContext = appContext;
        this.pizzeriaId = id;
    }

    @Override
    protected Object[] doInBackground(String... params) {
            return pizzeriaManager.getMenuPizzeria(pizzeriaId).toArray();
    }

    @Override
    protected void onPostExecute(Object[] result) {
        mListAdapter.updateItems(result, appContext);
    }
}

and here is the function called onPostExecute (updateItems) : 这是一个叫做onPostExecute(updateItems)的函数:

public void updateItems(Object[] newItems, Context appContext) {

            PizzaMenu pm;

            lPizzaNames.clear();
            lPizzaDesc.clear();
            lPizzaPrices.clear();

            Resources resources = appContext.getResources();
            TypedArray a = resources.obtainTypedArray(R.array.place_avator);

            for (int i = 0; i < newItems.length; i++) {
                pm = (PizzaMenu) newItems[i];
                lPizzaNames.add(pm.getName());
                lPizzaDesc.add(pm.getIngredients());
                lPizzaPrices.add(pm.getPrice());
                lPlaceAvators.add(a.getDrawable(0));
            }
            this.notifyDataSetChanged();
    }

Then, I call the AsyncTask like that: 然后,我这样调用AsyncTask:

GetMenuPizzeria getMenuPizzeria = new GetMenuPizzeria(adapter, this.getContext(), id);
getMenuPizzeria.execute();

and that works when I call it the first time, inside the onCreateView of my fragment. 当我第一次在片段的onCreateView中调用它时,该方法就起作用了。 The problem is, I have to update the list every time the selected pizzeria changes, so I wrote a method: 问题是,每次选定的比萨店更改时,我都必须更新列表,所以我编写了一个方法:

public void updatePizzeriaMenu(String idPizzeria) {
    GetMenuPizzeria getMenuPizzeria = new GetMenuPizzeria(new ContentAdapter(recyclerView.getContext()), this.getContext(), idPizzeria);
    getMenuPizzeria.execute();
}

That method correctly calls the AsyncTask and I don't get any errors, except the list is not updated (but notifyDataSetChanged() is called and onBindViewHolder is called as well). 该方法正确地调用了AsyncTask,并且没有任何错误,除了列表未更新(但是调用了notifyDataSetChanged(),还调用了onBindViewHolder)。 I suppose the way I'm retrieving the adapter is wrong or something like that because seems like the list I'm populating everytime I call updatePizzeriaMenu is empty. 我想我检索适配器的方式是错误的或类似的东西,因为每次我调用updatePizzeriaMenu为空时,似乎都在填充列表。 Could someone please help me and tell me what I'm missing? 有人可以帮我,告诉我我想念什么吗? Thanks in advance 提前致谢

EDIT: here is my adapter class : 编辑:这是我的适配器类:

public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {


    private final List<String> lPizzaNames = new ArrayList<String>();
    private final List<String> lPizzaDesc = new ArrayList<String>();
    private final List<String> lPizzaPrices = new ArrayList<String>();

    private final List<Drawable> lPlaceAvators = new ArrayList<Drawable>();

    public ContentAdapter(Context context) {
        Resources resources = context.getResources();
        TypedArray a = resources.obtainTypedArray(R.array.place_avator);
        a.recycle();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
    }

    public void updateItems(Object[] newItems, Context appContext) {
        //see above....
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.avator.setImageDrawable(lPizzaAvators.get(position % lPizzaAvators.size()));
        holder.name.setText(lPizzaNames.get(position % lPizzaNames.size()));
        holder.description.setText(lPizzaDesc.get(position % lPizzaDesc.size()));
        holder.price.setText(lPizzaPrices.get(position % lPizzaPrices.size()));
    }



    @Override
    public int getItemCount() {
        return lPizzaPrices.size();
    }



}

You are creating new adapters each time in updatePizzeriaMenu(String idPizzeria) method. 您每次都在updatePizzeriaMenu(String idPizzeria)方法中创建新的适配器。 but i think, those adapters is not related to any recyclerView through recyclerView.setAdapter() method. 但我认为,这些适配器与通过recyclerView.setAdapter()方法与任何recyclerView无关。 So do not create new ContentAdapter each time instead pass the previous adapter object. 因此,不要每次都创建新的ContentAdapter而是传递先前的适配器对象。

public void updatePizzeriaMenu(String idPizzeria) {
    GetMenuPizzeria getMenuPizzeria = new GetMenuPizzeria(adapter, this.getContext(), idPizzeria);
    getMenuPizzeria.execute();
}

As you are clearing previous data 当您清除以前的数据时

lPizzaNames.clear();
lPizzaDesc.clear();
lPizzaPrices.clear();

and updating new data, it should work fine. 并更新新数据,它应该可以正常工作。

you haven't updated the adapter in this method, you are assigning some values to some variables, and it doesn't seem to be those variables having any relationship with adapter, you have to set Object in adapter! 您尚未在此方法中更新适配器,而是为某些变量分配了一些值,并且似乎这些变量与适配器没有任何关系,您必须在适配器中设置Object!

for example, 例如,

 this.setPizzaMenuList(pizzaMenuList); 
 this.notifyDataSetChanged)();

will update the ListView.and 将更新ListView.and

lpizzaMenuList= pizzaMenuList;
this.notifyDataSetChanged)();

won't update the ListView! 不会更新ListView!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM