1

I have a listview and each row contains a textview and an edittext. I also have a button below the listview where i want to access only the edittext widget of all the rows of the listview. Can anybody help me ?

HexList Layout : This is my main listview which contains custom rows. And below the main list view i have a button "save as". On clicking the button i want to access the edittext of each and every row. I am confused about the class to be used to get the edittext widget, i.e. listview or the adapter for the listview.

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

    <ListView
        android:id="@+id/listViewHex1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_above="@+id/saveButton1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >    
    </ListView>

    <Button
        android:id="@+id/saveButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="@string/save" />    
</RelativeLayout>    

HexRow Layout: This is my custum row for the listview, which contains a textview and an edittext.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hexrowlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/hexEditTextRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textShortMessage" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/hexTextViewRow"
        android:layout_width="match_parent"
        android:layout_height="30dp" />

</LinearLayout>

In the main activity iam trying to access the edittext of each and evry row.

Button saveToFile = (Button) findViewById(R.id.saveButton1);
saveToFile.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // Saving hexcode button names from edittext into vitaminslist

        {
            int size = aa.getCount();
            for(int i = 0; i < size; i++){
                //benefits.getChi
                EditText e = (EditText) aa.getView(i, benefits, null);
            HexCode he = vitaminsList.get(i);
            he.setName(e.getText().toString());
            }

        }

    }
});
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
Ajax -the Max
  • 1,049
  • 9
  • 20

1 Answers1

3

Try Somthing like that you may got answer..

Button saveToFile = (Button) findViewById(R.id.saveButton1);
saveToFile.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
    {
        int size = myListView.getCount();
        for(int i = 0; i < size; i++){

        View view = myListView.getChildAt(i);

        EditText e = (EditText) view.findViewById(R.id.hexEditTextRow);
        HexCode he = vitaminsList.get(i);
        he.setName(e.getText().toString());

     }
    }
  }
});
Chirag Ghori
  • 4,231
  • 2
  • 20
  • 35
  • aa is my custom adapter to the listview, i was trying to get total rows from aa(custom adapter). I will give your code a try, thanks. – Ajax -the Max Jan 20 '14 at 07:06
  • i assume aa is listview...wait i will update my answer – Chirag Ghori Jan 20 '14 at 07:07
  • Yes i assumed the same thing with you, and am trying with the listview – Ajax -the Max Jan 20 '14 at 07:16
  • i think this will call before listview fill in adapter so if you want better result must use this code inside your custom adapte.... – Chirag Ghori Jan 20 '14 at 08:21
  • Not getting good results dude, i am getting null pointer exception at EditText e = ...blah, after exactly five iteration, dunno why. And when i try to enter text the same text appears on another edittext in the listview. – Ajax -the Max Jan 30 '14 at 09:10
  • Sorry... i don't known why you get NPE at `EditText` ....and for next point i can say you can use View Holder... – Chirag Ghori Jan 30 '14 at 09:19
  • Some link for you : [android-listview-with-viewholder-tutorial](http://www.myandroidsolutions.com/2012/07/19/android-listview-with-viewholder-tutorial/) , [smooth-scrolling](http://developer.android.com/training/improving-layouts/smooth-scrolling.html) , [how-to-implement-a-view-holder](http://stackoverflow.com/questions/4145602/how-to-implement-a-view-holder) – Chirag Ghori Jan 30 '14 at 09:21