如何将项目添加到 listView(重复项增加数量)并在以下活动中查看?

Posted

技术标签:

【中文标题】如何将项目添加到 listView(重复项增加数量)并在以下活动中查看?【英文标题】:How do I add items to a listView (for duplicates increase quantity) and view in following activity? 【发布时间】:2020-02-05 09:13:24 【问题描述】:

我有一个包含项目的 gridView,每个项目都有一个名称和数量。当我单击 gridView 中的项目时,我想将项目和数量添加到同一活动中的动态列表视图中。如果我多次单击该项目,则应增加动态 listView 中的项目数量(不允许重复项目。)这是我当前的 gridView,我已经删除了添加的代码和之前失败的 listView,我是现在就干杯吧:

    public class GridviewAdapter extends BaseAdapter

    private ArrayList<String> listItem;
    private ArrayList<Integer> listPicture;
    private Activity activity;

    public GridviewAdapter(Activity activity, ArrayList<String> listItem, ArrayList<Integer> listPicture) 
        super();
        this.listItem = listItem;
        this.listPicture = listPicture;
        this.activity = activity;
    

    @Override
    public int getCount() 
        // TODO Auto-generated method stub
        return listItem.size();
    

    @Override
    public String getItem(int position) 
        // TODO Auto-generated method stub
        return listItem.get(position);
    

    @Override
    public long getItemId(int position) 
        // TODO Auto-generated method stub
        return 0;
    

    public static class ViewHolder
    
        public ImageView itemPicture;
        public TextView itemName;
        public TextView itemPrice;
        public TextView itemStock;
        public TextView itemAvailability;
    

    @Override
    public View getView(int position, View convertView, ViewGroup 
 parent) 
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if(convertView==null)
        
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.item_grid_row, 
 null);

            view.itemName = (TextView) 
convertView.findViewById(R.id.itemName);
            view.itemPrice = (TextView) 
convertView.findViewById(R.id.itemPrice);
            view.itemStock = (TextView) 
convertView.findViewById(R.id.itemStock);
            view.itemAvailability = (TextView) 
convertView.findViewById(R.id.itemAvailability);
            view.itemPicture = (ImageView) 
convertView.findViewById(R.id.itemPicture);

            convertView.setTag(view);
        
        else
        
            view = (ViewHolder) convertView.getTag();
        

        view.itemName.setText(listItem.get(position));
       // view.itemPicture.setImageDrawable(listItem.get(position));

        return convertView;
    


这是单击 gridView 的结帐片段的代码。

    public class CheckoutFragment extends Fragment 

    private CheckoutViewModel checkoutViewModel;
    private EditText editText1;

    private GridviewAdapter mAdapter;
    private ArrayList<String> listItem;
    private ArrayList<Integer> listPicture;
    private GridView gridView;

    DBHelper db;

    LinearLayout layout_total, layout_grid;

    private Button sum;


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle 
savedInstanceState) 
        View root = inflater.inflate(R.layout.fragment_checkout, 
 container, false);

        db = new DBHelper(getActivity());

        Display display = 
getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        editText1 = (EditText)root.findViewById(R.id.editText1);
        editText1.setHeight(height/12);
        editText1.setShowSoftInputOnFocus(false);
        editText1.setFocusable(false);

        editText1.setTextColor(Color.parseColor("#FFFFFF"));
        editText1.setHintTextColor(Color.parseColor("#FFFFFF"));

editText1.getBackground().setColorFilter(Color.parseColor("#008577"), 
PorterDuff.Mode.SRC_ATOP);

        editText1.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Intent cart = new Intent(getActivity(), 
CartActivity.class);
                startActivity(cart);
            
        );

        prepareList();
        // prepared arraylist and passed it to the Adapter class
        mAdapter = new GridviewAdapter(getActivity(),db.getAll(), 
listPicture);

        // Set custom adapter to gridview
        gridView = (GridView)root.findViewById(R.id.gridView1);
        gridView.setAdapter(mAdapter);

        // Implement On Item click listener
        gridView.setOnItemClickListener(new 
AdapterView.OnItemClickListener()
        
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int 
position,
                                    long arg3) 
//this is where the grid is clicked and items added to dynamic listView
                Toast.makeText(getActivity(), 
mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
            
        );

        return root;
    

    public void prepareList() 
        // listItem = new ArrayList<String>();

        db = new DBHelper(getActivity());
        List<Item> listItem = new ArrayList<>();

        listItem =  db.getAllItems(db.ITEMS_TABLE_NAME);
    

    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
        inflater.inflate(R.menu.main, menu);
    

    public boolean onOptionsItemSelected(MenuItem item) 
        super.onOptionsItemSelected(item);
        switch(item.getItemId()) 
            case R.id.action_add_customer:
                //startActivity(getActivity(), 
NewCustomerActivity.class);
                Intent newCustomer = new Intent(getActivity(), 
NewCustomerActivity.class);
                startActivity(newCustomer);
                return true;
           /* case R.id.action_add_customer:
                return true;*/
            default:
                return super.onOptionsItemSelected(item);
        
    

【问题讨论】:

适配器类中的用户接口,并在 Checkfragment 类中实现和实现此接口。并在 setadapter 语句之后编写 mAdapter .notifydatasetchange。 【参考方案1】:

你需要更好地解释一下情况。现在我假设您的问题是这些项目正在复制第二个列表而不是附加计数。为此,没有 一条线解决方案。您需要做的是在网格中选择一个项目后,搜索它是否存在于第二个列表中。在这种情况下更新数量,否则将其添加到列表中。由于您的第二个列表基于 Table ,因此您必须执行 2 个查询。一种用于识别项目是否已存在(选择查询),另一种用于更改值(插入或更新查询)。如果我误解了,请纠正我,以便我可以更好地帮助你。

你为什么不在你的 GridView getItem(..) 方法中返回模型类而不是 'String' 它应该像这样工作

public class GridviewAdapter extends BaseAdapter

    ArrayList<GridModel> gridItems;
    //Constructor
    public GridviewAdapter(Activity activity, ArrayList<GridModel> gridItems) 
        super();
        this.gridItems = gridItems;
        this.activity = activity;
    

    @Override
    public int getCount() 
        // TODO Auto-generated method stub
        return griditems.size();
    

    @Override
    public GridModel getItem(int position) 
        // TODO Auto-generated method stub
        return gridItems.get(position);
    

    //...add your getView...



//GridModel Should be Like So..
class GridModel 
    public String itemPicture;
    public String itemName;
    public String itemPrice;
    public String itemStock;
    public String itemAvailability;

    //or use private variables with getter-setter methods

【讨论】:

我想要的是获取上面网格中物品的价格,当我点击网格时,吐司基本上显示了物品的名称,我如何从视图中获取物品.

以上是关于如何将项目添加到 listView(重复项增加数量)并在以下活动中查看?的主要内容,如果未能解决你的问题,请参考以下文章

用动画展开 ListView 项

如何检测ListView项的位置?

Xamarin Listview 仅添加 1 项我的列表中有 2 项

在listview中的addTextChangedListener中使用自定义列表适配器

Xamarin 将 ListView 项动态更新到 GUI

向 Firebase 添加新项目后 ListView 重复 [重复]