Re: [android-developers] Frustrated with CheckBox/CheckedListView in listview
Listview behaves in a different way . it tries to recycle views if it can . From your code you are not recycling views , you should try to use If(convertview ==null) { create new view } else { map and use it } Will give you some hint 1) put some log statement in get view and check how many times its called and what position from that you can figure out whats happening. Also google for some examples on customlistview which will give you some idea. On Wed, Apr 28, 2010 at 5:37 PM, Jeru Liu jeru@gmail.com wrote: sorry i forgot to mention that even i set the position to checkbox tag, the log always output the position of the last element in the onCheckedChanged On Thu, Apr 29, 2010 at 6:36 AM, Jeru Liu jeru@gmail.com wrote: Thanks buddy, but it still doesn't work in my case, now i will attach more informatin below. My Adapter class public class ProfileAdapter extends SimpleAdapter { Activity mContext; CheckBox cb; ProfileDAO mProfileDao; int mId; int mPosition; public ProfileAdapter(Activity context, ListMapString, String rowList, int resourceId, String[] from, int[] to) { super(context, rowList, resourceId, from, to); mContext = context; mProfileDao = new ProfileDAO(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = this.mContext.getLayoutInflater().inflate(R.layout.list_profile, null); row.setTag(R.id.profile_title, row.findViewById(R.id.profile_title)); row.setTag(R.id.profile_value, row.findViewById(R.id.profile_value)); row.setTag(R.id.cb_switch, row.findViewById(R.id.cb_switch)); row.setTag(R.id.id, row.findViewById(R.id.id)); TextView profileTitle = (TextView) row.getTag(R.id.profile_title); TextView profileValue = (TextView) row.getTag(R.id.profile_value); cb = (CheckBox) row.getTag(R.id.cb_switch); TextView id = (TextView) row.getTag(R.id.id); MapString, String temp = (MapString, String) ((AdapterView?) parent).getItemAtPosition(position); cb.setChecked(isEnable(temp.get(enable))); profileTitle.setText(temp.get(title)); //cb.setFocusable(true); profileValue.setText(temp.get(value)); //String sid = (String)temp.get(id); //id.setText(sid); cb.setTag(position); cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.v(abc, ** position is: + cb.getTag()); //mProfileDao.updateEnable(mId, isChecked); //Toast.makeText(mContext, getToastMessage(isChecked), Toast.LENGTH_SHORT).show(); } }); return (row); } private boolean isEnable(String value) { boolean isEnable = false; if (value.equals(true)) { isEnable = true; } return isEnable; } private String getToastMessage(boolean isEnabled) { if (isEnabled) { return Profile enabled; } else { return Profile disabled; } } } My list_profile.xml: I'd like to pass the id to the listener in the adaper but don't know how to do that, so i put a element in the TableRow and make it invisible by setting the width and height to 0 ?xml version=1.0 encoding=utf-8? TableLayout xmlns:android=http://schemas.android.com/apk/res/android; android:layout_width=fill_parent android:layout_height=wrap_content android:stretchColumns=0 android:padding=5dp TableRow TextView xmlns:android= http://schemas.android.com/apk/res/android; android:id=@+id/profile_title android:layout_width=fill_parent android:layout_height=wrap_content android:textSize=16sp android:textColor=#ff android:gravity=center_vertical/ CheckBox android:id=@+id/cb_switch android:layout_width=wrap_content android:layout_height=wrap_content / /TableRow TableRow TextView android:textSize=12sp android:id=@+id/profile_value android:layout_width=fill_parent android:layout_height=wrap_content /TextView TextView android:textSize=0sp android:id=@+id/id android:layout_width=0sp android:layout_height=0sp/TextView /TableRow /TableLayout The reference class: String[] from = { title, enable, value, id}; // the to array specifies the TextViews from the xml layout // on which we want to display the values defined in the from array int[] to = { R.id.profile_title, R.id.cb_switch, R.id.profile_value, R.id.id }; // get a reference to the ListView listView = (ListView) findViewById(android.R.id.list);
Re: [android-developers] Frustrated with CheckBox/CheckedListView in listview
On Tue, Apr 27, 2010 at 6:58 PM, Jeruliu jeru@gmail.com wrote: but there is way to detect the position of the item clicked??? Going off of what you have already, you could put the incoming position in the checkbox's tag, then retrieve it when it's clicked. - TreKing - Chicago transit tracking app for Android-powered devices http://sites.google.com/site/rezmobileapps/treking -- 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Frustrated with CheckBox/CheckedListView in listview
Thanks buddy, but it still doesn't work in my case, now i will attach more informatin below. My Adapter class public class ProfileAdapter extends SimpleAdapter { Activity mContext; CheckBox cb; ProfileDAO mProfileDao; int mId; int mPosition; public ProfileAdapter(Activity context, ListMapString, String rowList, int resourceId, String[] from, int[] to) { super(context, rowList, resourceId, from, to); mContext = context; mProfileDao = new ProfileDAO(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = this.mContext.getLayoutInflater().inflate(R.layout.list_profile, null); row.setTag(R.id.profile_title, row.findViewById(R.id.profile_title)); row.setTag(R.id.profile_value, row.findViewById(R.id.profile_value)); row.setTag(R.id.cb_switch, row.findViewById(R.id.cb_switch)); row.setTag(R.id.id, row.findViewById(R.id.id)); TextView profileTitle = (TextView) row.getTag(R.id.profile_title); TextView profileValue = (TextView) row.getTag(R.id.profile_value); cb = (CheckBox) row.getTag(R.id.cb_switch); TextView id = (TextView) row.getTag(R.id.id); MapString, String temp = (MapString, String) ((AdapterView?) parent).getItemAtPosition(position); cb.setChecked(isEnable(temp.get(enable))); profileTitle.setText(temp.get(title)); //cb.setFocusable(true); profileValue.setText(temp.get(value)); //String sid = (String)temp.get(id); //id.setText(sid); cb.setTag(position); cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.v(abc, ** position is: + cb.getTag()); //mProfileDao.updateEnable(mId, isChecked); //Toast.makeText(mContext, getToastMessage(isChecked), Toast.LENGTH_SHORT).show(); } }); return (row); } private boolean isEnable(String value) { boolean isEnable = false; if (value.equals(true)) { isEnable = true; } return isEnable; } private String getToastMessage(boolean isEnabled) { if (isEnabled) { return Profile enabled; } else { return Profile disabled; } } } My list_profile.xml: I'd like to pass the id to the listener in the adaper but don't know how to do that, so i put a element in the TableRow and make it invisible by setting the width and height to 0 ?xml version=1.0 encoding=utf-8? TableLayout xmlns:android=http://schemas.android.com/apk/res/android; android:layout_width=fill_parent android:layout_height=wrap_content android:stretchColumns=0 android:padding=5dp TableRow TextView xmlns:android= http://schemas.android.com/apk/res/android; android:id=@+id/profile_title android:layout_width=fill_parent android:layout_height=wrap_content android:textSize=16sp android:textColor=#ff android:gravity=center_vertical/ CheckBox android:id=@+id/cb_switch android:layout_width=wrap_content android:layout_height=wrap_content / /TableRow TableRow TextView android:textSize=12sp android:id=@+id/profile_value android:layout_width=fill_parent android:layout_height=wrap_content /TextView TextView android:textSize=0sp android:id=@+id/id android:layout_width=0sp android:layout_height=0sp/TextView /TableRow /TableLayout The reference class: String[] from = { title, enable, value, id}; // the to array specifies the TextViews from the xml layout // on which we want to display the values defined in the from array int[] to = { R.id.profile_title, R.id.cb_switch, R.id.profile_value, R.id.id }; // get a reference to the ListView listView = (ListView) findViewById(android.R.id.list); adapter = new ProfileAdapter(this, rowList, R.layout.list_profile, from, to); // nAdapter = new NewAdapter(this, pList); listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setItemsCanFocus(false); On Wed, Apr 28, 2010 at 10:28 PM, TreKing treking...@gmail.com wrote: On Tue, Apr 27, 2010 at 6:58 PM, Jeruliu jeru@gmail.com wrote: but there is way to detect the position of the item clicked??? Going off of what you have already, you could put the incoming position in the checkbox's tag, then retrieve it when it's clicked. - TreKing - Chicago transit tracking app for Android-powered devices http://sites.google.com/site/rezmobileapps/treking -- You received this
Re: [android-developers] Frustrated with CheckBox/CheckedListView in listview
sorry i forgot to mention that even i set the position to checkbox tag, the log always output the position of the last element in the onCheckedChanged On Thu, Apr 29, 2010 at 6:36 AM, Jeru Liu jeru@gmail.com wrote: Thanks buddy, but it still doesn't work in my case, now i will attach more informatin below. My Adapter class public class ProfileAdapter extends SimpleAdapter { Activity mContext; CheckBox cb; ProfileDAO mProfileDao; int mId; int mPosition; public ProfileAdapter(Activity context, ListMapString, String rowList, int resourceId, String[] from, int[] to) { super(context, rowList, resourceId, from, to); mContext = context; mProfileDao = new ProfileDAO(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = this.mContext.getLayoutInflater().inflate(R.layout.list_profile, null); row.setTag(R.id.profile_title, row.findViewById(R.id.profile_title)); row.setTag(R.id.profile_value, row.findViewById(R.id.profile_value)); row.setTag(R.id.cb_switch, row.findViewById(R.id.cb_switch)); row.setTag(R.id.id, row.findViewById(R.id.id)); TextView profileTitle = (TextView) row.getTag(R.id.profile_title); TextView profileValue = (TextView) row.getTag(R.id.profile_value); cb = (CheckBox) row.getTag(R.id.cb_switch); TextView id = (TextView) row.getTag(R.id.id); MapString, String temp = (MapString, String) ((AdapterView?) parent).getItemAtPosition(position); cb.setChecked(isEnable(temp.get(enable))); profileTitle.setText(temp.get(title)); //cb.setFocusable(true); profileValue.setText(temp.get(value)); //String sid = (String)temp.get(id); //id.setText(sid); cb.setTag(position); cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Log.v(abc, ** position is: + cb.getTag()); //mProfileDao.updateEnable(mId, isChecked); //Toast.makeText(mContext, getToastMessage(isChecked), Toast.LENGTH_SHORT).show(); } }); return (row); } private boolean isEnable(String value) { boolean isEnable = false; if (value.equals(true)) { isEnable = true; } return isEnable; } private String getToastMessage(boolean isEnabled) { if (isEnabled) { return Profile enabled; } else { return Profile disabled; } } } My list_profile.xml: I'd like to pass the id to the listener in the adaper but don't know how to do that, so i put a element in the TableRow and make it invisible by setting the width and height to 0 ?xml version=1.0 encoding=utf-8? TableLayout xmlns:android=http://schemas.android.com/apk/res/android; android:layout_width=fill_parent android:layout_height=wrap_content android:stretchColumns=0 android:padding=5dp TableRow TextView xmlns:android= http://schemas.android.com/apk/res/android; android:id=@+id/profile_title android:layout_width=fill_parent android:layout_height=wrap_content android:textSize=16sp android:textColor=#ff android:gravity=center_vertical/ CheckBox android:id=@+id/cb_switch android:layout_width=wrap_content android:layout_height=wrap_content / /TableRow TableRow TextView android:textSize=12sp android:id=@+id/profile_value android:layout_width=fill_parent android:layout_height=wrap_content /TextView TextView android:textSize=0sp android:id=@+id/id android:layout_width=0sp android:layout_height=0sp/TextView /TableRow /TableLayout The reference class: String[] from = { title, enable, value, id}; // the to array specifies the TextViews from the xml layout // on which we want to display the values defined in the from array int[] to = { R.id.profile_title, R.id.cb_switch, R.id.profile_value, R.id.id }; // get a reference to the ListView listView = (ListView) findViewById(android.R.id.list); adapter = new ProfileAdapter(this, rowList, R.layout.list_profile, from, to); // nAdapter = new NewAdapter(this, pList); listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setItemsCanFocus(false); On Wed, Apr 28, 2010 at 10:28 PM, TreKing treking...@gmail.com wrote: On Tue, Apr 27, 2010 at 6:58 PM, Jeruliu jeru@gmail.com wrote: but there is way to detect the position of the item clicked??? Going off of what you have already, you could