将项添加到Fragments中的ListView会创建重复项

时间:2016-12-01 09:18:54

标签: android listview

这个问题已被多次询问,但没有一个解决方案解决了我的问题。        我有一个带有viewpager的两个片段的活动,每个片段都有一个listview。在标签之间移动时,我的列表视图的项目正在重复 这是适配器代码。

public class ComboMenuAdapter extends BaseAdapter {


    private Context mContext;

    private MenuHolder mMenuHolder;


    private ArrayList<Integer> index;

    public ComboMenuAdapter(Context mContext, ArrayList<Integer> index) {
        this.index = index;

        this.mContext = mContext;


    }


    @Override
    public int getCount() {
        return index.size();
    }

    @Override
    public MenuHolder getItem(int position) {

        return DataHandler.getInstance().getData(index.get(position));
    }

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

    private class Holder {
        ImageView image;
        TextView price, liters, itemnametxt, mAdd_txt, minus, plus, counter;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final Holder mHolder;

        View v = convertView;

        if (v == null) {

            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

           v= vi.inflate(R.layout.combo_meals_adapter, null);

            mHolder = new Holder();
            mHolder.price = (TextView) v.findViewById(R.id.price);
            mHolder.itemnametxt = (TextView) v.findViewById(R.id.itemnametxt);
            mHolder.image = (ImageView) v.findViewById(R.id.image);
            mHolder.liters = (TextView) v.findViewById(R.id.ml);
            mHolder.mAdd_txt = (TextView) v.findViewById(R.id.addtxt);
            mHolder.minus = (TextView) v.findViewById(R.id.minus);
            mHolder.counter = (TextView) v.findViewById(R.id.count);
            mHolder.plus = (TextView) v.findViewById(R.id.plus);


            v.setTag(mHolder);


        } else {
            mHolder = (Holder) v.getTag();
        }
        mMenuHolder = getItem(position);
        mHolder.itemnametxt.setText(mMenuHolder.getItemname());
        mHolder.image.setImageResource(R.mipmap.ic_launcher);
        mHolder.price.setText(mMenuHolder.getItemprice());
        mHolder.counter.setText(mMenuHolder.getItemCount());
        return v;

    }

}

我无法找到错误,请帮助我,提前谢谢。

这里,我使用DataHandler Class

存储数据
public class DataHandler {

    ArrayList<MenuHolder> listOfItemsFromJson = new ArrayList<MenuHolder>();

    private static DataHandler mInstance = null;

  static   public DataHandler getInstance() {
        if (null == mInstance) {
            mInstance = new DataHandler();
        }
        return mInstance;
    }

    public DataHandler() {
        listOfItemsFromJson = new ArrayList<>();
    }


    public ArrayList<MenuHolder> getListOfItemsFromJson() {
        return listOfItemsFromJson;
    }

    public void clearList() {


        listOfItemsFromJson.clear();
    }

    public void addData(MenuHolder holder) {
        listOfItemsFromJson.add(holder);
    }

    public MenuHolder getData(int position) {

        return listOfItemsFromJson.get(position);

    }

    public void removeData(int position) {
        listOfItemsFromJson.remove(getData(position));
    }


    public void modifyData(MenuHolder holder, int position) {
        listOfItemsFromJson.set(position, holder);

    }


    public int size() {

        return listOfItemsFromJson.size();
    }


}

在onCreateView()上,我正在放置数据

 for (int i = 0; i < DataHandler.getInstance().size(); i++) {

            if (DataHandler.getInstance().getData(i).isCombo()) {
                DataHandler.getInstance().removeData(i);
            }

        }




        for (int i = 0; i < 3; i++) {
            MenuHolder mMenuHolder = new MenuHolder();

            mMenuHolder.setItemname(itemNames[i]);
            mMenuHolder.setImageName("@mipmap/ic_launcher");
            mMenuHolder.setItemCount("0");
            mMenuHolder.setItemprice("500");
            mMenuHolder.setUserSeleted(false);
            mMenuHolder.setCombo(true);
            mMenuHolder.setDrinks(false);
            mMenuHolder.setBiryani(false);
            DataHandler.getInstance().addData(mMenuHolder);

        }
        index.clear();
        for (int i = 0; i < DataHandler.getInstance().size(); i++) {
            if (DataHandler.getInstance().getListOfItemsFromJson().get(i).isCombo()) {
                index.add(i);
            }
        }

        mCombosAdapter = new ComboMenuAdapter(getActivity(), index);


        mlistView.setVisibility(View.VISIBLE);
        mlistView.setAdapter(mCombosAdapter);
        mlistView.setOnItemClickListener(this);

3 个答案:

答案 0 :(得分:1)

 mMenuHolder = getItem(position);
在设置数据之前应检索

。之前将它移到if / else逻辑之外。

mMenuHolder = getItem(position);
mHolder.itemnametxt.setText(mMenuHolder.getItemname());
mHolder.image.setImageResource(R.mipmap.ic_launcher);
mHolder.price.setText(mMenuHolder.getItemprice());
mHolder.counter.setText(mMenuHolder.getItemCount());

您想要在填写视图之前检索位置

答案 1 :(得分:0)

确保在向其添加新对象之前清除适配器:

if (!someAdapter.isEmpty())
     someAdapter.clear();

 someAdapter.addAll(new ArrarList(arrayListContainingUpdatedStuff));

不需要新的适配器分配,您可能会丢失对旧引用的引用,列表可能会变为空白。

希望这有帮助。

答案 2 :(得分:0)

对于此代码,您可以获得重复数据

  for (int i = 0; i < 3; i++) 
     {
        MenuHolder mMenuHolder = new MenuHolder();
        mMenuHolder.setItemname(itemNames[i]);
        mMenuHolder.setImageName("@mipmap/ic_launcher");
        mMenuHolder.setItemCount("0");
        mMenuHolder.setItemprice("500");
        mMenuHolder.setUserSeleted(false);
        mMenuHolder.setCombo(true);
        mMenuHolder.setDrinks(false);
        mMenuHolder.setBiryani(false);
        DataHandler.getInstance().addData(mMenuHolder);
    }