Wednesday, May 16, 2012

[android-developers] How to manage CheckBox or EditText item in a ListView ?

I want to manage checkbox and edittext in a ListView.

I fill the list calling fill(), user (un)check and modify content of each line, then I want to save modifications calling read(). Here is a sample code.

What is the way to interact with the listview and get the modified content ?

Thank you for your help !

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        
        setContentView(R.layout.main);
    mContext = this;
    managecontent();
        }

    void fill()
   {
   // fill the list
   HashMap<String, String> map;
   
   mylist.removeAll(mylist);
   map = new HashMap<String, String>();
map.put("choice", "choice#1");
map.put("valeur", "123");
mylist.add(map);
   map = new HashMap<String, String>();
map.put("choice", "choice#2");
map.put("valeur", "345");
mylist.add(map);
ListView list = (ListView) findViewById(R.id.choicelist);
SimpleAdapter listAdapter = new SimpleAdapter(mContext, mylist, R.layout.line,
       new String[] {"choice","valeur"},
       new int[] {R.id.choice,R.id.value});
list.setAdapter(listAdapter);
   }
    void read()
    {
// read the list / change content by program
View listItem;
CheckBox c;
EditText e;
String title;
ListView list = (ListView) findViewById(R.id.choicelist);
SimpleAdapter listAdapter = (SimpleAdapter) list.getAdapter();
if (listAdapter == null)
   return;
for (int i = 0; i < listAdapter.getCount(); i++)
{
   listItem = listAdapter.getView((int) i, null, list);
   if (listItem!=null)
    {
   c=(CheckBox)listItem.findViewById(R.id.choice);
   if (c!=null)
    {
    title=c.getText().toString(); // get the original label : choice#1 then choice#2
    if (!c.isChecked())
    {
    c.setText("change !"); // no effect !
    c.setChecked(true); // no effect !
    }
    e=(EditText)listItem.findViewById(R.id.value);
    if (e!=null)
    {
    title=e.getText().toString();// always get the orignal content : 123 then 345 (not the data user set in the EditText item)
    e.setText("X");// no effect !
    }
    }
    }
}
   }

line.xml :

<?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="wrap_content">
    <CheckBox
        android:id="@+id/choice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:checked="false" android:clickable="true"/>
  <EditText
      android:id="@+id/value"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:ems="10" >
      <requestFocus />
  </EditText>
</LinearLayout>


mail.xml :

<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent" android:layout_width="fill_parent">
<RelativeLayout android:layout_height="fill_parent" android:id="@+id/RelativeLayout01" android:padding="5sp" android:layout_width="fill_parent">
     
<ListView
        android:id="@+id/choicelist"
        android:layout_width="fill_parent"
        android:layout_height="200dp" android:choiceMode="multipleChoice" android:clickable="true" android:focusable="true">
    </ListView>     

 </RelativeLayout>
</ScrollView>

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate