简体   繁体   English

Android从GridView中的EditText获取文本

[英]Android get Text from EditText in a GridView

I have a GridView that displays several dozen rows worth of a custom layout, each of which consists of an EditText and a TextView object. 我有一个GridView,它显示几十行的自定义布局,每行包含一个EditText和一个TextView对象。

If I understand correctly, I should extend the BaseAdapter class to accomplish that. 如果我理解正确,则应该扩展BaseAdapter类来完成此操作。 If so, how can I get access to a specific EditText object? 如果是这样,我如何获得对特定EditText对象的访问权限?

Also, will recycling of views cause me to lose the text that the user has entered if one of the EditText views is no longer visible? 此外,如果其中一个EditText视图不再可见,视图的回收是否会使我丢失用户输入的文本?

Is there a simpler means to accomplish what I am describing here (perhaps something other than GridView) ? 有没有更简单的方法可以完成我在此描述的内容(也许不是GridView)?

When you extend BaseAdapter one of the methods you have to override is getView(...) . 扩展BaseAdapter ,必须重写的方法之一是getView(...) In this method, you create the view that needs to be shown. 使用这种方法,您可以创建需要显示的视图。 If you want to persist text that is entered in the EditText , what you need to do is set aa TextWatcher on the EditText, passing in the position of the EditText (parameter in getView(...) ), and whenever the text is changed, save the text in an array of sorts. 如果要保留在EditText输入的文本,则需要在EditText上设置一个TextWatcher ,传递EditText的位置( getView(...)参数),以及每当更改文本时,将文本保存为各种数组。 Then, whenever that position comes back through the getView(...) method, grab the text from the array and populate the EditText . 然后,只要该位置通过getView(...)方法返回,就从数组中获取文本并填充EditText

You should use an underlying data structure ( say a List ) which should hold the data for each element of the Grid. 您应该使用基础数据结构(例如List),该数据结构应保存Grid中每个元素的数据。

TextView and EditText should be populated from this List. 应该从此列表中填充TextView和EditText。

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

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