如何为列表视图中的每个项目定义特定操作

时间:2018-09-25 16:48:33

标签: java android arrays string listview

好吧,所以我的列表视图有一些行,在这些行中有一个文本视图,必须通过该行中的两个按钮进行修改。文本视图是一个数字(0),一个按钮使该数字递增,另一个按钮使该数字递减。

问题在于,当我增加或减少列表中一项的数量时,此数字不会停留在其行中,而在我上下滚动时会移至其他项。如果您不明白这一点,请观看以下视频:https://drive.google.com/open?id=11UYnu-sebZcbUIiG9oLoXtF8UeP1PAuv

那么,我怎样才能使这些数字保持不变?这是代码

CartListActivity.java

public class CartListActivity extends AppCompatActivity {

    ListView userList;
    UserCustomAdapter userAdapter;
    ArrayList<User> userArray = new ArrayList<User>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart_list);

        /**
         * add item in arraylist
         */
        userArray.add(new User("Olives verdes", "1,20€", "Aperitius", "0"));
        userArray.add(new User("Olives negres", "1,30€", "Aperitius", "0"));
        userArray.add(new User("Cogombrets", "1,80€", "Aperitius","0"));
        userArray.add(new User("Patates Lay's", "1,19€", "Aperitius","0"));
        userArray.add(new User("Potet", "0,95€", "Bebè","0"));
        userArray.add(new User("Biberó", "3,85€", "Bebè","0"));
        userArray.add(new User("Tovalloletes", "1,10€", "Bebè","0"));
        userArray.add(new User("Perxes de nadó", "1,95€", "Bebè","0"));
        /**
         * set item into adapter
         */
        userAdapter = new UserCustomAdapter(CartListActivity.this, R.layout.view_row,
                userArray);
        userList = (ListView) findViewById(R.id.listView);
        userList.setItemsCanFocus(false);
        userList.setAdapter(userAdapter);
        /**
         * get on item click listener
         */
        userList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    final int position, long id) {
                Log.i("List View Clicked", "**********");
                Toast.makeText(CartListActivity.this,
                        "List View Clicked:" + position, Toast.LENGTH_LONG)
                        .show();
            }
        });

    }

}

User.java

public class User {
    String name;
    String price;
    String category;
    String quantity;

    public String getName() {
        return name;
    }

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

    public String getPrice() {
        return price;
    }

    public void setPrice(String address) {
        this.price = price;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public User(String name, String address, String location, String quantity) {
        super();
        this.name = name;
        this.price = price;
        this.category = category;
        this.quantity = quantity;
    }

}

UserCustomAdapter.java

public class UserCustomAdapter extends ArrayAdapter<User> {
    Context context;
    int layoutResourceId;
    ArrayList<User> data = new ArrayList<User>();

    public UserCustomAdapter(Context context, int layoutResourceId,
                             ArrayList<User> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        UserHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new UserHolder();
            holder.textName = (TextView) row.findViewById(R.id.textView1);
            holder.textPrice = (TextView) row.findViewById(R.id.textView2);
            holder.textCategory = (TextView) row.findViewById(R.id.textView3);
            holder.textQuantity = (TextView) row.findViewById(R.id.quantityTxt);
            holder.btnEdit = (Button) row.findViewById(R.id.button1);
            holder.btnDelete = (Button) row.findViewById(R.id.button2);
            row.setTag(holder);
        } else {
            holder = (UserHolder) row.getTag();
        }
        User user = data.get(position);
        holder.textName.setText(user.getName());
        holder.textPrice.setText(user.getPrice());
        holder.textCategory.setText(user.getCategory());
        final UserHolder finalHolder = holder;
        holder.btnEdit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String value1 = finalHolder.textQuantity.getText().toString();
                int a = Integer.parseInt(value1);

                if(a <= 99) {
                    int sum = a + 1;

                    Log.i("Edit Button Clicked", "**********");
                    finalHolder.textQuantity.setText(Integer.toString(sum));
                }
            }
        });
        holder.btnDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                    String value2 = finalHolder.textQuantity.getText().toString();
                    int a = Integer.parseInt(value2);

                    if(a > 0) {
                        int substract = a - 1;
                        Log.i("Delete Button Clicked", "**********");
                        finalHolder.textQuantity.setText(Integer.toString(substract));
                    }
            }
        });
        return row;

    }

    static class UserHolder {
        TextView textName;
        TextView textPrice;
        TextView textCategory;
        TextView textQuantity;
        Button btnEdit;
        Button btnDelete;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在每次调用getView时设置userQuantity,并在单击Edit或Delete时更新User对象中的数量值。添加如下所示的行:

    final User user = data.get(position);   // CHANGE TO FINAL
    holder.textName.setText(user.getName());
    holder.textPrice.setText(user.getPrice());
    holder.textCategory.setText(user.getCategory());
    holder.textQuantity.setText(user.getQuantity()); // ADD THIS LINE
    final UserHolder finalHolder = holder;
    holder.btnEdit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String value1 = finalHolder.textQuantity.getText().toString();
            int a = Integer.parseInt(value1);

            if(a <= 99) {
                int sum = a + 1;

                Log.i("Edit Button Clicked", "**********");
                user.setQuantity(Integer.toString(sum)); // ADD THIS LINE
                finalHolder.textQuantity.setText(Integer.toString(sum));
            }
        }
    });
    holder.btnDelete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                String value2 = finalHolder.textQuantity.getText().toString();
                int a = Integer.parseInt(value2);

                if(a > 0) {
                    int substract = a - 1;
                    Log.i("Delete Button Clicked", "**********");

                     user.setQuantity(Integer.toString(subtract)); // ADD THIS LINE

                 finalHolder.textQuantity.setText(Integer.toString(substract));
                }
        }
    });