How to get id only those which I have checked in android? - java

if (v.getId() == R.id.contact_btnNext) {
StringBuilder id = new StringBuilder();
String prefix = "";
for (Datamodel datamodel : mAdapter.arrSelectedModel ) {
id.append(prefix).append(datamodel.getId());
prefix = ",";
}
Using this code I am getting value of all ListView it's which is checked and unchecked in id:
public class AdapterContactAddfriend extends BaseAdapter {
private Context context;
private ArrayList<Datamodel> arrModel;
public ArrayList<Datamodel> arrSelectedModel;
public ArrayList<Boolean> arrCheckboxState;
ViewHolder holder;
ImageLoader imageloader;
public AdapterContactAddfriend(Context context,
ArrayList<Datamodel> arrModel) {
this.context = context;
this.arrModel = arrModel;
arrCheckboxState = new ArrayList<Boolean>();
arrSelectedModel = new ArrayList<Datamodel>();
imageloader = new ImageLoader(context);
for (int i = 0; i < arrModel.size(); i++) {
arrCheckboxState.add(false);
}
}
#Override
public int getCount() {
return arrModel.size();
}
#Override
public Object getItem(int position) {
return arrModel.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater
.inflate(R.layout.row_contactaddfriend, null);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.rowcontact_checkbox);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtContactName.setText(arrModel.get(position).contactName);
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int position = (Integer) buttonView.getTag();
if (isChecked) {
arrCheckboxState.set(position, true);
arrModel.get(position).setChecked(true);
arrSelectedModel.add(arrModel.get(position));
} else {
arrModel.get(position).setChecked(false);
arrCheckboxState.set(position, false);
arrSelectedModel.remove(arrModel.get(position));
}
}
});
return convertView;
}
}
This is my adapter and I have taken boolean value public ArrayList<Boolean> arrCheckboxState for check and unchecked value but I am unable to get value of id which is checked where am doing mistake please tell me because I am getting value of id of all list-view item but I have to get only those id which is selected.

Remove this line..because your are not setting position as tag..as position is already there in parameters why are you getting it from tag ?
int position = (Integer) buttonView.getTag();
Use it like this
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(position<arrCheckboxState.size()){
if (isChecked) {
arrCheckboxState.set(position, true);
arrModel.get(position).setChecked(true);
arrSelectedModel.add(arrModel.get(position));
} else {
arrModel.get(position).setChecked(false);
arrCheckboxState.set(position, false);
arrSelectedModel.remove(arrModel.get(position));
}
}
}
});

Related

Android - setting a listener on items in each row of ExpandableListView

On each row i have two EditTexts.when i change the first one , i want the other to be changed as well(if needed ,for now just setting there '25' for testing).I am using a viewholder pattern,and setting on each of the first edittexts a TextChangedListener,expecting the second to be changed as well .problem is , whenever i change ANY of the first edittexts on ANY of the rows ,only the edittext on the last row is changed. here is the code of the adapter(listener is in getgroupview):
public class CustomExpandableListAdapterNewWorkout extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;
public CustomExpandableListAdapterNewWorkout(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
String categoryName= (String)getGroup(listPosition);
if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_exercises_exercise, null);
childViewHolder = new ChildViewHolder();
childViewHolder.mChildTitle = (TextView) convertView.findViewById(R.id.expandedListItem);
childViewHolder.mChildImage = (ImageView) convertView.findViewById(R.id.ImgExercisePic);
convertView.setTag(childViewHolder);
}
else
{
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.mChildTitle.setText(expandedListText);
childViewHolder.mChildTitle.setTextAppearance(context,R.style.TitleStyle);
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group_new_workout_exercise, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.mGroupTitle = (TextView) convertView.findViewById(R.id.exerciseName);
groupViewHolder.mMinSets = (EditText) convertView.findViewById(R.id.edtMinimumSets);
groupViewHolder.mMaxSets = (EditText) convertView.findViewById(R.id.edtMaxSets);
groupViewHolder.mMinSets.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
groupViewHolder.mMaxSets.setText("25");
}
});
convertView.setTag(groupViewHolder);
}
else
{
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.mGroupTitle.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
public final class GroupViewHolder {
TextView mGroupTitle;
EditText mMinSets;
EditText mMaxSets;
}
public final class ChildViewHolder {
TextView mChildTitle;
ImageView mChildImage;
}
}
probably there is something basic i don't understand about adapters and viewholders, and i would like to know the correct method to address it.
ok my bad.
should have declared "GroupViewHolder groupViewHolder" inside the getGroupView method,this way a new one with a new listener is created each time and affects his corresponding row

checkboxes in recyclerview get changed automatically by scrolling in android

ArrayList<HashMap<String, String>> hashMapArrayList;
public void onBindViewHolder(final EChallanRecyclerCardViewAdapter.ViewHolder holder, int position) {
holder.cbSelectedchallans.setChecked(false);
holder.cbSelectedchallans.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
String cvTotChallanPayAmount= holder.tvChallanPayAmount.getText().toString().replace("₹","");
int TotChallanPayAmount=Integer.parseInt(cvTotChallanPayAmount);
TotaltoPay=TotaltoPay+TotChallanPayAmount;
Bundle bundle=new Bundle();
bundle.putInt("TotaltoPay",TotaltoPay);
Intent intent = new Intent("Add_Send_TotaltoPay");
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(cntx).sendBroadcast(intent);
} else {
if(TotaltoPay!=0) {
String cvTotChallanPayAmount = holder.tvChallanPayAmount.getText().toString().replace("₹", "");
int TotChallanPayAmount = Integer.parseInt(cvTotChallanPayAmount);
TotaltoPay = TotaltoPay - TotChallanPayAmount;
Bundle bundle=new Bundle();
bundle.putInt("TotaltoPay",TotaltoPay);
Intent intent = new Intent("Remove_Send_TotaltoPay");
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(cntx).sendBroadcast(intent);
}
}
}
});
}
In Adapter class add dataObjectHolder.setIsRecyclable(false); in DataObjectHolder
Example code
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.content_my_leaves, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
dataObjectHolder.setIsRecyclable(false); // this line you should add
return dataObjectHolder;
}
First Use like this adpter class:
public class MyAdapter extends ArrayAdapter<ModelClass> {
private final ArrayList<ModelClass> list;
private final Activity context;
public MyAdapter(Activity context, ArrayList<ModelClass> list) {
super(context, R.layout.raw_project_add, list);
this.context = context;
this.list = list;
}
class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.raw_project_add, null);
viewHolder = new ViewHolder();
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.chkboxProject);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.chkboxProject, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.checkbox.setText(list.get(position).getTitle());
viewHolder.checkbox.setTypeface(Utils.getTypeFace(context));
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}
implement AdapterView.OnItemClickListener in your class.
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
CheckBox checkbox = (CheckBox) v.getTag(R.id.chkboxProject);
// Toast.makeText(v.getContext(), isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if (checkbox.isChecked())
return "is checked";
else
return "is not checked";
}

How to get checked items data and count from ExpandableListView

I want to show the count and all the data when clicked. But so far in testing using click on the Parent item
I tried increment checkedCount when check. But it show only checked count in in one group child elemements.
My Adapter:
public class RubExpandAdapter extends BaseExpandableListAdapter {
private Context context;
private List<RubricsModel> rubrics;
private List<VacancyModel> listForRubricListView;
private List<VacancyModel> listForSubRubricListView;
private HashMap<Integer, boolean[]> mChildCheckStates;
public static int rubricPage = 1;
public static int rubricId;
public static int subRubricPage = 1;
public static int subRubricId;
int checkedCount;
FragmentManager fm;
public RubExpandAdapter(Context context, ArrayList<RubricsModel> rubrics, FragmentManager fragmentManager) {
this.context = context;
this.rubrics = rubrics;
fm = fragmentManager;
listForRubricListView = new ArrayList<>();
mChildCheckStates = new HashMap<>();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition)
.getItems();
return chList.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
SubRubricsModel subRubricsModel = (SubRubricsModel) getChild(groupPosition,
childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.rubrics_child_row_layout, null);
}
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
TextView tv = (TextView) convertView.findViewById(R.id.tvsubrubricsfr);
tv.setText(subRubricsModel.getSubRubricName());
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBox.setChecked(!checkBox.isChecked());
}
});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
} else {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
}
for (int i = 0; i < mChildCheckStates.size(); i++){
if (mChildCheckStates.containsValue(true)){
checkedCount ++;
}
}
}
});
if (mChildCheckStates.containsKey(mGroupPosition)) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
checkBox.setChecked(getChecked[mChildPosition]);
} else {
boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
mChildCheckStates.put(mGroupPosition, getChecked);
checkBox.setChecked(false);
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition)
.getItems();
return chList.size();
}
#Override
public Object getGroup(int groupPosition) {
return rubrics.get(groupPosition);
}
#Override
public int getGroupCount() {
return rubrics.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final RubricsModel rubricsModel = (RubricsModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.rubrics_group_row_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvrubricsfr);
tv.setText(rubricsModel.getRubricName());
ImageView indicator = (ImageView) convertView.findViewById(R.id.ivGroupIndicator);
if (isExpanded) {
indicator.setImageResource(R.drawable.arrow_down);
} else {
indicator.setImageResource(R.drawable.arrow_right);
}
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// RubFragment.searchByRubrics = true;
// rubricId = rubricsModel.getRubricID();
// searchByRubric(String.valueOf(rubricId));
Toast.makeText(context, String.valueOf(mChildCheckStates.size()), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
how to do, tell me, pls?

Set Radiobutton true onItemclick on listview

I want to mark radio button true in listview onItemClick so what I am doing is
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
LinearLayout item_view = (LinearLayout) view;
final RadioButton itemcheck = (RadioButton) item_view
.findViewById(R.id.listview_radiobutton);
if (itemcheck.isChecked()) {
itemcheck.setChecked(true);
} else {
itemcheck.setChecked(false);
}
itemcheck.setChecked(true);
}
My listview
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginLeft="#dimen/view_margin_15"
android:layout_marginRight="#dimen/view_margin_15"
android:layout_marginTop="#dimen/view_margin_20"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:divider="#drawable/list_divider"
android:dividerHeight="#dimen/padding_2"
android:fastScrollEnabled="true"
android:footerDividersEnabled="true"
android:listSelector="#null"
android:scrollbars="none"
android:textFilterEnabled="true"
android:textStyle="normal" />
Edit:--
My Adapter code :--
public class Adapter extends ArrayAdapter<Details> {
private Context mContext;
private List<Details> transList;
private LayoutInflater infalInflater;
private OnCheckedRadioButon onCheckedRadioButon;
private Typeface mTypeface, mEditTypeface, mPasswdTypeface;
private int mSelectedPosition = -1;
private RadioButton mSelectedRB;
private PromoViewHolder viewHolder;
public Adapter(Context context, List<Details> mtransList, OnCheckedRadioButon onCheckedRadioButon) {
super(context, R.layout.dialog_listview, mtransList);
this.mContext = context;
this.transList = mtransList;
this.onCheckedRadioButon = onCheckedRadioButon;
this.infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
try {
viewHolder = null;
row = infalInflater.inflate(R.layout.dialog_listview_code, parent, false);
viewHolder = new ViewHolder();
viewHolder.radiobutton = (RadioButton) row.findViewById(R.id.radiobutton);
viewHolder.listview_name = (TextView) row.findViewById(R.id.listview_name);
setValueText(viewHolder, position);
viewHolder.radiobutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
onCheckedRadioButon.onCheckedButton(transList.get(position));
}
}
});
viewHolder.radiobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
}
mSelectedPosition = position;
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
viewHolder.radiobutton.setChecked(false);
} else {
viewHolder.radiobutton.setChecked(true);
if (mSelectedRB != null && viewHolder.radiobutton != mSelectedRB) {
mSelectedRB = viewHolder.radiobutton;
}
}
row.setTag(viewHolder);
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
private void setValueText(ViewHolder viewHolder, final int position) {
viewHolder.listview_name.setText(transList.get(position).getName());
}
public interface OnCheckedRadioButon {
void onCheckedButton(Details pr);
}
class ViewHolder {
RadioButton radiobutton;
TextView listview_name;
}
}
It is working but if I click on another position of the listview then the previous radiobutton position is not unchecked.I want to uncheck all the previous ones and mark only one at a time.
Any help will be appreciated.
Use POJO classes (Setter or Getter) to manage this type of condition. Use boolean variable in that class and change its values according to the position true or false.
POJO Class Like :
public class CheckListSource {
public boolean isSelected;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
In your adapter :
private ArrayList<CheckListSource > itemsData;
public ChildListAdapter(Activity activity, ArrayList<ChildListResponse> baseResponse) {
this.itemsData = baseResponse;
this.activity = activity;
}
In BindViewHolder Like :
viewHolder.checkParentView.setTag(itemsData.get(position));
viewHolder.checkParentView.setOnClickListener(checkedListener);
private View.OnClickListener checkedListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckListSource childListResponse = (CheckListSource ) v.getTag();
if (childListResponse.isSelected())
childListResponse.setSelected(false);
else
childListResponse.setSelected(true);
notifyDataSetChanged();
}
};
Use a boolean array in your Activity. Each boolean value corresponds to a RadioButton.
If a RadioButton is checked, set its boolean value to true, and set all other boolean values in the array to false.
In the getView() of your Adapter, call your_radio_button.setChecked(your_boolean_array[position]).
Once the boolean array is modified, call your_adapter.notifyDataSetChanged().
Checkout this it works for me..
public class ProgramAdapter extends ArrayAdapter<KeyInformation> {
private final String TAG = "ProgramAdapter";
private List<KeyInformation> mList;
private Context mContext;
private LayoutInflater mInflater;
private int mSelectedPosition = -1;
private RadioButton mSelectedRB;
private String mUserApllication = "";
public ProgramAdapter(Context context, List<KeyInformation> objects) {
super(context, R.layout.program_item, objects);
mContext = context;
mList = objects;
mInflater = ( LayoutInflater ) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mUserApllication = Settings.System.getString(mContext.getContentResolver(),
Settings.System.PROGRAMMABLE_KEY_ACTION);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.program_item, parent, false);
holder.mImageView = (ImageView) convertView.findViewById(R.id.icon);
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mSeparator = (TextView) convertView.findViewById(R.id.title_separator);
holder.mRadioButton = (RadioButton) convertView.findViewById(R.id.radioBtn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mRadioButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
if(position != mSelectedPosition && mSelectedRB != null){
mSelectedRB.setChecked(false);
}
mUserApllication ="";
mSelectedPosition = position;
mSelectedRB = (RadioButton) view;
}
});
String userApp = mList.get(position).packageName;
if(mUserApllication.equals(userApp)) {
mSelectedPosition = position;
}
if (mSelectedPosition != position) {
holder.mRadioButton.setChecked(false);
} else {
holder.mRadioButton.setChecked(true);
mSelectedRB = holder.mRadioButton;
}
holder.mImageView.setImageDrawable(mList.get(position).icon);
holder.mTextView.setText(mList.get(position).lable);
if (position == 5) {
holder.mSeparator.setVisibility(View.VISIBLE);
} else {
holder.mSeparator.setVisibility(View.GONE);
}
return convertView;
}
public int getmSelectedPosition () {
return mSelectedPosition;
}
private static class ViewHolder {
ImageView mImageView;
TextView mTextView;
TextView mSeparator;
RadioButton mRadioButton;
}
}
Please go through below, it will work.
class Details{
public boolean isSelect=false;
}
public class Adapter extends ArrayAdapter<Details> {
private Context mContext;
private List<Details> transList;
private LayoutInflater infalInflater;
private OnCheckedRadioButon onCheckedRadioButon;
private Typeface mTypeface, mEditTypeface, mPasswdTypeface;
private int mSelectedPosition = -1;
private RadioButton mSelectedRB;
private PromoViewHolder viewHolder;
public Adapter(Context context, List<Details> mtransList, OnCheckedRadioButon onCheckedRadioButon) {
super(context, R.layout.dialog_listview, mtransList);
this.mContext = context;
this.transList = mtransList;
this.onCheckedRadioButon = onCheckedRadioButon;
this.infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void updateList(){
viewHolder.radiobutton.setChecked(false);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
try {
viewHolder = null;
row = infalInflater.inflate(R.layout.dialog_listview_code, parent, false);
viewHolder = new ViewHolder();
viewHolder.radiobutton = (RadioButton) row.findViewById(R.id.radiobutton);
viewHolder.listview_name = (TextView) row.findViewById(R.id.listview_name);
setValueText(viewHolder, position);
viewHolder.radiobutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
onCheckedRadioButon.onCheckedButton(transList.get(position));
}
}
});
viewHolder.radiobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Details detail=transList.get(position);
for(int i=0;i<transList.size;i++){
Detail b=transList.get(i);
b.isSelect=false;
}
detail.isSelect=true;
adapter.notifydatasetchange();
}
});
Details detail=transList.get(position);
if (detail.isSelect) {
viewHolder.radiobutton.setChecked(true);
} else {
viewHolder.radiobutton.setChecked(false);
}
row.setTag(viewHolder);
} catch (Exception e) {
e.printStackTrace();
}
return row;
}
private void setValueText(ViewHolder viewHolder, final int position) {
viewHolder.listview_name.setText(transList.get(position).getName());
}
public interface OnCheckedRadioButon {
void onCheckedButton(PromoDetails promoDetails);
}
class ViewHolder {
RadioButton radiobutton;
TextView listview_name;
}
}

ListView item not deleting/copying correctly even with view holder

My listview isn't updating correctly when I try to remove or copy an item. The data is storing correctly in the database, but is not displayed correctly on the listview, which I think is due to issues with recycling views.
For example, I deleted an item with id 87, then I copied a different item and it showed up in the listview as having an id of 87 even though it should have been 88 (and was correctly stored in the db as 88).
I've tried looking at some other examples of code with view holders and custom adapters, but I can't seem to figure out the issue. Have I applied the ViewHolder concept correctly to my custom adapter?
public class WorkoutAdapter extends ArrayAdapter<ExerciseSetModel> {
private Context context;
private ArrayList<ExerciseSetModel> exerciseSetList;
private JSONParser jParser = new JSONParser();
private String isComplete;
private String exerciseId;
public WorkoutAdapter(Context context, ArrayList<ExerciseSetModel> objects) {
super(context, R.layout.exercise_set_item, objects);
this.context = context;
this.exerciseSetList = objects;
isComplete = "";
}
private class ViewHolder{
TextView tvExerciseSetName;
TextView tvExerciseSetDetails;
CheckBox cbIsComplete;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout view = (RelativeLayout) convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (RelativeLayout) inflater.inflate(R.layout.exercise_set_item, parent, false);
holder = new ViewHolder();
holder.tvExerciseSetName = (TextView) view.findViewById(R.id.exerciseSetName);
holder.tvExerciseSetDetails = (TextView) view.findViewById(R.id.exerciseSetDetails);
holder.cbIsComplete = (CheckBox) view.findViewById(R.id.cbIsComplete);
if (exerciseSetList.get(position).getIsComplete() == 1) {
holder.cbIsComplete.setChecked(true);
isComplete = "1";
}
else {
holder.cbIsComplete.setChecked(false);
isComplete = "0";
}
holder.cbIsComplete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
exerciseSetList.get(position).setIsComplete(1);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "1";
} else {
exerciseSetList.get(position).setIsComplete(0);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "0";
}
new UpdateExerciseComplete().execute();
}
});
holder.tvExerciseSetName.setText(exerciseSetList.get(position).getExerciseName());
holder.tvExerciseSetDetails.setText(exerciseSetList.get(position).getExerciseDetails() + " id: " + exerciseSetList.get(position).getExerciseId());
}
else{
holder = (ViewHolder) view.getTag();
}
return view;
}
class UpdateExerciseComplete extends AsyncTask<String, String, String> {
private String updateIsCompleteUrl;
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
updateIsCompleteUrl = "localhost/android_connect/updateIsComplete.php";
LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
params.put("is_complete", isComplete);
params.put("exercise_id", exerciseId);
jParser.makePostRequest(updateIsCompleteUrl, params);
return null;
}
protected void onPostExecute(String file_url) {
}
}
}
You have implemented wrong ViewHolder pattern. replace modified getView
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout view = (RelativeLayout) convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (RelativeLayout) inflater.inflate(R.layout.exercise_set_item, parent, false);
holder = new ViewHolder();
holder.tvExerciseSetName = (TextView) view.findViewById(R.id.exerciseSetName);
holder.tvExerciseSetDetails = (TextView) view.findViewById(R.id.exerciseSetDetails);
holder.cbIsComplete = (CheckBox) view.findViewById(R.id.cbIsComplete);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
if (exerciseSetList.get(position).getIsComplete() == 1) {
holder.cbIsComplete.setChecked(true);
isComplete = "1";
}
else {
holder.cbIsComplete.setChecked(false);
isComplete = "0";
}
holder.cbIsComplete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
exerciseSetList.get(position).setIsComplete(1);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "1";
} else {
exerciseSetList.get(position).setIsComplete(0);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "0";
}
new UpdateExerciseComplete().execute();
}
});
holder.tvExerciseSetName.setText(exerciseSetList.get(position).getExerciseName());
holder.tvExerciseSetDetails.setText(exerciseSetList.get(position).getExerciseDetails() + " id: " + exerciseSetList.get(position).getExerciseId());
return view;
}
For getting correct position in onclick() function. you have to tag position on your view holder.cbIsComplete.setTag(position) then get this tag inside onCLick() using int position=(Integer)buttonView.getTag()

Categories

Resources