ListView项目的位置重复

时间:2018-01-08 12:07:35

标签: android listview

我正在创建一个动态listview使用片段,其中项目的位置在第6个索引后重复(注意项目不重复)。

我必须在点击按钮时更改项目数量,但是当我尝试点击并更改索引0处的数量时,它也会更改第7个索引数量。

请帮帮我 提前完成。

    public class MenuListAdapter extends ArrayAdapter<DataModel> {

    private Activity activity;

    public String[] ItemsList = new String[1000];
    public String[] ItemsRelPrice;

    ArrayList<String> foodItemTitle = new ArrayList<>();

    private static String currentTitle;

    private String selection;
    private String ttl;


    public MenuListAdapter(Activity activity, int resource, List<DataModel> dataModelList) {
        super(activity, resource, dataModelList);

        this.activity = activity;
    }

    @Nullable
    @Override
    public DataModel getItem(int position) {
        return super.getItem(position);
    }

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

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

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

        ViewHolder holder = null;
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.food_item_list, parent, false);
            holder = new ViewHolder(convertView);

            final ImageButton addBtn = convertView.findViewById(R.id.img_add);
            final ImageButton removeBtn = convertView.findViewById(R.id.img_remove);

            final TextView qty = convertView.findViewById(R.id.item_quantity);
            final TextView priceTtl = convertView.findViewById(R.id.txt_cost);
            final TextView foodItem = convertView.findViewById(R.id.item_name);
            final TextView foodPrice = convertView.findViewById(R.id.per_item_cost);

            addBtn.setTag(position);
            addBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Log.v("btn tag", String.valueOf(addBtn.getTag()));
                    Log.v("qty tag", String.valueOf(qty.getTag()));
                    Log.v("Price tag", String.valueOf(priceTtl.getTag()));

                    int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
                    int costPerItem = Integer.valueOf(foodPrice.getText().toString());

                    if (qty.getTag() == addBtn.getTag())
                        qty.setText(String.valueOf(curntQty));

                    if (priceTtl.getTag() == addBtn.getTag())
                        priceTtl.setText(String.valueOf(costPerItem * curntQty));

                    new MenuPage().addItem(costPerItem);

                }
            });

            removeBtn.setTag(position);
            removeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (qty.getText().toString() != "0") {

                        int qtyTtl = Integer.parseInt(qty.getText().toString()) - 1;
                        qty.setText(String.valueOf(qtyTtl));

                        int costPerItem = Integer.valueOf(foodPrice.getText().toString());
                        int curPrice = Integer.valueOf(priceTtl.getText().toString());

                        priceTtl.setText(String.valueOf(curPrice - costPerItem));

                        new MenuPage().removeItem(costPerItem);

                    } else {
                        Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
                    }
                }
            });


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        DataModel dataModel = getItem(position);

        holder.foodItem.setTag(position);
        holder.foodItem.setText(dataModel.getItemTitleFinal());

        holder.foodPrice.setTag(position);
        holder.foodPrice.setText(dataModel.getItemPriceFinal());

        holder.foodType.setTag(position);
        holder.foodType.setImageResource(dataModel.getItemImgFinal());

        holder.addBtn.setTag(position);
        holder.removeBtn.setTag(position);
        holder.qty.setTag(position);
        holder.priceCurnt.setTag(position);

        return convertView;
    }


    private static class ViewHolder {

        private TextView foodItem;
        private TextView foodPrice;
        private ImageView foodType;
        private ImageButton addBtn;
        private ImageButton removeBtn;
        private TextView qty;
        private TextView priceCurnt;


        public ViewHolder(View v) {

            foodItem = v.findViewById(R.id.item_name);
            foodPrice = v.findViewById(R.id.per_item_cost);
            foodType = v.findViewById(R.id.fdTypeImg);
            addBtn = v.findViewById(R.id.img_add);
            removeBtn = v.findViewById(R.id.img_remove);
            qty = v.findViewById(R.id.item_quantity);
            priceCurnt = v.findViewById(R.id.txt_cost);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

我修改了你的适配器类。您需要dataModelList作为来源以扩大视图持有者。现在,您需要通过构造函数提供列表,并在我可能错过的位置进行小的修改。如果需要修改,请告诉我。

public class MenuListAdapter extends ArrayAdapter<DataModel> {

private Activity activity;

public String[] ItemsList = new String[1000];
public String[] ItemsRelPrice;

ArrayList<String> foodItemTitle = new ArrayList<>();

private static String currentTitle;

private String selection;
private String ttl;

//List of DataModel objects to inflate into view holders
private List<DataModel> dataModelList;


public MenuListAdapter(Activity activity, int resource, List<DataModel> dataModelList) {
    super(activity, resource, dataModelList);
    this.activity = activity;
    //set the list in constructor
    this.dataModelList = dataModelList;
}


@Nullable
@Override
public DataModel getItem(int position) {
    return dataModelList.get(position);
}

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

@Override
public long getItemId(int position) {
    return dataModelList.get(position).getId();
}

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

     ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.food_item_list, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    //findViewById codes removed because they are unnecessary
    //Copy to final object so that it can be used inside inner classes
    final ViewHolder finalHolder = holder;
    DataModel dataModel = getItem(position);

    finalHolder.foodItem.setTag(position);
    finalHolder.foodItem.setText(dataModel.getItemTitleFinal());

    finalHolder.foodPrice.setTag(position);
    finalHolder.foodPrice.setText(dataModel.getItemPriceFinal());

    finalHolder.foodType.setTag(position);
    finalHolder.foodType.setImageResource(dataModel.getItemImgFinal());

    finalHolder.addBtn.setTag(position);
    finalHolder.removeBtn.setTag(position);

    finalHolder.qty.setTag(position);
    finalHolder.priceCurnt.setTag(position);

    finalHolder.addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Log.v("btn tag", String.valueOf(finalHolder.addBtn.getTag()));
            Log.v("qty tag", String.valueOf(finalHolder.qty.getTag()));
            Log.v("Price tag", String.valueOf(finalHolder.priceCurnt.getTag()));

            int curntQty = Integer.parseInt(finalHolder.qty.getText().toString()) + 1;
            int costPerItem = Integer.valueOf(finalHolder.foodPrice.getText().toString());

            if (finalHolder.qty.getTag() == finalHolder.addBtn.getTag())
                finalHolder.qty.setText(String.valueOf(curntQty));

            if (finalHolder.priceCurnt.getTag() == finalHolder.addBtn.getTag())
                finalHolder.priceCurnt.setText(String.valueOf(costPerItem * curntQty));

            new MenuPage().addItem(costPerItem);

        }
    });

    finalHolder.removeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (finalHolder.qty.getText().toString() != "0") {

                int qtyTtl = Integer.parseInt(finalHolder.qty.getText().toString()) - 1;
                finalHolder.qty.setText(String.valueOf(qtyTtl));

                int costPerItem = Integer.valueOf(finalHolder.foodPrice.getText().toString());
                int curPrice = Integer.valueOf(finalHolder.priceCurnt.getText().toString());

                finalHolder.priceCurnt.setText(String.valueOf(curPrice - costPerItem));

                new MenuPage().removeItem(costPerItem);

            } else {
                Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
            }
        }
    });

    return convertView;
}


private static class ViewHolder {

    private TextView foodItem;
    private TextView foodPrice;
    private ImageView foodType;
    private ImageButton addBtn;
    private ImageButton removeBtn;
    private TextView qty;
    private TextView priceCurnt;


    public ViewHolder(View v) {

        foodItem = v.findViewById(R.id.item_name);
        foodPrice = v.findViewById(R.id.per_item_cost);
        foodType = v.findViewById(R.id.fdTypeImg);
        addBtn = v.findViewById(R.id.img_add);
        removeBtn = v.findViewById(R.id.img_remove);
        qty = v.findViewById(R.id.item_quantity);
        priceCurnt = v.findViewById(R.id.txt_cost);
    }
}
}

答案 1 :(得分:0)

更改以下代码:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    ViewHolder holder = null;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.food_item_list, parent, false);
        holder = new ViewHolder(convertView);

        final ImageButton addBtn = convertView.findViewById(R.id.img_add);
        final ImageButton removeBtn = convertView.findViewById(R.id.img_remove);

        final TextView qty = convertView.findViewById(R.id.item_quantity);
        final TextView priceTtl = convertView.findViewById(R.id.txt_cost);
        final TextView foodItem = convertView.findViewById(R.id.item_name);
        final TextView foodPrice = convertView.findViewById(R.id.per_item_cost);

        addBtn.setTag(position);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.v("btn tag", String.valueOf(addBtn.getTag()));
                Log.v("qty tag", String.valueOf(qty.getTag()));
                Log.v("Price tag", String.valueOf(priceTtl.getTag()));

                int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
                int costPerItem = Integer.valueOf(foodPrice.getText().toString());

                if (qty.getTag() == addBtn.getTag())
                    qty.setText(String.valueOf(curntQty));

                if (priceTtl.getTag() == addBtn.getTag())
                    priceTtl.setText(String.valueOf(costPerItem * curntQty));

                new MenuPage().addItem(costPerItem);

            }
        });

        removeBtn.setTag(position);
        removeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (qty.getText().toString() != "0") {

                    int qtyTtl = Integer.parseInt(qty.getText().toString()) - 1;
                    qty.setText(String.valueOf(qtyTtl));

                    int costPerItem = Integer.valueOf(foodPrice.getText().toString());
                    int curPrice = Integer.valueOf(priceTtl.getText().toString());

                    priceTtl.setText(String.valueOf(curPrice - costPerItem));

                    new MenuPage().removeItem(costPerItem);

                } else {
                    Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
                }
            }
        });


        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    DataModel dataModel = getItem(position);

    holder.foodItem.setTag(position);
    holder.foodItem.setText(dataModel.getItemTitleFinal());

    holder.foodPrice.setTag(position);
    holder.foodPrice.setText(dataModel.getItemPriceFinal());

    holder.foodType.setTag(position);
    holder.foodType.setImageResource(dataModel.getItemImgFinal());

    holder.addBtn.setTag(position);
    holder.removeBtn.setTag(position);
    holder.qty.setTag(position);
    holder.priceCurnt.setTag(position);

    return convertView;
}


private static class ViewHolder {

    private TextView foodItem;
    private TextView foodPrice;
    private ImageView foodType;
    private ImageButton addBtn;
    private ImageButton removeBtn;
    private TextView qty;
    private TextView priceCurnt;


    public ViewHolder(View v) {

        foodItem = v.findViewById(R.id.item_name);
        foodPrice = v.findViewById(R.id.per_item_cost);
        foodType = v.findViewById(R.id.fdTypeImg);
        addBtn = v.findViewById(R.id.img_add);
        removeBtn = v.findViewById(R.id.img_remove);
        qty = v.findViewById(R.id.item_quantity);
        priceCurnt = v.findViewById(R.id.txt_cost);
    }
}

要:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    ViewHolder holder = null;


    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.item_stock, parent, false);
        convertView = inflater.inflate(R.layout.food_item_list, parent, false);
        holder = new ViewHolder(convertView);

        final ImageButton addBtn = convertView.findViewById(R.id.img_add);
        final ImageButton removeBtn = convertView.findViewById(R.id.img_remove);

        final TextView qty = convertView.findViewById(R.id.item_quantity);
        final TextView priceTtl = convertView.findViewById(R.id.txt_cost);
        final TextView foodItem = convertView.findViewById(R.id.item_name);
        final TextView foodPrice = convertView.findViewById(R.id.per_item_cost);


        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    DataModel dataModel = getItem(position);

    holder.foodItem.setTag(position);
    holder.foodItem.setText(dataModel.getItemTitleFinal());

    holder.foodPrice.setTag(position);
    holder.foodPrice.setText(dataModel.getItemPriceFinal());

    holder.foodType.setTag(position);
    holder.foodType.setImageResource(dataModel.getItemImgFinal());

    holder.addBtn.setTag(position);
    holder.removeBtn.setTag(position);
    holder.qty.setTag(position);
    holder.priceCurnt.setTag(position);


    holder.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.v("btn tag", String.valueOf(addBtn.getTag()));
                Log.v("qty tag", String.valueOf(qty.getTag()));
                Log.v("Price tag", String.valueOf(priceTtl.getTag()));

                int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
                int costPerItem = Integer.valueOf(foodPrice.getText().toString());

                if (qty.getTag() == addBtn.getTag())
                    qty.setText(String.valueOf(curntQty));

                if (priceTtl.getTag() == addBtn.getTag())
                    priceTtl.setText(String.valueOf(costPerItem * curntQty));

                new MenuPage().addItem(costPerItem);

            }
        });

        holder.removeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (qty.getText().toString() != "0") {

                    int qtyTtl = Integer.parseInt(qty.getText().toString()) - 1;
                    qty.setText(String.valueOf(qtyTtl));

                    int costPerItem = Integer.valueOf(foodPrice.getText().toString());
                    int curPrice = Integer.valueOf(priceTtl.getText().toString());

                    priceTtl.setText(String.valueOf(curPrice - costPerItem));

                    new MenuPage().removeItem(costPerItem);

                } else {
                    Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
                }
            }
        });

    return convertView;
}


private static class ViewHolder {

    private TextView foodItem;
    private TextView foodPrice;
    private ImageView foodType;
    private ImageButton addBtn;
    private ImageButton removeBtn;
    private TextView qty;
    private TextView priceCurnt;


    public ViewHolder(View v) {

        foodItem = v.findViewById(R.id.item_name);
        foodPrice = v.findViewById(R.id.per_item_cost);
        foodType = v.findViewById(R.id.fdTypeImg);
        addBtn = v.findViewById(R.id.img_add);
        removeBtn = v.findViewById(R.id.img_remove);
        qty = v.findViewById(R.id.item_quantity);
        priceCurnt = v.findViewById(R.id.txt_cost);
    }
}

答案 2 :(得分:0)

将代码addBtn.setOnClickListene更改为removeBtn button.setOnClickListener

并执行相同操作
addBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        Log.v("btn tag", String.valueOf(addBtn.getTag()));
        Log.v("qty tag", String.valueOf(qty.getTag()));
        Log.v("Price tag", String.valueOf(priceTtl.getTag()));

        int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
        // update your quantity to data model here
        dataModel.updateQuantiy(curntQty);

        int costPerItem = Integer.valueOf(foodPrice.getText().toString());

        /*if (qty.getTag() == addBtn.getTag())
            qty.setText(String.valueOf(curntQty));*/

        if (priceTtl.getTag() == addBtn.getTag())
            priceTtl.setText(String.valueOf(costPerItem * curntQty));

        new MenuPage().addItem(costPerItem);

        // Notify that the data set change at the position to re-binding layout.
        notifyItemChanged(position);
    }
});

并且

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

    // Your normal code

    DataModel dataModel = getItem(position);

    holder.foodItem.setTag(position);
    holder.foodItem.setText(dataModel.getItemTitleFinal());

    holder.foodPrice.setTag(position);
    holder.foodPrice.setText(dataModel.getItemPriceFinal());

    holder.foodType.setTag(position);
    holder.foodType.setImageResource(dataModel.getItemImgFinal());

    holder.addBtn.setTag(position);
    holder.removeBtn.setTag(position);
    holder.qty.setTag(position);
    holder.priceCurnt.setTag(position);

   // add your quantity to textview
   holder.qty.setText(dataModel.getQuantity());

    return convertView;
}