i have a listivew with custom arrayadapter and custom layout, it have a Checkbox.
I want to make this listview only one checkbox enabled but i dont know how can i do it.
This is my adapter, but the check and uncheck dont works fine. Any exmple please.
public class gremioAdapter extends ArrayAdapter<Gremio> {
Context context;
int layoutResourceId;
ArrayList<Gremio> data = null;
protected String comentarioAlEdiText;
public gremioAdapter(Context context, int layoutResourceId, ArrayList<Gremio> data)
{
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public void updateDataSet(Gremio objetoGremioAnadir) {
this.data.add(objetoGremioAnadir);
notifyDataSetChanged();
}
public void updateDataSet(Gremio objetoGremioAnadir, int posicion) {
this.data.add(posicion, objetoGremioAnadir);
notifyDataSetChanged();
}
public void updateDataSet(String comentario, int posicion) {
this.data.get(posicion).comentario = comentario;
notifyDataSetChanged();
}
public void updateDataSet() {
notifyDataSetChanged();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
GremioHolder holder = null;
final GremioHolder h2 = holder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new GremioHolder();
holder.tvGremio = (TextView) row.findViewById(R.id.tvGremio);
holder.cbActivo = (CheckBox) row.findViewById(R.id.cbGremioActivo);
holder.tvComentario = (TextView) row.findViewById(R.id.tvComentario);
row.setTag(holder);
}
else {
holder = (GremioHolder) row.getTag();
}
Gremio gremioFinal = data.get(position);
holder.tvGremio.setText(gremioFinal.literal);
holder.tvComentario.setText(gremioFinal.comentario);
if (data.get(position).getActivo().equals("1")) {
holder.cbActivo.setChecked(true);
}
else {
holder.cbActivo.setChecked(false);
}
holder.cbActivo.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if (data.get(position).activo.equals("0"))
h2.cbActivo.setChecked(true);
if (data.get(position).activo.equals("1"))
h2.cbActivo.setChecked(false);
}
});
holder.cbActivo.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
for (int i = 0; i < data.size(); i++) {
data.get(i).activo = "0";
}
data.get(position).activo = "1";
}
if (isChecked) {
for (int i = 0; i < data.size(); i++) {
data.get(i).activo = "0";
}
}
notifyDataSetChanged();
}
});
return row;
}
public class GremioHolder {
TextView tvGremio;
CheckBox cbActivo;
TextView tvComentario;
}
}
}
This should take care of your requirement. A very good tutorial on how you can achieve this.
http://tokudu.com/2010/android-checkable-linear-layout/
So the basic funda is that,
Extend a Layout (Linear/Relative) and implement the checkable interface.
After inflating the layout, find out which child was selected and check the corresponding checkbox. Uncheck any other previously checked item.
Make sure that your ListView's XML has the following marked [android:choiceMode="singleChoice"]
hop it is useful to you...
just change in your getview() method .
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final Holder holder;
if (vi == null) {
vi = inflater.inflate(R.layout.raw_customelabel_list, null);
holder = new Holder();
holder.txtname = (TextView) vi
.findViewById(R.id.raw_customlabel_txt_name);
holder.checkbox = (CheckBox) vi
.findViewById(R.id.raw_customlabel_checkbox);
vi.setTag(holder);
} else {
holder = (Holder) vi.getTag();
}
vi.setTag(holder);
holder.txtname.setText(strarry[position]);
holder.checkbox.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < strarry.length; i++) {
if (v.getId() == i) {
checkarry[i] = true;
Log.v("check", ""+position);
} else {
checkarry[i] = false;
}
}
notifyDataSetChanged();
}
});
if (checkarry[position]) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
return vi;
}
}
Related
Good day,
I'm doing a listview, baseadapter with checkbox that is able to delete multiple selected rows.
here is my onCreate :
ArrayList<Memos> list;
list = new ArrayList<Memos>();
list.add(new Memos(1, "s", "s"));
list.add(new Memos(2, "x", "aaa"));
list.add(new Memos(3, "v", "aesf"));
final ListView lv = (ListView) findViewById(R.id.myList);
lv.setAdapter(new MemoListAdapter(list, this));
deletebutton
#Override
public void onClick(View v) {
MemoListAdapter myAdapter = (MemoListAdapter)lv.getAdapter();
myAdapter.remove();
}
then here is my complete baseadapter :
public class MemoListAdapter extends BaseAdapter {
private List<Memos> listComment;
private Context context;
private LayoutInflater inflater = null;
private ArrayList<Memos> deleteMemos;
public MemoListAdapter(List<Memos> listComment, Context context) {
super();
this.listComment = listComment;
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
deleteMemos = new ArrayList<Memos>();
}
#Override
public int getCount() {
return listComment.size();
}
#Override
public Memos getItem(int position) {
return listComment.get(position);
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
public class ViewHolder
{
TextView body;
TextView date;
CheckBox checkBox;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_view, null);
holder = new ViewHolder();
holder.body = (TextView) convertView.findViewById(R.id.big_text);
holder.date = (TextView) convertView.findViewById(R.id.small_text);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
holder.body.setText(listComment.get(position).getMessageBody());
holder.date.setText(listComment.get(position).getMessageDate());
holder.checkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is chkIos checked?
if(listComment.get(position).isCheckbox()) {
holder.checkBox.setChecked(false);
deleteMemos.remove(listComment.get(position));
}
else {
// Do invisible or gone stuff here
holder.checkBox.setChecked(true);
deleteMemos.add(listComment.get(position));
}
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public void remove() {
for(Memos memo : deleteMemos) {
listComment.remove(memo);
}
this.notifyDataSetChanged();
}
}
after selecting rows to delete (4 rows)
the list adjusted but the contents and the check value of the checkbox is still the same.
I am wondering what part of the notifyDataSetChanged is wrong.
Thanks in advance!
Your views are reused that's why it happen . Try adding below codes into your getView()
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_view, null);
holder = new ViewHolder();
holder.body = (TextView) convertView.findViewById(R.id.big_text);
holder.date = (TextView) convertView.findViewById(R.id.small_text);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(listComment.get(position).isCheckbox()) {
holder.checkBox.setChecked(true);
}else {
holder.checkBox.setChecked(false);
}
holder.body.setText(listComment.get(position).getMessageBody());
holder.date.setText(listComment.get(position).getMessageDate());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
listComment.get(position).setIsCheckbox(isChecked);
if (isChecked){
deleteMemos.add(listComment.get(position));
}else{
deleteMemos.remove(listComment.get(position));
}
}
});
return convertView;
}
listview row having multiple checkboxex, when i checked the checkboxex then if i scroll the listview, its automatically unchecked the checkboxes. how can fix this issue. here is my getView method
public View getView(final int position, View convertView, ViewGroup parent) {
final QuestionRadioHolder questionRadioHolder;
final QuestionCheckboxHolder questionCheckboxHolder;
final Model rowItem = getItem(position);
final int type = getItemViewType(position);
if (type == TYPE1) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.radio_layout, parent, false);
questionRadioHolder = new QuestionRadioHolder();
questionRadioHolder.txtQuestion = (TextView) convertView.findViewById(R.id.txtQue);
questionRadioHolder.rg = (RadioGroup) convertView.findViewById(R.id.rg);
Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf");
questionRadioHolder.txtQuestion.setTypeface(face);
convertView.setTag(questionRadioHolder);
} else {
questionRadioHolder = (QuestionRadioHolder) convertView.getTag();
}
List<String> arrayList = rowItem.getArrayList();
questionRadioHolder.txtQuestion.setText(rowItem.getQuestionText());
questionRadioHolder.rg.removeAllViews();
questionRadioHolder.rg.clearCheck();
for (int i = 0; i < arrayList.size(); i++) {
RadioButton radioButton = new RadioButton(getContext());
radioButton.setId(i);
radioButton.setTextSize(12);
radioButton.setText(arrayList.get(i));
radioButton.setTypeface(face1);
questionRadioHolder.rg.addView(radioButton);
}
// questionRadioHolder.rg.removeAllViews();
questionRadioHolder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup rg, int checkedId) {
for (int i = 0; i < rg.getChildCount(); i++) {
RadioButton btn = (RadioButton) rg.getChildAt(i);
ArrayList<String> list = new ArrayList<String>();
if (btn.getId() == checkedId) {
String text = btn.getText().toString();
int id = mList.get(position).getQuid();
System.out.println(position + " position 2");
map.put(id, text);
list.add(0, text);
aMap1.put(id, list);
Log.d("radio", text);
Log.d("Queid:", "" + id);
return;
}
}
}
});
} else if (type == TYPE2) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.checkbox_layout, parent, false);
questionCheckboxHolder = new QuestionCheckboxHolder();
questionCheckboxHolder.txtQuestionChk = (TextView) convertView.findViewById(R.id.chkQues);
questionCheckboxHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.chkrl2);
Typeface face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-SemiBold.ttf");
face1 = Typeface.createFromAsset(getContext().getAssets(), "fonts/Raleway-Regular.ttf");
questionCheckboxHolder.txtQuestionChk.setTypeface(face);
convertView.setTag(questionCheckboxHolder);
} else {
questionCheckboxHolder = (QuestionCheckboxHolder) convertView.getTag();
}
List<String> arrayList = rowItem.getArrayList();
questionCheckboxHolder.txtQuestionChk.setText(rowItem.getQuestionText());
questionCheckboxHolder.relativeLayout.removeAllViews();
for (int i = 0; i < arrayList.size(); i++) {
checkBox = new CheckBox(getContext());
checkBox.setId(i);
checkBox.setMinHeight(30);
checkBox.setMinimumWidth(30);
checkBox.setText(arrayList.get(i));
checkBox.setTypeface(face1);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int position = (Integer) buttonView.getTag();
ArrayList<String> arraylist2 = new ArrayList<String>();
ArrayList<String> list = new ArrayList<String>();
int id = mList.get(position).getQuid();
arraylist2 = aMap1.get(id);
System.out.println(arraylist2);
if (arraylist2 == null) {
arraylist2 = list;
}
if (buttonView.isChecked()) {
arraylist2.add(buttonView.getText().toString());
aMap1.put(id, arraylist2);
} else {
arraylist2.remove(buttonView.getText().toString());
aMap1.put(id, arraylist2);
}
System.out.println(aMap1.get(id));
}
});
questionCheckboxHolder.relativeLayout.addView(checkBox);
}
}
return convertView;
}
If i scroll the listview, its automatically unchecked the checkbox, how can checked the checkbox.
Try this way this worked for me
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelCls> listData;
public CustomAdapter(Context mainActivity, List<ModelCls> listData) {
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getLISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelCls item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
and if you want to recyclerview try this
http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html
Save your check list in ArrayList<Boolean>, and load it in getView():
ArrayList<Boolean> listCheck;
public YourAdapter(Context context, int resource, ArrayList<T> list) {
super(context, resource, list);
listCheck = new ArrayList<Boolean>;
for (int i = 0; i < list.size(); i++) {
listCheck.add(false); // init listCheck
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
yourCheckBox.setChecked(listCheck.get(position)); // set checkbox
yourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listCheck.set(position, isChecked); // update listChecked
}
}
}
If you checked on checkbox, you can save it into a HashMap(key:position, value:true/false), and in method getView() you can distinguish which is checked or unchecked, then make the checkbox show its state.
Try Using a model class or array to keep the values of checkbox and update it on onclick if that checkbox may solve your issue I think.
holder.checkBox.setChecked(mCheckList.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCheckList.set(position, !mCheckList.get(position));
notifyDataSetChanged();
}
});
I have a problem with a listView consisting of check-boxes. When I scroll some items get selected automatically.
This is my code for the getView method in my custom adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.prerequisite_course, parent, false);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.course_checkbox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Course course = (Course) cb.getTag();
if (cb.isChecked())
checkBoxChecked(cb, course);
else
checkBoxNotChecked(cb, course);
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
Course course = coursesDataManager.getCourseInPosition(position);
String currentId = COURSES_SUBJECT + " " + course.getId();
holder.name.setText(currentId);
holder.name.setTag(course);
// for the clear button:
if (!holder.name.isChecked()) {
checkBoxNotChecked(holder.name, (Course) holder.name.getTag());
}
return convertView;
}
private void checkBoxChecked(CheckBox cb, Course add) {
cb.setBackgroundColor(Color.rgb(1, 67, 121));
cb.setTextColor(Color.WHITE);
myCoursesManager.addPrerequisite(add);
}
private void checkBoxNotChecked(CheckBox cb, Course remove) {
cb.setBackgroundColor(Color.WHITE);
cb.setTextColor(Color.BLACK);
myCoursesManager.removePrerequisite(remove);
}
Also, criticism of the code would be welcomed as I'm new to android development. Thank you.
You need to maintain boolean array for checkbox,try this way this worked for me
public class CustomAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private List<ModelPooja> listData;
public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
context = mainActivity;
this.listData = listData;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.checks.setOnCheckedChangeListener(null);
holder.checks.setFocusable(false);
if (listData.get(position).isselected) {
holder.checks.setChecked(true);
} else {
holder.checks.setChecked(false);
}
holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton cb, boolean b) {
if (checkMaxLimit()) {
if (listData.get(position).isselected && b) {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
} else {
holder.checks.setChecked(false);
listData.get(position).isselected = false;
Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
}
} else {
if (b) {
listData.get(position).isselected = true;
} else {
listData.get(position).isselected = false;
}
}
}
});
holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
return convertView;
}
public boolean checkMaxLimit() {
int countermax = 0;
for(ModelPooja item : listData){
if(item.isselected){
countermax++;
}
}
return countermax >= 5;
}
public class ViewHolder {
TextView tv;
public CheckBox checks;
}
}
Create a Model Class for saving the checkBox status for the list items.
public class ModelClass
{
int position=null;
String item=null;
boolean selected=false;
public ModelClass(int position,String item, boolean selected) {
super();
this.position=position;
this.item=item;
this.selected = selected;
}
}
Then in your Adapter Class
private ArrayList<ModelClass> arl;
Inside your getView()
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
arl.get(position).selected=isChecked;
}
});
holder.checkBox.setChecked(arl.get(position).selected);
And now no matter how much you scroll your checkBoxes state will be consistent(Trust me, I have used this code in my Project).
I am trying to get a checked value but it is not showing the value, I have implemented custom adapter there i am not getting how to update position
My code is..
public Add_member_adapter(Context a, ArrayList<ItemsModel> d) {
context = a;
data = d;
itemChecked = new boolean[data.size()];
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
public final Object getItem(int position) {
return data.get(getCount() - position - 1);
}
public final long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView Name, UserId_Fk;
CheckBox chkbox;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
vi.setVisibility(View.GONE);
} else {
tempValues = null;
tempValues = (ItemsModel) data.get(position);
holder.Name.setText(tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
holder.chkbox.setChecked(false);
holder.chkbox.setChecked(itemChecked[position]);
holder.chkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked[position] = isChecked;
}
});
}
return vi;
}
and i am declaring List setOnItemClickListener another in activity
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckBox checkbox = ((CheckBox) view.findViewById(R.id.chkbox));
String Name = list.getItemAtPosition(position).toString();
}
});
Any clickable view inside a list view item will steal list view onItemClickListener click.
If you want to catch check event on your check boxes, you should use CompoundButton.OnCheckedChangeListener instead of View.OnClickListener.
Since convertView on ListView is re-useable, you must aware that your data will not accurate. You must make a data set to hold your values that you want to keep.
This code is from your adapter
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();
holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
vi.setVisibility(View.GONE);
} else {
tempValues = null;
tempValues = (ItemsModel) data.get(position);
holder.Name.setText(tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
holder.chkbox.setChecked(checkedHolder[position]);
holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedHolder[position] = isChecked;
}
});
}
return vi;
}
public boolean[] checkedHolder;
private void createCheckedHolder() {
checkedHolder = new boolean[getCount()];
}
Call createCheckedHolder on your adapter's constructor. And if you want to get the checked value, you can get from checkedHolder
From the checkedHolder, you can get the position of any items that selected by the user, then you can call below code from your activity
private ArrayList<String> getSelectedNames(){
List<String> names = new ArrayList<String>();
for (int i = 0; i < adapter.getCount(); i++) {
if(adapter.checkedHolder[i]){
//get all name values that checked by user
names.get(i).add(yourDataSet.getItemName().toString());
}
}
return names
}
See my code. I am using this way for getting the checked item from listview :
public class MovieAdapter extends ArrayAdapter<MovieVO>{
Context context;
List<MovieVO> movieList;
public MovieAdapter(Context context, int resource, List<MovieVO> objects) {
super(context, resource, objects);
this.context = context;
movieList = objects;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_row, null);
TextView tvMovieName = (TextView) convertView.findViewById(R.id.tvMovieName);
TextView tvRating = (TextView) convertView.findViewById(R.id.tvRating);
TextView tvGenre = (TextView) convertView.findViewById(R.id.tvGenre);
TextView tvYear = (TextView) convertView.findViewById(R.id.tvYear);
ImageView ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
CheckBox cbSelect = (CheckBox) convertView.findViewById(R.id.cbSelect);
if(movieList.get(position).isSelected())
{
cbSelect.setChecked(true);
}
else
{
cbSelect.setChecked(false);
}
cbSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
movieList.get(position).setSelected(true);
}
else
{
movieList.get(position).setSelected(false);
}
}
});
tvMovieName.setText(movieList.get(position).getName());
tvRating.setText(movieList.get(position).getRating());
tvGenre.setText(Arrays.toString(movieList.get(position).getGenre()).replace("[", "").replace("]", ""));
tvYear.setText(movieList.get(position).getYear());
Picasso.with(context).load(movieList.get(position).getImageUrl()).into(ivImage);
return convertView;
}
Here, i add the true or false value based on the item check and uncheck. When you want all selected value from list, then just iterate the list and check the true false value.
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;
}
}