DarthCaniac
DarthCaniac

Reputation: 383

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. The problem is, the first 7 cells work fine with the code below, 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.

Edit: Discovered that it only works for the number of items displayed on screen (So when you flip it into landscape and 3 items are shown, it works only for the first 3).

Upvotes: 0

Views: 181

Answers (2)

Zharf
Zharf

Reputation: 2658

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.

Upvotes: 1

Looking Forward
Looking Forward

Reputation: 3585

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

Upvotes: 2

Related Questions