Reputation: 35
So I have a Gridview where each grid item consists of an ImageView and a Checkbox. It calls Image Adapter to populate the grid dynamically. Now, when I select one checkbox, lets say number 5, another checkbox like number 12 gets selected automatically. Similar for deselection. I cannot figure out why this is happening. Any help will be appreciated. Thanks!
public class SignupFarmerCrop extends ActionBarActivity implements View.OnClickListener {
GridView gridView;
private ImageAdapter adpt;
private List<DistrictCommodity> localResponse;
private MediaPlayer mp;
private boolean[] thumbnailsselection;
private int count;
private PreferencesHelper oldSignup;
private String district;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_farmer_crop);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
ImageButton sound = (ImageButton) findViewById(R.id.button);
sound.setOnClickListener(this);
oldSignup = new PreferencesHelper(this);
district = oldSignup.getStringPreferences("districtId");
String language = Locale.getDefault().getLanguage();
if (language.equals("en"))
language = "L001";
else
language = "L002";
// Exec async load task
new EndpointsDistrictCropAsyncTask().execute(new Pair<Context, String>(this, district),
new Pair<Context, String>(this, language));
gridView = (GridView) findViewById(R.id.gridView1);
adpt = new ImageAdapter(this, localResponse);
gridView.setAdapter(adpt);
public void onClick(View v) {
if (mp != null) mp.release();
mp = MediaPlayer.create(this, R.raw.fname);
mp.start();
}
/** Called when the user clicks a crop button */
public void goConfirm(View view) {
// Do something in response to button
Intent intent = new Intent(this, SignupConfirmActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
if(null!=mp){
mp.release();
}
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_signup_farmer_crop, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private List<DistrictCommodity> crops;
private final Logger logger = Logger.getLogger(ImageAdapter.class.getName());
public ImageAdapter(Context context, List<DistrictCommodity> activeResponses) {
this.context = context;
this.crops = activeResponses;
}
public View getView(int position, View convertView, ViewGroup parent) {
View gridView = convertView;
ViewHolder holder;
if (gridView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = inflater.inflate(R.layout.activity_signup_crop_grid, null);
holder.checkbox = (CheckBox) gridView.findViewById(R.id.grid_item_label);
holder.imageview = (ImageView) gridView.findViewById(R.id.grid_item_image);
gridView.setTag(holder);
}
else {
holder = (ViewHolder) gridView.getTag();
}
holder.checkbox.setId(position);
holder.imageview.setId(position);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
logger.warning("Id of checkbox is: " + id);
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.checkbox.setText(crops.get(position).getCommodity());
String crop = crops.get(position).getType();
logger.warning("Image file called is @drawable/" + crop + ".png and context is: " + context.getPackageName());
Drawable drawable = context.getResources().getDrawable(context.getResources()
.getIdentifier("@drawable/potato", null, context.getPackageName()));
holder.imageview.setImageDrawable(drawable);
holder.id = position;
return gridView;
}
@Override
public int getCount() {
return count;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public void setActiveDeals(List<DistrictCommodity> activeResponses) {
this.crops = activeResponses;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}
Upvotes: 1
Views: 1370
Reputation: 11
MainActivity.java
private void enterEditMode() {
try {
listView.setAdapter(null);
listView.setOnItemClickListener(null);
db = new MySQLiteHelper(this);
//call the note object which hold all note object
List<note> note = db.getAllNotes();
int total = note.size();
//declare the values with specific number
String[] values = new String[total];
//add all the note innto the values
int counter = 0;
for (note note1 : note) {
values[counter] = note1.getSubject();
counter++;
}
db.close();
cbAdapter = new checkboxListviewAdapter(this,values);
// Assign adapter to ListView
listView.setAdapter(cbAdapter);
cbAdapter.notifyDataSetChanged();
} catch (Exception e) {
//tell user to send report
showMessage("Error!");
}
}
checkboxListviewAdapter.java
public class checkboxListviewAdapter extends BaseAdapter{
Context context;
String[] subjectList;
Boolean[] selectedList;
private LayoutInflater inflater;
public checkboxListviewAdapter (Context context, String[] subjectList ) {
this.context = context;
this.subjectList = subjectList;
selectedList = new Boolean[subjectList.length];
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < selectedList.length; i++){
selectedList[i] = false;
}
}
@Override
public int getCount() {
return subjectList.length;
}
@Override
public Object getItem(int position) {
return subjectList[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (convertView == null) {
holder = new Holder();
convertView = inflater.inflate(R.layout.checkbox_listview_text_black, null);
holder.tv = (TextView) convertView.findViewById(R.id.textview1);
holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.cb.setChecked(selectedList[position]);
holder.tv.setText(subjectList[position]);
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedList[position] == false) {
selectedList[position] = true;
} else {
selectedList[position] = false;
}
}
});
return convertView;
}
static class Holder {
TextView tv;
CheckBox cb;
}
}
Explaination:
In checkboxListviewAdapter.java
1) Let's say you have 10 checkbox in your listview. So, create a boolean[] to hold your all checkbox's boolean value.
private boolean[] booleanList = new boolean[10];
2) In your constructor, use for loop to set booleanList's as false.
public checkboxListviewAdapter (Context context, String[] subjectList ) {
//this.context = context;
//this.subjectList = subjectList;
//selectedList = new Boolean[subjectList.length];
//inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < selectedList.length; i++){
selectedList[i] = false;
}
}
3) Now, in your getView, add the below code to get the boolean value from the booleanList and set the boolean value each time we scroll the listview.
holder.cb.setChecked(selectedList[position]);
4) Finally, to add a listener to your checkbox, use OnClickListener.
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedList[position] == false) {
selectedList[position] = true;
} else {
selectedList[position] = false;
}
}
});
5) run the code and check the result.
Upvotes: 1
Reputation: 30985
You are saving the state of the checkbox in thumbnailsselection
when it's clicked, but you don't use that state in getView()
to set up the initial appearance of the checkbox. So you are probably getting random checkbox states from recycled views.
Call holder.checkbox.setState()
with the state from thumbnailsselection
to create the GridView
item with the correct checkbox appearance.
Upvotes: 0