0

I have a Listview content form TextView and Edittext, and I want to get the value from each Edittext in this Listview.

My problems are: When the Listview is created, the focus is not on the first Edittext in Listview.

The second problem, is when I enter a value in Edittext and move list text down in screen the value in Edittext changed or removed, or the value moved to another Edittext on screen.

You can see image in this link

my code is

        ArrayList<HashMap<String, String>> AllStudent ;
    AllStudent = new ArrayList<HashMap<String, String>>();
    for (int j=0; j<15; j++){
        HashMap<String, String> map = new HashMap<String, String>(); 
        // adding each child node to HashMap key => value
        map.put("id"," " + j);
        map.put("name", "A" + j ); 
        // adding HashList to ArrayList
        AllStudent.add(map);
    }
   listadapter = new SimpleAdapter(
            sc_grade_group.this, AllStudent,
            R.layout.list_item_graed, new String[] { "id","name"  },
            new int[] { R.id.list_itemGra_t1, R.id.list_itemGra_t2 });
    setListAdapter(listadapter); 



    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item_LinearLayout"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:background="@drawable/custom_bg_1"
    android:orientation="horizontal"
    android:padding="2dp"
    android:weightSum="1" >

    <TextView
        android:id="@+id/list_itemGra_t1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <TextView
        android:id="@+id/list_itemGra_t2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:background="@drawable/custom_bg_2"
        android:gravity="left|center_vertical"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textColor="@color/black"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/list_itemGra_t4"
        android:layout_width="180dp"
        android:layout_height="match_parent"
        android:layout_weight="0.28"
        android:background="@drawable/custom_bg_2"
        android:gravity="left|center_vertical"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textColor="@color/gold"
        android:textSize="13sp"
        android:textStyle="bold"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.33"
        android:background="@color/cachecolor2"
        android:gravity="center_vertical|center_horizontal"
        android:weightSum="1" >

        <EditText
            android:id="@+id/list_itemGra_Degree"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="numberDecimal" >

            <requestFocus />
        </EditText>

    </LinearLayout>

    <TextView
        android:id="@+id/list_itemGra_t3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.07"
        android:background="@color/cachecolor2" >

    </LinearLayout>

       <TextView
        android:id="@+id/list_itemGra_t5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

</LinearLayout>
joao2fast4u
  • 6,868
  • 5
  • 28
  • 42
Abdulla
  • 13
  • 1
  • 3

2 Answers2

0

Ok first of all, if you want that entered information should be persistent when scrolled out and scrolled back, you have to make a model that should store what was entered. All information is cleared when you scroll, and scroll back is because ListView re-creates only those view's which are on screen and visible.

Used SimpleAdapter can hook up with ViewBinder interface, though its hard to handle storing in there. My suggestion would be to make a class model and make a custom adapter for this.

Here is a great tutorial on various ListView implementations (http://www.vogella.com/tutorials/AndroidListView/article.html).

Second problem, to prevent the view focusing once you enter the area, you can try setting seting the attributes android:focusable="true" and android:focusableInTouchMode="true" on EditText, though this should be tested, im not sure it will work.

0

1, Try in onCreate:

yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {                                   // run when focus is lost
            String value = v.getText().toString();         // get the value from the EditText
            // Your code to update hashmap
        }
    }
});

2, When you create the ListView, add a OnFocusChangeListener, like shown below

myList.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            myHandler.postAtFrontOfQueue(new Runnable() {
                public void run() {
                    myList.setSelection(0);
                }
            });
        }
    }
});

and if it wont work read Focusable EditText inside ListView

Community
  • 1
  • 1
Matej Špilár
  • 2,617
  • 3
  • 15
  • 28