java.lang.IndexOutOfBoundsException: Invalid index 20, size is 20 - java

I'm using a custom adapter to load 20 items from a JSON string as well as implement ads in the 4th and 14th position. What I'm encountering is that when I scroll to view item #19, it crashes with the error in the title.
I know that the error is saying that it's trying to access an index that is not in the array but I think it's because it's included the adViews in the index (which it shouldn't). I feel like this is something simple that I am missing but please help. Here are my 2 adapters:
public class ListViewAdapter extends BaseAdapter implements AdListener {
private final Activity activity;
private final BaseAdapter delegate;
public ListViewAdapter(Activity activity, BaseAdapter delegate) {
this.activity = activity;
this.delegate = delegate;
}
#Override
public int getCount() {
// Total count includes list items and ads.
return delegate.getCount() + 2;
}
#Override
public Object getItem(int position) {
// Return null if an item is an ad. Otherwise return the delegate item.
if (isItemAnAd(position)) {
return null;
}
return delegate.getItem(position - 1);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (isItemAnAd(position)) {
if (convertView instanceof AdView) {
return convertView;
} else {
AdView adView = new AdView(activity, AdSize.SMART_BANNER,"AD ID (removed for post)" );
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
return adView;
}
} else {
return delegate.getView(position-1, convertView, parent);
}
}
#Override
public int getViewTypeCount() {
return delegate.getViewTypeCount() + 1;
}
#Override
public int getItemViewType(int position) {
if (isItemAnAd(position)) {
return delegate.getViewTypeCount();
} else {
return delegate.getItemViewType(getOffsetPosition(position));
}
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return (!isItemAnAd(position)) && delegate.isEnabled(getOffsetPosition(position));
}
private boolean isItemAnAd(int position) {
// Place an ad at the first and last list view positions.
return (position == 4 || position == 14);
}
#Override
public void onDismissScreen(Ad arg0) {
}
#Override
public void onFailedToReceiveAd(Ad arg0, AdRequest.ErrorCode arg1) {
}
#Override
public void onLeaveApplication(Ad arg0) {
}
#Override
public void onPresentScreen(Ad arg0) {
}
#Override
public void onReceiveAd(Ad arg0) {
}
private int getOffsetPosition(int position) {
return position - 1;
}
}
Here's the custom adapter which sets the listview:
class CustomMovieAdapter extends BaseAdapter {
private ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public CustomMovieAdapter(Context context, ArrayList<SearchResults> results){
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return searchArrayList.size()+2;
}
#Override
public Object getItem(int position) {
return searchArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if(convertView == null){
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
assert convertView != null;
holder.txtRating = (TextView) convertView.findViewById(R.id.movieRating);
holder.txtMovieName = (TextView) convertView.findViewById(R.id.movieName);
holder.txtMovieSize = (TextView) convertView.findViewById(R.id.movieSize);
holder.txtImdbRating = (TextView) convertView.findViewById(R.id.imdbScore);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtRating.setText(searchArrayList.get(position+1).getRating());
holder.txtMovieName.setText(searchArrayList.get(position+1).getName());
holder.txtMovieSize.setText(searchArrayList.get(position+1).getSize());
holder.txtImdbRating.setText(searchArrayList.get(position+1).getImdbRating());
return convertView;
}
class ViewHolder {
TextView txtRating;
TextView txtMovieName;
TextView txtMovieSize;
TextView txtImdbRating;
}
}

#Override
public int getCount() {
// Total count includes list items and ads.
return delegate.getCount() + 2;
}
you should return the size of the dataset without changing it. If you need to show more items, add those to the dataset. Changing it with
#Override
public int getCount() {
// Total count includes list items and ads.
return delegate.getCount() ;
}
Should fix your exception.
Edit, in getView you should avoid tampering the position android is providing you:
position+1

Related

Interact with view of a specific item in a custom ListView

I am using a custom BaseAdapter to create a custom list. What I'm trying to do is have a public function outside of getView(), in this example setName(), that can interact with a view of a specific item on the list.
For example calling setName(1); should make invisible the TextView of the second item on the list.
Thank you in advance.
public class ListAdapterNew extends BaseAdapter {
private int size;
private Context context;
private ViewHolder viewHolder;
String[][] data;
ArrayList<DataModel> listArray;
public ListAdapterNew(Context context, String[][] data, int size) {
listArray = new ArrayList<DataModel>(size);
for(int i=0;i<size;i++){
listArray.add(new DataModel(i));
}
this.context=context;
this.data=data;
this.size=size;
}
#Override
public int getCount() {
return listArray.size();
}
#Override
public Object getItem(int i) {
return listArray.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
public int getViewTypeCount() { return getCount(); }
#Override
public int getItemViewType(int position) {
return position;
}
private static class ViewHolder
{
TextView number,name;
}
#Override
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater li = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.main_user_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.number = view.findViewById(R.id.main_list_number);
viewHolder.name = view.findViewById(R.id.main_list_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
viewHolder.number.setText((index+1)+")");
viewHolder.name.setText(data[index][0]);
}
view.setClickable(true);
view.setFocusable(true);
return view;
}
public void setName(int index){
viewHolder.name.setVisibility(View.GONE);
}
}
The answer was simple. I use a public function inside the ListAdapterNew to update the data[][] and call notifyDataSetChanged(); on the adapter.
There is also a small change in the getView() of the adapter that allows the view to update when items become visible, as shown below.
if (view == null) {
LayoutInflater li = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.main_user_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.number = view.findViewById(R.id.main_list_number);
viewHolder.name = view.findViewById(R.id.main_list_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (view != null){
viewHolder.number.setText((index+1)+")");
viewHolder.name.setText(data[index][0]);
}
Function that updates the data
public void updateIcons(int index, int data){
this.icons[index]=data;
}

setAdapter in recyclerView cannot be applied

So I followed an online tutorial to have a sort of gallery app. The pictures are shown inside a GridView but I needed to change it into a recyclerview. When I do that though I get that error and my adapter doesn't seem to work with the recyclerview. Here is my adapter code:
class AlbumAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap< String, String >> data;
public AlbumAdapter(Activity a, ArrayList < HashMap < String, String >> d) {
activity = a;
data = d;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
AlbumViewHolder holder = null;
if (convertView == null) {
holder = new AlbumViewHolder();
convertView = LayoutInflater.from(activity).inflate(
R.layout.album_row, parent, false);
holder.galleryImage = (ImageView) convertView.findViewById(R.id.galleryImage);
holder.gallery_count = (TextView) convertView.findViewById(R.id.gallery_count);
holder.gallery_title = (TextView) convertView.findViewById(R.id.gallery_title);
convertView.setTag(holder);
} else {
holder = (AlbumViewHolder) convertView.getTag();
}
holder.galleryImage.setId(position);
holder.gallery_count.setId(position);
holder.gallery_title.setId(position);
HashMap < String, String > song = new HashMap < String, String > ();
song = data.get(position);
try {
holder.gallery_title.setText(song.get(Function.KEY_ALBUM));
holder.gallery_count.setText(song.get(Function.KEY_COUNT));
Glide.with(activity)
.load(new File(song.get(Function.KEY_PATH))) // Uri of the picture
.into(holder.galleryImage);
} catch (Exception e) {}
return convertView;
}
}
class AlbumViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
}
And this is some of the code from the activity in which I set my adapter:
#Override
protected void onPostExecute(String xml) {
AlbumAdapter adapter = new AlbumAdapter(ProfileActivity.this, albumList);
galleryGridView.setAdapter(adapter);
galleryGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
Intent intent = new Intent(ProfileActivity.this, AlbumActivity.class);
intent.putExtra("name", albumList.get(+position).get(Function.KEY_ALBUM));
startActivity(intent);
}
});
}
This is the error I'm currently getting:
Error message
I basically want to fix the adapter so that it works inside a recyclerview.
You have to create RecyclerView.Adapter to work with RecyclerView. It's bit different that BaseAdapter. Let me give you example of how to implement this.
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.album_row, viewGroup, false);
return new AlbumAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
holder.galleryImage.setId(position);
holder.gallery_count.setId(position);
holder.gallery_title.setId(position);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
public ViewHolder(View v) {
galleryImage = v.findViewById(R.id.galleryImage);
gallery_count =v.findViewById(R.id.gallery_count);
gallery_title = v.findViewById(R.id.gallery_title);
}
}
}

How to change one adapter value from another adapter?

I have a little bit problem.In my activity there is two Adapter one is for color selection and another is for size selection. While i clicked one of the item of color then recently the available size adapter should be change but i got problem in size adapter. it changes only when i click the size item. I research and try to solve problem but it doesnt works for me.
Here is my code.
AddToCartActivity.java
public class AddToCartActivity extends BaseActivity{
#Override
protected int getLayout() {
return R.layout.activity_add_to_cart;
}
#Override
protected void init() {
//api called here
}
// response of api
#Override
public void productDetail(ProductCommonModel productCommonModel,
ArrayList<ProductChildModel> productChildModels, HashMap<Integer,
ArrayList<ChildAttributeModel>> childWithAttribute, HashMap<Integer,
ArrayList<ChildImageModel>> childWithImages,
ArrayList<com.hazesoft.dokan.singleproductdetail.model.ColorModel>
colorModels, ArrayList<SizeModel> sizeModels,
ArrayList<RelatedProductModel> relatedProductModels) {
this.productCommonModel = productCommonModel;
this.productChildModels = productChildModels;
this.childWithAttribute = childWithAttribute;
this.childWithImages = childWithImages;
this.colorModels = colorModels;
this.sizeModels = sizeModels;
this.relatedProductModels = relatedProductModels;
tvProductName.setText(productCommonModel.getName());
if (productCommonModel.getSpecialPrice() == 0) {
tvSellingPrice.setText(getString(R.string.rs) + productCommonModel.getSellingPrice());
tvDiscount.setVisibility(View.GONE);
tvSpecialPrice.setVisibility(View.GONE);
} else {
tvSpecialPrice.setText(getString(R.string.rs) + productCommonModel.getSpecialPrice());
tvSellingPrice.setText(getString(R.string.rs) + productCommonModel.getSellingPrice());
tvSellingPrice.setPaintFlags(tvSellingPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
tvDiscount.setText(productCommonModel.getDiscount() + getString(R.string.percentage));
}
setChildDetail(childWithAttribute, productChildModels);
setColorModel(colorModels);
setSizeModel(sizeModels);
quantity = Integer.parseInt(tvQuantityCart.getText().toString());
}
// setcolor adapter
private void setColorModel(ArrayList<ColorModel> colorModels) {
MyColorGridViewAdapter adapter = new MyColorGridViewAdapter(this, colorModels);
gvColor.setAdapter(adapter);
gvColor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.setSelectedPostion(position);
adapter.notifyDataSetChanged();
}
});
}
// set size adapter
private void setSizeModel(ArrayList<SizeModel> sizeModels) {
sizeCustomModels = new ArrayList<>();
for(int i=0;i<sizeModels.size();i++){
sizeCustomModels.add(new SizeCustomModel(sizeModels.get(i).getAttName(),0));
}
setCustomSizeModelToAdapter(sizeCustomModels);
}
// this is code when i click color and change the size adapter but size doesnt change recently only changes when i click any item of the size
public void getSelectedC0lor(String color) {
selectedColor = color;
selectedSize=null;
sizeCustomModels = new ArrayList<>();
availableSize = new ArrayList<>();
for (int i = 0; i < skuColorSIzeList.size(); i++) {
if (skuColorSIzeList.get(i).getColor().equals(selectedColor)) {
availableSize.add(skuColorSIzeList.get(i).getSize());
}
}
for(int i=0;i<sizeModels.size();i++){
String size = null;
int status=0;
for(int j=0;j<availableSize.size();j++){
if(sizeModels.get(i).getAttName().equals(availableSize.get(j))){
size = sizeModels.get(i).getAttName();
status = 1;
break;
}else {
size = sizeModels.get(i).getAttName();
status = 0;
}
}
sizeCustomModels.add(new SizeCustomModel(size,status));
}
sizeRecylerAdapter.getNewModel(sizeCustomModels);
/*sizeRecylerAdapter = new MyCustomSizeRecylerAdapter(sizeCustomModels,this);
rvSize.setAdapter(sizeRecylerAdapter);
sizeRecylerAdapter.notifyDataSetChanged();*/
/*setCustomSizeModelToAdapter(sizeCustomModels);*/
}
}
MyColorGridViewAdapter.java
public class MyColorGridViewAdapter extends BaseAdapter {
Context context;
List<ColorModel> colorModelList;
String select_color;
boolean ch =false;
int checkPosition = -1;
public MyColorGridViewAdapter(Context context, List<ColorModel> colorModelList) {
this.context = context;
this.colorModelList = colorModelList;
}
public void setSelectedPostion(int postion){
this.checkPosition = postion;
}
#Override
public int getCount() {
return colorModelList.size();
}
#Override
public Object getItem(int position) {
return colorModelList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.custom_color_list_item,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(context).load(colorModelList.get(position).getImage()).into(holder.ivImage);
holder.tvColorName.setText(colorModelList.get(position).getAttName());
if(checkPosition==position){
holder.ivChecked.setVisibility(View.VISIBLE);
select_color = colorModelList.get(position).getAttName();
if( context instanceof AddToCartActivity){
((AddToCartActivity) context).getSelectedC0lor(select_color);
}
}else {
holder.ivChecked.setVisibility(View.GONE);
}
if(colorModelList.size()==1){
holder.ivChecked.setVisibility(View.VISIBLE);
select_color = colorModelList.get(position).getAttName();
if( context instanceof AddToCartActivity){
((AddToCartActivity) context).getSelectedC0lor(select_color);
}
}
return convertView;
}
class ViewHolder{
#BindView(R.id.view)
LinearLayout view;
#BindView(R.id.tv_color_name)
TextViewHelper tvColorName;
#BindView(R.id.iv_image)
ImageView ivImage;
#BindView(R.id.iv_checked)
ImageView ivChecked;
public ViewHolder(View view) {
ButterKnife.bind(this,view);
}
}
}
MyCustomSizeRecylerAdapter.java
public class MyCustomSizeRecylerAdapter extends RecyclerView.Adapter<MyCustomSizeRecylerAdapter.MyViewHolder> {
ArrayList<SizeCustomModel> sizeModels;
Context context;
int checkPosition = -1;
String selectedSize;
public MyCustomSizeRecylerAdapter(ArrayList<SizeCustomModel> sizeModels, Context context) {
this.sizeModels = sizeModels;
this.context = context;
}
public void getNewModel(ArrayList<SizeCustomModel> customModels) {
sizeModels.clear();
this.sizeModels = customModels;
selectedSize = null;
Log.d("sizemodel", "getNewModel: " + new Gson().toJson(sizeModels));
notifyDataSetChanged();
}
public void getSelectedPosition(int position) {
checkPosition = position;
notifyDataSetChanged();
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.size_adapter, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_sizeName.setText(sizeModels.get(position).getSize());
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
if (sizeModels.get(position).getStock_Status() == 0) {
holder.ll_mainview.setClickable(false);
holder.ll_sizeAdapter.setBackgroundResource(R.color.blue_700);
} else if (sizeModels.get(position).getStock_Status() == 1) {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
if (checkPosition == position) {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_green);
holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.white));
selectedSize = sizeModels.get(position).getSize();
if (context instanceof AddToCartActivity) {
((AddToCartActivity) context).getSelectSize(selectedSize);
}
} else {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.tv_black));
}
if (sizeModels.size() == 1) {
holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_green);
holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.white));
selectedSize = sizeModels.get(position).getSize();
if (context instanceof AddToCartActivity) {
((AddToCartActivity) context).getSelectSize(selectedSize);
}
}
}
}
#Override
public int getItemCount() {
return sizeModels.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.tv_sizeName)
TextView tv_sizeName;
#BindView(R.id.ll_sizeAdapter)
LinearLayout ll_sizeAdapter;
#BindView(R.id.main_view)
LinearLayout ll_mainview;
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
At first both adapter will set data and after click color item the size adapter must be change but it only changes when i click any of the item. adapter.notifyDataSetChanged() doesnt work here for me.
Both Adapter set
When i click color item but doesnt change size adapter
when i click size item only change size adapter
Use Interface to bridge with two adapter and communicate with each other.

Recyclerview holder mixes on scroll

When scroll recyclerview some items mixes. After I add ads after every 15 items, holder get wrong data. Some items are vip items. I will change background color of these items. But when I scroll it dublicates mixes. How can I solve?
This is my adapter
private Context mCtx;
private List<Car> carList;
private RecyclerViewAnimator mAnimator;
private int AD_TYPE=1;
private int CONTENT_TYPE=2;
private int LIST_AD_DELTA=15;
public ProductAllCarAdapter(RecyclerView recyclerView,Context mCtx, List<Car> carList) {
this.mCtx = mCtx;
this.carList = carList;
mAnimator = new RecyclerViewAnimator(recyclerView);
}
#Override
public ProductAllCarAdapter.ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == AD_TYPE){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_add_item, null);
ProductAllCarAdapter.ProductViewHolder vh = new ProductAllCarAdapter.ProductViewHolder(itemView);
mAnimator.onCreateViewHolder(itemView);
return vh;
} else {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_car_item, null);
ProductAllCarAdapter.ProductViewHolder vh = new ProductAllCarAdapter.ProductViewHolder(itemView);
mAnimator.onCreateViewHolder(itemView);
return vh;
}
}
#Override
public int getItemViewType(int position) {
if (position>0 && position % LIST_AD_DELTA == 0)
return AD_TYPE;
return CONTENT_TYPE;
}
#Override
public void onBindViewHolder(ProductAllCarAdapter.ProductViewHolder holder, int position) {
if (getItemViewType(position) == CONTENT_TYPE) {
final Car car = carList.get(holder.getAdapterPosition());
GlideApp.with(mCtx).load(car.getImg()).into(holder.imageView);
if (car.getVip() == 1) {
holder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mCtx, R.color.colorVip));
holder.imageViewVIP.setVisibility(View.VISIBLE);
}
final String carid = String.valueOf(car.getCarid());
mAnimator.onBindViewHolder(holder.itemView, position);
} else {
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Context mcontext = view.getContext();
Bundle bundle = ActivityOptionsCompat.makeCustomAnimation(mcontext, android.R.anim.fade_in, android.R.anim.fade_out).toBundle();
Intent intent = new Intent(mcontext, AdsItem.class);
mcontext.startActivity(intent, bundle);
}
});
mAnimator.onBindViewHolder(holder.itemView, position);
}
}
private int getRealPosition(int position) {
if (LIST_AD_DELTA == 0) {
return position;
} else {
return position - position / LIST_AD_DELTA;
}
}
#Override
public long getItemId(int position) { return position; }
#Override
public int getItemCount() {
int additionalContent = 0;
if (carList.size() > 0 && carList.size() > LIST_AD_DELTA) {
additionalContent = ( carList.size() / LIST_AD_DELTA);
}
return carList.size() + additionalContent;
}
public static class ProductViewHolder extends RecyclerView.ViewHolder {
private View mView;
ImageView imageView, imageViewVIP;
RelativeLayout relativeLayout;
public ProductViewHolder(View itemView) {
super(itemView);
mView = itemView;
imageView = itemView.findViewById(R.id.imageView);
imageViewVIP = itemView.findViewById(R.id.imageViewVIP);
relativeLayout = itemView.findViewById(R.id.relativeLayoutpc);
}
public void setOnClickListener(View.OnClickListener listener) {
mView.setOnClickListener(listener);
}
}
I think problem onBindViewHolder function use wrong holder. ArrayList also return true value but on scroll it mixes.
You just have to add the corresponding else of the following if statement block.
if (car.getVip() == 1) {
holder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mCtx, R.color.colorVip));
holder.imageViewVIP.setVisibility(View.VISIBLE);
} else {
holder.relativeLayout.setBackgroundColor(ContextCompat.getColor(mCtx, R.color.colorNormal));
holder.imageViewVIP.setVisibility(View.GONE);
}
This is inside your onBindViewHolder function where the view type is CONTENT_TYPE.
Hope that solves your problem.

How to get only updated checkbox status in listview android

I have a listview with a checkbox in a dialog. Now I need to get only the updated checkbox item from the dialog's 'ok click'. For example if the user checks only one checkbox in the row I need to get only that status, but if the user checks and unchecks the checkbox I don't want that status. I tried using a hash table to get and remove statuses when checked, but on listview scroll the checkbox event gets executed and hash table filled with the wrong values.
public class SelectGroupAdaptor extends BaseAdapter {
Activity activity;
ArrayList<SelectGroupItems> data = new ArrayList<>();
Hashtable<Integer,Boolean> status;
public SelectGroupAdaptor(Activity activity) {
this.activity = activity;
}
public void add(SelectGroupItems selectGroupItems) {
this.data.add(selectGroupItems);
notifyDataSetChanged();
status=new Hashtable<>();
}
public ArrayList<SelectGroupItems> getCheckedData () {
return data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView textView;
CheckBox checkBox;
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(R.layout.layout_select_group_row, null);
}
textView = (TextView) convertView.findViewById(R.id.label);
textView.setTypeface(Util.setCustomFont(activity));
checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
textView.setText(data.get(position).getGroupName());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
data.get(position).setChecked(isChecked);
if(status!=null)
{
if(status.get(position)!=null)
{
status.remove(position);
}else
{
status.put(position,isChecked);
}
}
}
});
checkBox.setChecked((data.get(position).isChecked()));
return convertView;
}
public Hashtable<Integer,Boolean> getStatus()
{
return status;
}
}
Can anyone help me
You don't need HashMap, a simple item holder class with a status property would help you. See your modified code:
Edit: If I understood you correctly, you want to get the SelectGroupItems object when its check status changed? If so see edited code below:
public class SelectGroupAdaptor extends BaseAdapter {
Activity activity;
ArrayList<SelectGroupItemHolder> data = new ArrayList<SelectGroupItemHolder>();
OnItemCheckStatusChangedListener onItemCheckStatusChangedListener;
// a item holder class, not a view holder
private static class SelectGroupItemHolder {
SelectGroupItems selectGroupItems;
boolean status;
}
public interface OnItemCheckStatusChangedListener {
public void onItemCheckStatusChanged(SelectGroupItems selectGroupItems, boolean checkStatus, int position);
}
public void setOnItemCheckStatusChangedListener(OnItemCheckStatusChangedListener onItemCheckStatusChangedListener) {
this.onItemCheckStatusChangedListener = onItemCheckStatusChangedListener;
}
public SelectGroupAdaptor(Activity activity) {
this.activity = activity;
}
public void add(SelectGroupItems selectGroupItems) {
SelectGroupItemHolder selectGroupItemHolder = new SelectGroupItemHolder();
selectGroupItemHolder.selectGroupItems = selectGroupItems;
this.data.add(selectGroupItemHolder);
notifyDataSetChanged();
}
public ArrayList<SelectGroupItems> getCheckedData() {
ArrayList<SelectGroupItems> checkedItemList = new ArrayList<SelectGroupItems>();
for (SelectGroupItemHolder selectGroupItemHolder : data) {
if (selectGroupItemHolder.status) {
checkedItemList.add(selectGroupItemHolder.selectGroupItems);
}
}
return checkedItemList;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView textView;
CheckBox checkBox;
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(R.layout.layout_select_group_row, null);
}
textView = (TextView) convertView.findViewById(R.id.label);
textView.setTypeface(Util.setCustomFont(activity));
checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
textView.setText(data.get(position).selectGroupItems.getGroupName());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
data.get(position).status = isChecked;
if(onItemCheckStatusChangedListener != null) {
onItemCheckStatusChangedListener.onItemCheckStatusChanged(data.get(position).selectGroupItems, data.get(position).status, position);
}
}
});
checkBox.setChecked((data.get(position).status));
return convertView;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Set the check status change listener of your selectGroupAdapter instance. ie:
selectGroupAdaptor.setOnItemCheckStatusChangedListener(new SelectGroupAdaptor.OnItemCheckStatusChangedListener() {
#Override
public void onItemCheckStatusChanged(SelectGroupItems selectGroupItems, boolean checkStatus, int position) {
// handle check status change
}
});

Categories