简体   繁体   English

从ListView获取EditText值的工作原理…有时

[英]Getting EditText value from ListView works… Sometimes

So I have a ListView with custom Cells that have an EditText in them. 所以我有一个带有自定义单元格的ListView,其中包含一个EditText。 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: 问题是,前7个单元格可以使用下面的代码正常工作 ,然后崩溃:

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). 编辑:发现它仅适用于屏幕上显示的项目数(因此,当您将其翻转为横向并且显示3个项目时,它仅适用于前3个项目)。

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 在for循环外声明edittext实例

You shouldn't iterate the views in a ListView and assume that it actually contains a separate view for each item in the list. 您不应该迭代ListView的视图,并假定它实际上为列表中的每个项目都包含一个单独的视图。 It doesn't, ListView recycles views and only swaps the data according to the ListAdapter when scrolling through. 不会, ListView回收视图,并且仅在滚动时根据ListAdapter交换数据。

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. 相反,您应该在ListAdapter迭代数据,但这意味着您很可能必须修改适配器代码才能在编辑文本时将文本保存在EditTexts某个位置。

you can use baseadapter class to set a custom list which is much more convenient. 您可以使用baseadapter类来设置自定义列表,这更加方便。 You can use this article to go through the details- 您可以使用本文详细介绍-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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