Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Usually, when you need to obtain data from views inside a ListView (or a GridView), it is not quite as simple as iterating over these views, and accessing their properties: for example, checked state of a CheckBox, selected option in a RadioGroup, and in your case, user's input in an EditText. The reason why this isn't simple: ListViews serve Views on demand. </p> <p>Say you have a ListView which has 20 items to show. Given a certain screen size, it can probably display 8 items, and you need to scroll to access the rest. If at this point, you try to access all 20 Views, you will most certainly run into a <code>NPE</code>: because all 20 Views simply don't exist. On scroll, older views (those that are not on screen anymore) are recycled and/or new views are created. </p> <blockquote> <p>I think I shoud add some code in the function public Object getItem(int arg0)... but I don't know how to do</p> </blockquote> <p>This method's purpose is to serve data to the BaseAdapter. Its <em>mostly</em> used in the getView(...) method to return an Object using the <code>position</code> argument. Method's body, in your case, will look like this:</p> <pre><code>@Override public Object getItem(int arg0) { return mListNumber.get(arg0); } </code></pre> <p>Its usage:</p> <pre><code>@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { .... } else { .... } String currentItem = (String) getItem(position); // use 'currentItem' } </code></pre> <p><strong>Solution:</strong></p> <p>Maintain data yourself. </p> <p>Create an <code>ArrayList&lt;String&gt;</code> inside your BaseAdapter:</p> <pre><code>public class ListNumberAdapter extends BaseAdapter { private LayoutInflater mInflater; private List&lt;String&gt; mListNumber; private Context mContext; // List private ArrayList&lt;String&gt; mListData; public ListNumberAdapter(Context context, List&lt;String&gt; listNumber){ mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mListNumber=listNumber; mContext=context; // Create and initialize the list mListData = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; mListNumber.size(); i++) { mListData.add(""); } } .... @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { .... } else { .... } .... // Update 'mListData' whenever EditText's content changes holder.editNumber.addTextChangedListener(new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {} public void afterTextChanged(Editable s) { // Text has changed mListData.set(position, s.toString()) } }); return convertView; } // Use this method to obtain 'mListData' public ArrayList&lt;String&gt; getCurrentEditTextContent() { return mListData; } } </code></pre> <p>So, from your activity, you would obtain the data using:</p> <pre><code>ArrayList&lt;String&gt; currentContent = yourAdapter.getCurrentEditTextContent(); </code></pre> <p>By the way, you will also have to change <code>holder.btnDelete's</code> OnClickListener: when you remove an object from <code>mListNumber</code>, do the same operation for <code>mListData</code> to maintain consistency. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload