简体   繁体   中英

Getting EditText value from ListView works… Sometimes

So I have a ListView with custom Cells that have an EditText in them. I put together a function that is called on request, which spits out a nice list of all the values of each of the Edit Texts. All the cells are created from a static list, and they are all exactly the same. , and then it crashes: ,然后崩溃:

ListView l = (ListView) CartPage.this.findViewById(R.id.cartItems);
// total_item_count is equal to the int 11, which is how many cells there are.
for(int i=0;i<total_item_count-1;i++){
    View view=l.getChildAt(i);
    System.out.println("("+i+"/"+total_item_count+")");
    EditText editText=(EditText) view.findViewById(R.id.product_count);
    String string=editText.getText().toString();
    System.out.println(i+": "+string);

}

Here is my error log:

02-17 06:44:17.540: E/AndroidRuntime(29086): FATAL EXCEPTION: main
02-17 06:44:17.540: E/AndroidRuntime(29086): java.lang.IllegalStateException: Could not execute method of the activity
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.view.View$1.onClick(View.java:3617)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.view.View.performClick(View.java:4222)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.view.View$PerformClick.run(View.java:17397)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.os.Handler.handleCallback(Handler.java:725)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.os.Looper.loop(Looper.java:137)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.app.ActivityThread.main(ActivityThread.java:5167)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at java.lang.reflect.Method.invoke(Method.java:511)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at dalvik.system.NativeStart.main(Native Method)
02-17 06:44:17.540: E/AndroidRuntime(29086): Caused by: java.lang.reflect.InvocationTargetException
02-17 06:44:17.540: E/AndroidRuntime(29086):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at java.lang.reflect.Method.invoke(Method.java:511)
02-17 06:44:17.540: E/AndroidRuntime(29086):    at android.view.View$1.onClick(View.java:3612)
02-17 06:44:17.540: E/AndroidRuntime(29086):    ... 11 more
02-17 06:44:17.540: E/AndroidRuntime(29086): Caused by: java.lang.NullPointerException
02-17 06:44:17.540: E/AndroidRuntime(29086):    at com.venuevip.android.CartPage.updateCart(CartPage.java:305)
02-17 06:44:17.540: E/AndroidRuntime(29086):    ... 14 more

I have tried cleaning my project 10 times now, to no avail.

ListView l = (ListView) CartPage.this.findViewById(R.id.cartItems);
EditText editText=(EditText) view.findViewById(R.id.product_count);
String string;
// total_item_count is equal to the int 11, which is how many cells there are.
for(int i=0;i<total_item_count-1;i++){
    View view=l.getChildAt(i);
    System.out.println("("+i+"/"+total_item_count+")");

    string=editText.getText().toString();
    System.out.println(i+": "+string);

}

Declare edittext instance outside of for loop

You shouldn't iterate the views in a ListView and assume that it actually contains a separate view for each item in the list. It doesn't, ListView recycles views and only swaps the data according to the ListAdapter when scrolling through.

Instead you should iterate your data in your ListAdapter , but that means that you will most likely have to modify your adapter code to save the text in the EditTexts somewhere when they are edited.

you can use baseadapter class to set a custom list which is much more convenient. You can use this article to go through the details-

http://www.vogella.com/tutorials/AndroidListView/article.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM