0

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

Teshte
  • 624
  • 1
  • 7
  • 26

1 Answers1

1

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));
Sam
  • 86,580
  • 20
  • 181
  • 179
  • 1
    @Teshte also, definitely watch the link that Sam posted. Tremendous help. I am going to watch "World of ListView" after I get done with the one about navigation – codeMagic Apr 13 '13 at 16:00
  • I will try your solution one of these days...as this is a side project different from my job..If it works I will award Sam with the correct answer...Thanks :) – Teshte Apr 15 '13 at 08:35
  • I think i tried you solution...see my edited code above...howerver still doesn;t work – Teshte Apr 15 '13 at 19:15
  • 1
    In my Activity I added `listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);` and I could write in the EditTexts. If that doesn't work for you, you may need to use the answers in [Focusable EditText inside ListView](http://stackoverflow.com/q/2679948/1267661), or [Android: EditText in ListView](http://stackoverflow.com/q/2825571/1267661), or one of the others here on Stack Overflow. – Sam Apr 16 '13 at 03:37