ListView显示重复数据

时间:2017-04-07 02:57:31

标签: android sqlite listview

这是我显示ListView的方式,我不确定这是否是正确的方法,或者不显示我的列表视图。问题是我的列表视图在我的" CondimentDescription"有多个数据,以下是我的代码,请提出建议。 The ListView show like this, but this is not what I want.
This is what I want

public List<OrderList> getOrderList() {
            List<OrderList> OrderList = new ArrayList<OrderList>();

            try {
                String selectQuery = "select t2.Id,t2.ProductCode,t2.Price,t2.Qty,t3.Description,t4.condimentDescription from TempCS t1 \n" +
                        "left outer join TempCSDetail t2 on t1.DocNo=t2.Docno\n" +
                        "left outer join mProduct t3 on t2.ProductCode=t3.Code \n" +
                        "left outer join TempCSCondiment t4 on t2.SeqNo=t4.SeqNo"+
                        "where t1.DocNo=" + TablePageDocNo + "\n";

                SQLiteDatabase db1 = db.getReadableDatabase();
                Cursor cursor = db1.rawQuery(selectQuery, null);

                if (cursor.moveToFirst()) {
                        do {
                            OrderList list = new OrderList();
                           list.setProductCode(cursor.getString(cursor.getColumnIndex("ProductCode")));
                            list.setPrice(Double.parseDouble(cursor.getString(cursor.getColumnIndex("Price"))));
                            list.setQty(cursor.getString(cursor.getColumnIndex("Qty")));
                            list.setDescription(cursor.getString(cursor.getColumnIndex("Description")));
                            list.set_ID(Integer.parseInt(cursor.getString(cursor.getColumnIndex("ID"))));
             //this will more than 1 row in my Sqlite database  
                            list.setCondimentDescription(cursor.getString(cursor.getColumnIndex("CondimentDescription")));
                                    OrderList.add(list);
                                } while (cursor.moveToNext());
                            }

            }catch(Exception e){
            }

        return OrderList;
    }

适配器列表

public class  OrderListAdapter extends BaseAdapter {
        LayoutInflater mInflater;
        public OrderListAdapter() {
            mInflater = LayoutInflater.from(ListFragmentActivity.this);
        }
        @Override
        public int getCount() {
            return orderlist.size();
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listproduct1, null);
            }

            final TextView _ID = (TextView) convertView.findViewById(R.id.proID);
            _ID.setText("" + orderlist.get(position).get_ID());
            final String proID = _ID.getText().toString();

            final TextView ProductCode = (TextView) convertView.findViewById(R.id.productcode);
            ProductCode.setText("" + orderlist.get(position).getProductCode());

            final TextView Description = (TextView) convertView.findViewById(R.id.description);
            Description.setText("" + orderlist.get(position).getDescription());

            final TextView Price = (TextView) convertView.findViewById(R.id.price);
        Price.setText("" + String.format("%.2f",orderlist.get(position).getPrice()));
        final Double price= Double.valueOf(Price.getText().toString());

        final TextView Qty = (TextView) convertView.findViewById(R.id.qty);
        Qty.setText("" + orderlist.get(position).getQty());

        final TextView condimentDescription=(TextView)convertView.findViewById(R.id.condimentDescription);
        condimentDescription.setText("" + orderlist.get(position).getCondimentDescription());

        notifyDataSetChanged();
        return convertView;
    }
}

3 个答案:

答案 0 :(得分:0)

每次你应该在do-while循环中创建OrderList的新对象。将其从功能顶部移除。

答案 1 :(得分:0)

循环前清除列表。

 OrderList.clear();

if (cursor.moveToFirst()) {

之前添加明确声明

如果列表中重复的项目,则可以使用此代码将其删除。

List<OrderList> OrderList  = new ArrayList<>();
// add elements to al, including duplicates
Set<OrderList> hs = new HashSet<>();
hs.addAll(OrderList);
OrderList.clear();
OrderList.addAll(hs);

答案 2 :(得分:0)

select distinct t2.Id as ID,t2.ProductCode,t2.Price,t2.Qty,t3.Description from TempCS t1 \n" +
                        "left outer join TempCSDetail t2 on t1.DocNo=t2.Docno\n" +
                        "left outer join mProduct t3 on t2.ProductCode=t3.Code \n" +
                        "left outer join TempCSCondiment t4 on t1.column_name=t4.column_name"+
                        "where t1.DocNo=" + TablePageDocNo + "\n";

试试这个