Teshte
Teshte

Reputation: 632

EditTexts in ListView complete more then once

My problem is kind of strange...basically I have a ListView like the one from above :

<ListView
    android:id="@+id/listViewMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/order_button"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >

</ListView>

and for it, i have defined the elements to look like this :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.7">

        <TextView android:id="@+id/name"
          android:textSize="14sp"
          android:textStyle="bold"
          android:textColor="#0033CC"
          android:layout_marginLeft="10sp"
          android:layout_marginTop="5sp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
        />

        <TextView android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

        <TextView android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

    </LinearLayout>

    <EditText
        android:id="@+id/quantityEdit"
        android:layout_height="85dp"
        android:layout_width="85dp"
        android:textSize="12sp"
        android:textColor="#0033CC"
        android:layout_marginRight="15dp"
        android:layout_weight="0.3"
        android:inputType="number"
        android:ems="10"
        android:hint="@string/quantity_show">

        <requestFocus />
    </EditText>

    </LinearLayout>

and this is my NEW EDITED addapter

public MyCustomBaseAdapter(Context context, ArrayList<Product> products) {
         this.products = products;
         this.mInflater = LayoutInflater.from(context);

         for (int i=0;i<products.size();i++){
             editTextsVlues.add("");
         }
     }

     public int getCount() {
         return products.size();
     }

     public Object getItem(int position) {
         return products.get(position);
     }

     public long getItemId(int position) {
         return position;
     }

     public View getView(int position, View convertView, ViewGroup parent) {

         ViewHolder holder;

         if (convertView == null) {
             convertView = mInflater.inflate(R.layout.product_item, null);
             holder = new ViewHolder();
             holder.quantity = (EditText) convertView.findViewById(R.id.quantityEdit);
             holder.picView = (ImageView) convertView.findViewById(R.id.pic_view);

             convertView.setTag(holder);
         } else {
             holder = (ViewHolder) convertView.getTag();
         }



         holder.quantity.setText(editTextsVlues.get(position));

         holder.picView.setImageBitmap(products.get(position).getPicture());

         positionListener = position;

         holder.quantity.addTextChangedListener(new TextWatcher() {

             @Override
             public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                 // When user changed the Text
                 editTextsVlues.set(positionListener, cs.toString());
             }

             @Override
             public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                     int arg3) {
                 // TODO Auto-generated method stub

             }

             @Override
             public void afterTextChanged(Editable arg0) {
                 // TODO Auto-generated method stub

             }
         });

         return convertView;
     }


} 

The problem that I facing is that when i have more elements in the list (they don't fit all in my screen), when i enter a value in a EditText, then another EditText from below is beeing populated...Why is that happening...? I have found no similar cases on the internet .

EDTI: Sam i tried your approach..however my programming skils aren't very good...what do you think is the problem with the adapter now ?...When i enter a value, and lose focus on that edit text, it simply delets it...:(

EDIT2: The tutorial from this website helped me solve my problem : http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/ without using TextWatcher

Upvotes: 0

Views: 237

Answers (1)

Sam
Sam

Reputation: 86948

when i enter a value in a EditText, then another EditText from below is beeing populated...Why is that happening...?

ListView's recycle each row's layout. Think of it this way: why have 1000 unique layouts (which is very slow) when you can use about 10 rows and simply reuse them after they're scrolled off the screen? This concept is covered in detail in Turbo Charge your UI and other Google I/O presentations, they're well worth watching.

Anyway the solution is easy, save the contents of each EditText in your Adapter and restore their values the same way you restore the TextView Strings.


Addition

how do i do that?:)

Add a new member to your Adapter:

private List<String> editTexts = new ArrayList<String>(); // Please think of a better name :)

Use a TextWatcher to record any changes to each EditText and save these Strings in this List. Then in getView() restore the values with:

holder.quantity.setText(editTexts.get(position));

Upvotes: 1

Related Questions