我使用简单的Adapter.i创建了一个listview。我以key和value(HashMap)的形式获取行数据。我想将特定行插入数据库但我的问题是当我获取行数据时当项目超过1时,将以键和值的形式出现,然后我如何在数据库的下一行添加数据。如果有任何人有任何想法,请告诉我。
这是方法(),通过它我将简单适配器设置为listview并迭代地图中的每个项目: -
private void showList(List<LineItem> list) {
saleList = new ArrayList<Map<String, String>>();
for (LineItem line : list) {
saleList.add(line.toMap());
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(getActivity().getBaseContext(), saleList,
R.layout.listview_lineitem, new String[]{"name", "quantity", "price"}, new int[]{R.id.name, R.id.quantity, R.id.price});
saleListView.setAdapter(sAdap);
for (Map<String, String> map : saleList) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
Log.e("key..::>>", "" + pair.getKey());
Log.e("value..::>>", "" + pair.getValue());.
}
}
}
这是我的Model类: -
public class LineItem {
private final Product product;
private int quantity;
private int id;
private double unitPriceAtSale;
public static final int UNDEFINED = -1;
public LineItem(Product product, int quantity) {
this(UNDEFINED, product, quantity, product.getUnitPrice());
}
public LineItem(Product product) {
this.product = product;
}
public LineItem(int id, Product product, int quantity,
double unitPriceAtSale) {
this.id = id;
this.product = product;
this.quantity = quantity;
this.unitPriceAtSale = unitPriceAtSale;
}
public Product getProduct() {
return product;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void addQuantity(int amount) {
this.quantity += amount;
}
public double getTotalPriceAtSale() {
return unitPriceAtSale * quantity;
}
public Map<String, String> toMap() {
Map<String, String> map = new HashMap<String, String>();
map.put("name", product.getName());
map.put("quantity", quantity + "");
map.put("price", getTotalPriceAtSale() + "");
return map;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setUnitPriceAtSale(double unitPriceAtSale) {
this.unitPriceAtSale = unitPriceAtSale;
}
public Double getPriceAtSale() {
return unitPriceAtSale;
}
@Override
public boolean equals(Object object) {
if (object == null)
return false;
if (!(object instanceof LineItem))
return false;
LineItem lineItem = (LineItem) object;
return lineItem.getId() == this.getId();
}
}
我正确地得到了名称,价格和数量。我想在数据库中添加名称,价格,数量。但问题是我得到的是名称,价格,数量,以键和值的形式(地图) 。当我想在数据库中添加这些东西但是当我在listview中有多个行数据并以key和value的形式从listview获取数据时,如何将数据插入到数据库中。因为我想插入数据库中的多行数据(例如listview行数据,我想在数据库中添加数据)。
我的logcat是: -
10-19 16:56:59.824 18890-18890/com.refresh.pos E/key..::>>: quantity
10-19 16:56:59.824 18890-18890/com.refresh.pos E/value..::>>: 1
10-19 16:56:59.824 18890-18890/com.refresh.pos E/key..::>>: price
10-19 16:56:59.824 18890-18890/com.refresh.pos E/value..::>>: 200.0
10-19 16:56:59.824 18890-18890/com.refresh.pos E/key..::>>: name
10-19 16:56:59.824 18890-18890/com.refresh.pos E/value..::>>: aman
10-19 16:56:59.824 18890-18890/com.refresh.pos E/key..::>>: quantity
10-19 16:56:59.824 18890-18890/com.refresh.pos E/value..::>>: 2
10-19 16:56:59.824 18890-18890/com.refresh.pos E/key..::>>: price
10-19 16:56:59.824 18890-18890/com.refresh.pos E/value..::>>: 5556.0
10-19 16:56:59.824 18890-18890/com.refresh.pos E/key..::>>: name
10-19 16:56:59.824 18890-18890/com.refresh.pos E/value..::>>: cghg
i want to add item in database like :-
three fields :- quantity price name
1 200.0 aman
2 5556.0 cghg
I hope anyone understand now what i want to do.
答案 0 :(得分:0)
我认为“saleList”具有地图对象。所以每个地图对象都是一行。
ContentValues cv=new ContentValues();
for (Map<String, String> map : saleList) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
if(pair.getKey().equals("name")){
cv.put(pair.getKey(),pair.getKey());
}else if(pair.getKey().equals("price")){
cv.put(pair.getKey(),pair.getKey());
}else{
cv.put(pair.getKey(),pair.getKey());
}
//here insert your contentvalues to db
}
}
我觉得每张地图只有三个值,如名称,价格,数量。
如果不正确,那么您应该为每一行创建模型类 使用这些变量并将模型类对象插入 数组列表。然后你可以轻松地将数据作为行。