listview with radio group error - android

everybody i am creating a custom listview with dynamic radiobuttons adding to radiogroup upto that i am getting what i want but when i try to select one radio button in first row then automatically first button in 5th ,9th,13 row is getting selected and when i select any button in second row then same button in 6th,8th,12th row is getting selected what i am doing wrong here and my adapter class
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
String tag = "Events";
private final List<Model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox,checkbox1;
protected RadioGroup mgroup;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(tag," 3");
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
//viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
//viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkbox1);
viewHolder.mgroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
final RadioButton[] mbutton=new RadioButton[5];
for(int l=0;l<5;l++){
mbutton[l]=new RadioButton(context);
mbutton[l].setText("test"+l);
viewHolder.mgroup.addView(mbutton[l]);
}
viewHolder.mgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
for(int i=0; i<mRadioGroup.getChildCount(); i++) {
RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
//int t=table.indexOfChild(table_row);
//System.out.println(t);
int t=mRadioGroup.getId();
System.out.println(t);
if(btn.getId() == checkedId) {
String text = btn.getText().toString();
// do something with text
Log.d(text," event1");
return;
}
}
}
});
view.setTag(viewHolder);
Log.d(tag,"me");
} else {
view = convertView;
Log.d(tag,"meeee");
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
Log.d(tag," event7");
return view;
}
}
i am posting my working code so that it will be helpfull to others have a look at it
public class InteractiveArrayAdapter extends ArrayAdapter<Model> implements OnClickListener {
String tag = "Events";
private final List<Model> list;
private final Activity context;
int li,jh;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox,checkbox1;
protected RadioGroup mgroup;
protected RadioButton mbutton;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
//Log.d(tag," 3");
View view =null;
if (convertView == null) {
//System.out.println("ok");
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.mgroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
final RadioButton[] mbutton=new RadioButton[5];
for(int l=0;l<5;l++){
mbutton[l]=new RadioButton(context);
mbutton[l].setText("test"+l+position);
mbutton[l].setId(l+1);
mbutton[l].setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RadioButton byt=(RadioButton) v;
byt.isChecked();
byt.getId();
Model element = (Model) viewHolder.mgroup.getTag();
element.setBte(byt.getId());
element.gte((String) viewHolder.text.getText());
System.out.println("givemeresult");
}
});
viewHolder.mgroup.addView(mbutton[l]);
}
view.setTag(viewHolder);
viewHolder.mgroup.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).mgroup.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
view.getTag()).mgroup.getTag();
holder.text.setText(list.get(position).getName());
if (list.get(position).getName()==list.get(position).done()){
holder.mgroup.check(list.get(position).isSelected());
}
else {
holder.mgroup.clearCheck();
}
return view;
}

That's because views are reused. You have to set holder.mgroup in the else of if(convertView == null)
Your code a bit changed by me:
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
String tag = "Events";
private final List<Model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context.getApplicationContext();
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox,checkbox1;
protected RadioGroup mgroup;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(tag," 3");
View view = convertView;
ViewHolder viewHolder = null;
final RadioButton[] mbutton = null;
if (view == null) {
LayoutInflater inflator = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.rowbuttonlayout, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
//viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
//viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkbox1);
viewHolder.mgroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
// I'm not sure about this..
// Begin
mbutton=new RadioButton[5];
for(int l=0;l<5;l++){
mbutton[l]=new RadioButton(context);
mbutton[l].setText("test"+l);
viewHolder.mgroup.addView(mbutton[l]);
}
viewHolder.mgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
for(int i=0; i<mRadioGroup.getChildCount(); i++) {
RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
//int t=table.indexOfChild(table_row);
//System.out.println(t);
int t=mRadioGroup.getId();
System.out.println(t);
if(btn.getId() == checkedId) {
String text = btn.getText().toString();
// do something with text
Log.d(text," event1");
return;
}
}
}
});
// End
view.setTag(viewHolder);
Log.d(tag,"me");
} else {
viewHolder = (ViewHolder) convertView.getTag();
// Begin
mbutton = viewHolder.mgroup;
// End
Log.d(tag,"meeee");
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
Log.d(tag," event7");
return view;
}
}
My implementation of an imageView (stripped):
public class DealAdapter extends ArrayAdapter<DealObject> {
private Context mContext;
private Activity mActivity;
private ArrayList<DealObject> mItems;
private int mXmlId;
public DealAdapter(Context context, int textViewResourceId, ArrayList<DealObject> items, Activity activity) {
super(context, textViewResourceId, items);
this.mContext = context.getApplicationContext();
this.mActivity = activity;
this.mItems = items;
this.mXmlId = textViewResourceId;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
//View v = null;
View v = convertView;
ViewHolder holder = null;
RemoteImageLoader imageLoader = new RemoteImageLoader(mContext, true);
RemoteImageView dealImage = null;
DealObject mo = mItems.get(position);
try {
if (v == null) {
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(mXmlId, null);
holder = new ViewHolder();
dealImage = (RemoteImageView) v.findViewById(R.id.deal_image);
holder.dealImage = dealImage;
v.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the ImageView.
holder = (ViewHolder) convertView.getTag();
holder.dealImage.setBackgroundColor(Color.WHITE);
dealImage = holder.dealImage;
}
if(mo.getImage() != null){
// calling reset is important to prevent old images from displaying in a recycled view.
dealImage.reset();
holder.dealImage.setImageUrl(imageUrl);
try {
holder.dealImage.loadImage();
}
catch (Exception ex) {
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return v;
}
private static final class ViewHolder {
private RemoteImageView dealImage;
}
}

Related

setVisibility(View.INVISIBLE) not working on first row of ListView

setVisibility(View.INVISIBLE) is not working on first row of ListView when Listview is created. But when I scroll down and scroll up, it's working fine
MY CODE:
public class AudioListAdapter extends ArrayAdapter<GalleryItem>{
private Context context;
private int layoutResourceId;
private ArrayList<GalleryItem> data = new ArrayList<GalleryItem>();
private ArrayList<Boolean> isPlaying = new ArrayList<Boolean>();
boolean isPlaying1;
final MediaPlayer mediaPlayer = new MediaPlayer();
public AudioListAdapter(Context context, int resource, ArrayList<GalleryItem> data) {
super(context, resource, data);
this.layoutResourceId = resource;
this.context = context;
this.data = data;
Iterator<GalleryItem> iterator = data.iterator();
while (iterator.hasNext()) {
GalleryItem galleryItem = (GalleryItem) iterator.next();
isPlaying.add(false);
}
//notifyDataSetChanged();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
final ViewHolder holder;
//View row = convertView;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.playImage = (ImageView) convertView.findViewById(R.id.audio_play_image);
holder.stopImage = (ImageView) convertView.findViewById(R.id.audio_stop_image);
holder.title = (TextView) convertView.findViewById(R.id.audio_title);
holder.duration = (TextView) convertView.findViewById(R.id.audio_duration);
isPlaying.set(position, false);
//convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
LogUtils.d("TEST", holder.title.getText().toString());
}
final GalleryItem item = data.get(position);
System.out.println(item.getDateModified());
holder.title.setText(item.getTitle());
holder.title.setSelected(true);
holder.duration.setText("00:00");
if (isPlaying.get(position)) {
holder.playImage.setVisibility(View.INVISIBLE);
holder.stopImage.setVisibility(View.VISIBLE);
}else{
holder.playImage.setVisibility(View.VISIBLE);
holder.stopImage.setVisibility(View.INVISIBLE);
}
OnClickListener audioControlsListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (isPlaying.get(position)) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
isPlaying.set(position, false);
holder.playImage.setVisibility(View.VISIBLE);
holder.stopImage.setVisibility(View.INVISIBLE);
}else{
holder.playImage.setVisibility(View.INVISIBLE);
holder.stopImage.setVisibility(View.VISIBLE);
for(int i=0 ; i< isPlaying.size();i++){
isPlaying.set(i, false);
}
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(item.getPath());
mediaPlayer.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
isPlaying.set(position, true);
notifyDataSetChanged();
}
}
};
holder.playImage.setOnClickListener(audioControlsListener);
holder.stopImage.setOnClickListener(audioControlsListener);
convertView.setTag(holder);
return convertView;
}
static class ViewHolder{
TextView title;
TextView duration;
ImageView playImage;
ImageView stopImage;
}
}
Still setVisibility(View.INVISIBLE) not working. But I used only one ImageView and replaced the image when required.This worked for me.

How to set checked value on checkbox when click listview data

My listview data have imageview , textview and checkbox. When I click listview data , everything is work .But checkbox value is not change . I want to implement as native android multi selected item. I want to apply onClick event not only checkbox but also imageview and textview. i.e it is effected that user click everywhere on one row
This is my code.
public class ProductListChkAdapter extends BaseAdapter {
private Context mContext;
private static CashSaleProductInfo _CashSaleProductInfo = null;
private List<CashSaleProductInfo> _listChkCashSaleProductInfo;
private int chkPrdcodeID;
private LayoutInflater inflater;
public static List<CashSaleListInfo> _listCashSaleListInfo;
private CashSaleProductLogic _sellProductLogic;
boolean[] checkBoxState;
private LayoutInflater layoutInflater;
public ProductListChkAdapter(Context paramContext,
List<CashSaleProductInfo> paramList1, int textViewResourceId) {
this.mContext = paramContext;
this._listChkCashSaleProductInfo = paramList1;
this.chkPrdcodeID = textViewResourceId;
checkBoxState = new boolean[_listChkCashSaleProductInfo.size()];
}
#Override
public int getCount() {
return (_listChkCashSaleProductInfo == null) ? 0
: _listChkCashSaleProductInfo.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int position, View convertView,
ViewGroup paramViewGroup) {
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.productinfochklist, null);
TextView lblProductCode = (TextView) convertView
.findViewById(R.id.lblProductCode);
CheckBox chkPrdCode = (CheckBox) convertView
.findViewById(R.id.chkProductCode);
ImageView imgPrdCode = (ImageView) convertView
.findViewById(R.id.main_list_item_pcode);
RelativeLayout _relPrdlist = (RelativeLayout) convertView
.findViewById(R.id.relPrdlist);
imgPrdCode.setImageResource(R.drawable.c_f);
viewHolder = new ViewHolder();
viewHolder.lblProductCode = lblProductCode;
viewHolder.chkPrdCode = chkPrdCode;
viewHolder._relPrdlist = _relPrdlist;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.lblProductCode.setText(_listChkCashSaleProductInfo
.get(position).getDESC_ENG().toString());
viewHolder.chkPrdCode.setChecked(checkBoxState[position]);
viewHolder._relPrdlist.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MainActivity._CashSaleProductInfo = new CashSaleProductInfo();
// CheckBox cb = (CheckBox) v;
CheckBox cb = (CheckBox) v;
int p = position;
boolean b = checkBoxState[p];
// if (((CheckBox) v).isChecked()) {
if (!checkBoxState[position]) {
// if (cb.isChecked()) {
checkBoxState[position] = true;
// States _state = (States) cb.getTag();
MainActivity._CashSaleProductInfo
.setPRODUCT_CODE(_listChkCashSaleProductInfo.get(
position).getPRODUCT_CODE());
MainActivity._CashSaleProductInfo
.setDESC_ENG(_listChkCashSaleProductInfo.get(
position).getDESC_ENG());
MainActivity._listchkCashSaleProductInfo
.add(MainActivity._CashSaleProductInfo);
} else {
MainActivity._CashSaleProductInfo
.setPRODUCT_CODE(_listChkCashSaleProductInfo.get(
position).getPRODUCT_CODE());
MainActivity._CashSaleProductInfo
.setDESC_ENG(_listChkCashSaleProductInfo.get(
position).getDESC_ENG());
Iterator<CashSaleProductInfo> iterator = MainActivity._listchkCashSaleProductInfo
.iterator();
while (iterator.hasNext()) {
CashSaleProductInfo _CashSaleProductInfo = iterator
.next();
if (_CashSaleProductInfo.getPRODUCT_CODE().equals(
MainActivity._CashSaleProductInfo
.getPRODUCT_CODE())) {
iterator.remove();
} else {}
}
checkBoxState[position] = false;
Log.i("CC", " After "
+ MainActivity._listchkCashSaleProductInfo.size());
}
}
});
return convertView;
}
public static class ViewHolder {
TextView lblProductCode;
CheckBox chkPrdCode;
ImageView imgPrdCode;
RelativeLayout _relPrdlist;
}
}
Try add this code inside onClick to your checkbox:
cb.setChecked(true);
or
cb.setChecked(false);
try to add to Check Box setOnCheckedChangeListener
where you can store state of your CheckBox
also here Link Try this

How to set tag the ID to a checkbox android

I have created the custom adapter as follows.
public class sendivitesadapter extends ArrayAdapter<Item>{
private Context context;
private ArrayList<Item> items;
private qrusers qrusers;
private LayoutInflater vi;
private String[] array;
qrusers qrus;
public sendivitesadapter(Context context,ArrayList<Item> items) {
super(context, 0,items);
this.context= context;
this.qrusers =(qrusers) context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return super.getCount();
}
#Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return super.getItem(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
sendItem ei = (sendItem)i;
v = vi.inflate(R.layout.checkboxlist, null);
final TextView title = (TextView)v.findViewById(R.id.contactname);
final TextView subtitle = (TextView)v.findViewById(R.id.companyname);
final CheckBox checkBox=(CheckBox)v.findViewById(R.id.checboxlist);
//checkBox.setTag(qrus.uid);
// Log.e("IDDDDDDD", text);
//checkBox.setTag("12");
checkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s=(String) v.getTag();
Log.e("IDDDDDDDDDDDDDDDDDDDDDD", s);
}
});
if (title != null)
title.setText(ei.contactname);
if(subtitle != null)
subtitle.setText(ei.companyname);
}
}
return v;
}
}
And my activity where I have assigned this adapter to a listview is as follows.
#Override
protected void onPostExecute(String result) {
JSONArray jarray;
try {
jarray= new JSONArray(result);
name= new String[jarray.length()];
company=new String[jarray.length()];
uid=new String[jarray.length()];
for (int i=0;i<jarray.length();i++){
JSONObject jobj = jarray.getJSONObject(i);
name[i]= jobj.getString("Name");
company[i]=jobj.getString("Company");
uid[i]=jobj.getString("UserID");
System.out.println(uid[i]);
items.add(new sendItem(name[i], company[i], qrcheckBox));
sendadapter = new sendivitesadapter(qrusers.this,items);
listView.setAdapter(sendadapter);
Now as you see I get the name ,company and uid from webservice. I get the ID of user in string array uid. Now I want to set the uid to the checkbox which i have created in custom adapter. and on click of that it should hold the uid. How can i achieve this?
try below Custom Adapter
private class Demo_List_Adapter extends ArrayAdapter<Data_Model> {
private ArrayList<Data_Model> inviteList;
public Demo_List_Adapter(Context context, int textViewResourceId,
ArrayList<Data_Model> inviteList) {
super(context, textViewResourceId, inviteList);
this.inviteList = new ArrayList<Data_Model>();
this.inviteList.addAll(inviteList);
}
private class ViewHolder {
TextView textView_FromPersonName;
TextView textView_Subject;
TextView textView_Body;
TextView textView_Date;
ImageView imageView_Star;
CheckBox checkBox_MsgSelection;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_row_message_inbox, null);
holder = new ViewHolder();
holder.textView_FromPersonName = (TextView) convertView
.findViewById(R.id.textView_FromPersonName);
holder.textView_Subject = (TextView) convertView
.findViewById(R.id.textView_Subject);
holder.textView_Body = (TextView) convertView
.findViewById(R.id.textView_Body);
holder.textView_Date = (TextView) convertView
.findViewById(R.id.textView_Date);
holder.imageView_Star = (ImageView) convertView
.findViewById(R.id.imageView_Star);
holder.checkBox_MsgSelection = (CheckBox) convertView
.findViewById(R.id.checkBox_MsgSelection);
convertView.setTag(holder);
holder.checkBox_MsgSelection
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Data_Model inviteModel = (Data_Model) cb
.getTag();
inviteModel.setChecked(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Data_Model inviteModel = inviteList.get(position);
if (inviteModel.getJa_MS_IsRead().equals("0"))
convertView.setBackgroundColor(getActivity().getResources()
.getColor(R.color.gray_background));
else
convertView.setBackgroundColor(getActivity().getResources()
.getColor(R.color.white));
holder.textView_FromPersonName.setText(inviteModel
.getJa_ME_FromPesrsonName());
holder.textView_Subject.setText(inviteModel.getJa_ME_Subject());
holder.textView_Body
.setText(stripHtml(inviteModel.getJa_ME_Body()));
String s[] = inviteModel.getJa_ME_CreatedDate().split("\\s+");
holder.textView_Date.setText(s[0]);
if (inviteModel.getJa_MS_IsFlagged().trim().equalsIgnoreCase("1")) {
holder.imageView_Star.setImageResource(R.drawable.star);
} else {
holder.imageView_Star.setImageResource(R.drawable.star_unsele);
}
holder.checkBox_MsgSelection.setTag(inviteModel);
return convertView;
}
}
holder.checkBox_MsgSelection.setTag(inviteModel); for to set tag

The checked Togglebuttons in my ListView aren't checked anymore when i scroll

I dynamicly add Togglebuttons from an Array into my RadioGroup. The adding works fine and every different List items works fine but when I scroll the checked status is changed back.
How can I fix this?
Here is my ListAdapter class:
public class BezoekverslagDetailsListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;;
public BezoekverslagDetailsListAdapter(Activity context, List<Vraag> vragen){
super(context, R.layout.vraag_item, vragen);
this.context = context;
this.vragen = vragen;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = null;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.vraag_item, null, false);
holder = new ViewHolder();
holder.vraag = (TextView)convertView.findViewById(R.id.tv_vraag);
holder.group = (RadioGroup)convertView.findViewById(R.id.toggleGroup);
holder.position = position;
holder.toggle = new ToggleButton(context);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
final Vraag vraag = vragen.get(position);
holder.vraag.setText(vraag.getVraag());
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
for (int j = 0; j < group.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) group.getChildAt(j);
view.setChecked(view.getId() == checkedId);
vraag.getAntwoorden().get(view.getId()).setSelected(view.getId() == checkedId);
}
}
});
for(final Antwoord a : vraag.getAntwoorden()){
holder.toggle = new ToggleButton(context);
holder.toggle.setText(a.getAntwoord());
holder.toggle.setTextOn(a.getAntwoord());
holder.toggle.setTextOff(a.getAntwoord());
holder.toggle.setId(a.getId());
holder.toggle.setChecked(a.getSelected());
if(a.getVisible() == false){
holder.toggle.setVisibility(ToggleButton.INVISIBLE);
}
holder.toggle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// your click actions go here
((RadioGroup)view.getParent()).check(view.getId());
}
});
holder.group.addView(holder.toggle);
}
return convertView;
}
static class ViewHolder {
TextView vraag;
RadioGroup group;
ToggleButton toggle;
int position;
}
}
Instead of this :
final Vraag vraag = vragen.get(position);
holder.vraag.setText(vraag.getVraag());
try this :
final Vraag vraag = vragen.get(holder.position);
holder.vraag.setText(vraag.getVraag());
While scrolling list View it has a weird behaviour of calculating the position based on the Items which are currently there in the View . I had faced this weird View . I dynamically set the tag of that particular view to the actual position of the view and then did a View.get(tag).
I found an other way, now it holds the checked Togglebuttons but the problem now is that the buttons get changed from position.
Here is the code:
public class BezoekverslagDetailsListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;
public BezoekverslagDetailsListAdapter(Activity context, List<Vraag> vragen){
super(context, R.layout.vraag_item, vragen);
this.context = context;
this.vragen = vragen;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
//convertView = null;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vraag_item, null, false);
holder = new ViewHolder();
holder.vraag = (TextView)view.findViewById(R.id.tv_vraag);
holder.group = (RadioGroup)view.findViewById(R.id.toggleGroup);
holder.position = position;
holder.toggle = new ToggleButton(context);
for(final Antwoord a : vragen.get(holder.position).getAntwoorden()){
holder.toggle = new ToggleButton(context);
holder.toggle.setText(a.getAntwoord());
holder.toggle.setTextOn(a.getAntwoord());
holder.toggle.setTextOff(a.getAntwoord());
holder.toggle.setId(a.getId());
holder.toggle.setChecked(a.getSelected());
if(a.getVisible() == false){
holder.toggle.setVisibility(ToggleButton.INVISIBLE);
}
holder.toggle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// your click actions go here
((RadioGroup)view.getParent()).check(view.getId());
a.setSelected(holder.toggle.isChecked());
}
});
holder.group.addView(holder.toggle);
}
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
for (int j = 0; j < group.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) group.getChildAt(j);
view.setChecked(view.getId() == checkedId);
}
}
});
view.setTag(holder);
}
else{
view = convertView;
}
ViewHolder viewholder = (ViewHolder)view.getTag();
viewholder.vraag.setText(vragen.get(holder.position).getVraag());
return view;
}
static class ViewHolder {
TextView vraag;
RadioGroup group;
ToggleButton toggle;
int position;
}
}
I finally fixed it all!
This is my final code:
public class BezoekverslagDetailsListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;
int i;
public BezoekverslagDetailsListAdapter(Activity context, List<Vraag> vragen){
super(context, R.layout.vraag_item, vragen);
this.context = context;
this.vragen = vragen;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
convertView = null;
final Vraag vraag = vragen.get(position);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.vraag_item, null, false);
holder = new ViewHolder();
holder.vraag = (TextView)view.findViewById(R.id.tv_vraag);
holder.group = (RadioGroup)view.findViewById(R.id.toggleGroup);
holder.position = position;
holder.toggle = new ToggleButton(context);
for(final Antwoord a : vraag.getAntwoorden()){
holder.toggle = new ToggleButton(context);
holder.toggle.setText(a.getAntwoord());
holder.toggle.setTextOn(a.getAntwoord());
holder.toggle.setTextOff(a.getAntwoord());
holder.toggle.setId(i);
holder.toggle.setChecked(a.getSelected());
if(a.getVisible() == false){
holder.toggle.setVisibility(ToggleButton.INVISIBLE);
}
holder.toggle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// your click actions go here
((RadioGroup)view.getParent()).check(view.getId());
if(a.getSelected()==true){
a.setSelected(false);
}
else if(a.getSelected()==false){
for(Antwoord b: vraag.getAntwoorden()){
if(b.getSelected()==true){
b.setSelected(false);
}
}
a.setSelected(true);
vraag.setAntwoord(a.getAntwoord());
}
}
});
i++;
holder.group.addView(holder.toggle);
}
view.setTag(holder);
}
else{
view = convertView;
holder = (ViewHolder) view.getTag();
}
ViewHolder viewholder = (ViewHolder) view.getTag();
viewholder.vraag.setText(vraag.getVraag());
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
for (int j = 0; j < group.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) group.getChildAt(j);
view.setChecked(view.getId() == checkedId);
//vragen.get(holder.position).getAntwoorden().get(view.getId()).setSelected(view.getId() == checkedId);
}
}
});
view.setTag(viewholder);
return view;
}
static class ViewHolder {
TextView vraag;
RadioGroup group;
ToggleButton toggle;
int position;
}
}

Issue when i scroll in my listview it adds addtional ToggleButtons that i add with an Array

I have the Vraag Class that i use to make the different items in the List.
That class has an Array of the Antwoord class. For each Antwoord i want a togglebutton that i added with the for loop. The problem now is that when i scroll in the list it adds additional togglebuttons to the list items. Here is my adapter code:
public class VraagListAdapter extends ArrayAdapter<Vraag>{
public Context context;
public List<Vraag> vragen;
ViewHolder holder;
int id = 0;
public VraagListAdapter(Activity context, List<Vraag> vragen){
super(context, R.layout.vraag_item, vragen);
this.context = context;
this.vragen = vragen;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.vraag_item, null, false);
holder = new ViewHolder();
holder.vraag = (TextView)convertView.findViewById(R.id.tv_vraag);
holder.group = (RadioGroup)convertView.findViewById(R.id.toggleGroup);
holder.position = position;
holder.toggle = new ToggleButton(context);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.vraag.setText(vragen.get(position).getVraag());
final int positievraag = position;
holder.group.setOnCheckedChangeListener(ToggleListener);
for(Antwoord a : vragen.get(position).getAntwoorden()){
holder.toggle = new ToggleButton(context);
holder.toggle.setText(a.getAntwoord());
holder.toggle.setTextOn(a.getAntwoord());
holder.toggle.setTextOff(a.getAntwoord());
holder.toggle.setId(id);
if(a.getVisible() == false){
holder.toggle.setVisibility(ToggleButton.INVISIBLE);
}
holder.toggle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// your click actions go here
((RadioGroup)view.getParent()).check(view.getId());
vragen.get(positievraag).setAntwoord((String)holder.toggle.getText());
}
});
id++;
holder.group.addView(holder.toggle);
}
return convertView;
}
static class ViewHolder {
TextView vraag;
RadioGroup group;
ToggleButton toggle;
int position;
}
static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
for (int j = 0; j < radioGroup.getChildCount(); j++) {
final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
view.setChecked(view.getId() == i);
}
}
};
}
I solved the problem by inserting convertView = null; at the beginning of the getView()
The only problem i still got that ToggleButtons that where checked aren't checked anymore when I scroll down and up again! And how do i get all the checked Togglebuttons in my Activity?

Categories

Resources