i have a problem related to using checkbox in a dynamic listview. I checked similar questions and their answers and tried to adjust my code. Unfortunately, i couldn't get over it.
Briefly, at the begining my checks on the list was disappeared when i scrolled the list. Later on i made some changes on its codes and this time when i clicked on the first item on the list, checkbox repeated every 6th items without clicking on them.
Book.java
package book.lib;
public class Books {
private String title;
private String image;
private Boolean checkbox;
public Books() {
}
//Getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Boolean getCheckbox() {
return checkbox;
}
public void setCheckbox(boolean checkbox) {
this.checkbox = checkbox;
}
}
BookAdapter.java
public class BookAdapter extends ArrayAdapter<Books> {
private final Context context;
private ArrayList<Books> bookList;
LayoutInflater vi;
int Resource;
ViewHolder holder = null;
GetBookThumb getBookThumb;
public BookAdapter(Context context, int resource, ArrayList<Books> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
bookList = objects;
getBookThumb=new GetBookThumb(context);
this.context = context;
}
public int getCount() {
return bookList.size();
}
public Books getItem(int position) {
return bookList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.bookIMG = (ImageView) v.findViewById(R.id.thumb);
holder.bookTITLE = (TextView) v.findViewById(R.id.book_title);
holder.checkBox = (CheckBox) v.findViewById(R.id.checkBox);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
getBookThumb.DisplayImage(bookList.get(position).getImage(), holder.bookIMG);
holder.bookTITLE.setText(bookList.get(position).getTitle());
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Books book = bookList.get(position);
book.setCheckbox(holder.checkBox.isChecked());
Toast.makeText(context.getApplicationContext(),
"Clicked on Checkbox: " + holder.checkBox.getText() +
" is " + holder.checkBox.isChecked(),
Toast.LENGTH_LONG).show();
}
});
}
return v;
}
static class ViewHolder {
public ImageView bookIMG;
public TextView bookTITLE;
public CheckBox checkBox;
}
}
Aucun commentaire:
Enregistrer un commentaire