Wednesday, May 16, 2012

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

The way you are using your code is not going to work...

When dealing with a listview you pretty much always want to be dealing with the adapter directly...  For example, you should never call the adapter's getView() method, as that creates a new view based on the data in your adapter.

If you want to have UI interaction in your listview you are most likely going to need to write your own adapter subclass so you can customize the getView() method.  In there you can handle code for the checkbox to update the adapter's data when it is clicked, and then call notifyDataSetChanged() on the adapter.

The listview is simply a UI representation of your data.  Your adapter is the data that the listview uses to display the data.  If you just change the listview but not the adapter's data then you will lose the changes when you scroll the listview.

Thanks,
Justin Anderson
MagouyaWare Developer
http://sites.google.com/site/magouyaware


On Wed, May 16, 2012 at 4:26 PM, chronogps <chronogps@free.fr> wrote:
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
  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

--
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