所以我的问题是我在onitemclicklistener
设置listview1
,并且如果订单列表中已经存在项目,那么数量会增加,但我的问题是我的程序保持崩溃
这是我的代码
MainActivity.java
public class MainActivity extends Activity {
Database helper;
ListView lv,lv2;
ArrayAdapter<PostItemList> adapter;
ArrayAdapter<PostInventory> adapterInventory;
ArrayAdapter<PostOrder> adapterOrder;
ArrayList<PostItemList> list;
ArrayList<PostInventory> listInventory;
ArrayList<PostOrder> listorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new Database(MainActivity.this);
lv = (ListView)findViewById(R.id.itemList);
lv2 = (ListView)findViewById(R.id.orderList);
list = helper.getAllData();
adapter = new PostAdapterItemList(MainActivity.this,list);
lv.setAdapter(adapter);
listorder = new ArrayList<PostOrder>();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,final int pos,
long arg3) {
// TODO Auto-generated method stub
PostItemList getData = list.get(pos);
String parseData = getData.toStringOrder();
String name[] = parseData.split(",");
int itemQuantity = Integer.parseInt(name[1]);
double itemPrice = Double.parseDouble(name[2]);
if (itemQuantity == 0){
String output = name[0] + " is out of stock.";
Toast.makeText(getBaseContext(), output, Toast.LENGTH_LONG).show();
} else {
itemQuantity = itemQuantity - 1;
String newQuantity = String.valueOf(itemQuantity);
helper.updateQuantity(name[0], newQuantity);
list.clear();
list = helper.getAllData();
adapter = new PostAdapterItemList(MainActivity.this, list);
lv.setAdapter(adapter);
int checker = 0;
if (listorder.isEmpty()){
listorder.add(new PostOrder(name[0], "1", name[2]));
adapterOrder = new PostAdapterOrder(MainActivity.this,listorder);
lv2.setAdapter(adapterOrder);
} else if (listorder.size()>0){
for(PostOrder order : listorder){
if (name[0].contentEquals(order.getItemName())){
String quantity = order.getItemQuantity();
order.setItemQuantity(String.valueOf((Integer.parseInt(quantity))+1));
adapterOrder.notifyDataSetChanged();
} else {
checker = 1;
}
}
}
if (checker==1){
listorder.add(new PostOrder(name[0], "1", name[1]));
adapterOrder = new PostAdapterOrder(MainActivity.this, listorder);
lv2.setAdapter(adapterOrder);
}
}
}
});
}
logcat的
10-11 00:05:13.190: D/dalvikvm(854): GC_FOR_ALLOC freed 57K, 5% free 3034K/3176K, paused 32ms, total 33ms
10-11 00:05:13.190: I/dalvikvm-heap(854): Grow heap (frag case) to 3.646MB for 635812-byte allocation
10-11 00:05:13.290: D/dalvikvm(854): GC_FOR_ALLOC freed 0K, 4% free 3655K/3800K, paused 22ms, total 22ms
10-11 00:05:13.870: I/Choreographer(854): Skipped 73 frames! The application may be doing too much work on its main thread.
10-11 00:05:13.950: D/gralloc_goldfish(854): Emulator without GPU emulation detected.
10-11 00:05:14.230: I/Choreographer(854): Skipped 37 frames! The application may be doing too much work on its main thread.
10-11 00:05:20.070: D/AndroidRuntime(854): Shutting down VM
10-11 00:05:20.070: W/dalvikvm(854): threadid=1: thread exiting with uncaught exception (group=0xb3a67ba8)
10-11 00:05:20.080: E/AndroidRuntime(854): FATAL EXCEPTION: main
10-11 00:05:20.080: E/AndroidRuntime(854): Process: com.example.posv3, PID: 854
10-11 00:05:20.080: E/AndroidRuntime(854): java.util.ConcurrentModificationException
10-11 00:05:20.080: E/AndroidRuntime(854): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
10-11 00:05:20.080: E/AndroidRuntime(854): at com.example.posv3.MainActivity$1.onItemClick(MainActivity.java:71)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.widget.AbsListView$3.run(AbsListView.java:3638)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.os.Handler.handleCallback(Handler.java:733)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.os.Handler.dispatchMessage(Handler.java:95)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.os.Looper.loop(Looper.java:136)
10-11 00:05:20.080: E/AndroidRuntime(854): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-11 00:05:20.080: E/AndroidRuntime(854): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 00:05:20.080: E/AndroidRuntime(854): at java.lang.reflect.Method.invoke(Method.java:515)
10-11 00:05:20.080: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-11 00:05:20.080: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-11 00:05:20.080: E/AndroidRuntime(854): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
如果您要从iteration
for(Iterator<MyAdapter> it = result.iterator(); it.hasNext();) {
MyAdapter s = it.next();
if(s.getClientPoints().getPointsSpent() == 0) {
it.remove();
}
}
答案 1 :(得分:0)
Sir Himanshu Agarwal,谢谢你的帮助。我解决了这个问题,并且在列表中已经列出的项目列出了我的解决方案
for(PostOrder order : listorder){
if (name[0].contentEquals(order.getItemName())){
String quantity = order.getItemQuantity();
order.setItemQuantity(String.valueOf((Integer.parseInt(quantity))+1));
adapterOrder.notifyDataSetChanged();
break;
} else {
counter++;
}
}
}
if (counter == listorder.size()){
listorder.add(new PostOrder(name[0], "1", name[1]));
adapterOrder = new PostAdapterOrder(MainActivity.this, listorder);
lv2.setAdapter(adapterOrder);
}