I have a custom listadapter (extends baseAdapter) en in those list items I have a checkbox.
Now when I click on de listitem the checkbox gets checked. But if I would check the first item, the 4th/5th item also gets checked. But the first item, which I actually click on gets stored in the database, which is right. But the 5th doesn't. And if I revisit that activity the first item gets loaded in because he was stored in de db, but also that 5th gets loaded in :O AND I JUST CANT FIND OUT WHY.. So Annoying! So I hope one of you can help me out here..
My IndicatorListAdapter.java
package nl.webdesignwiljan.exp.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import nl.webdesignwiljan.exp.models.Indicator;
import nl.webdesignwiljan.exp.R;
import nl.webdesignwiljan.exp.models.UserIndicator;
public class IndicatorListAdapter extends BaseAdapter {
private Context mContext;
private Indicator[] mIndicatoren;
private UserIndicator[] mUserIndicatoren;
public IndicatorListAdapter(Context context, Indicator[] indicators, UserIndicator[] userIndicatoren) {
mContext = context;
mIndicatoren = indicators;
mUserIndicatoren = userIndicatoren;
}
@Override
public int getCount() {
return mIndicatoren.length;
}
@Override
public Object getItem(int position) {
return mIndicatoren[position];
}
@Override
public long getItemId(int position) {
return 0; // Not going to use
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.indicator_item, null);
holder = new ViewHolder();
holder.indicatorItemLabel = (TextView) convertView.findViewById(R.id.indicatorItemLabel);
holder.indicatorCheckbox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String title = position + " : " + mIndicatoren[position].getDesc();
holder.indicatorItemLabel.setText(title);
// TODO: LOAD THE CHECK-VALUE FROM THE USERINDICATORS IN
for (UserIndicator userIndicatoren : mUserIndicatoren) {
// Check is the current userIndicator and indicator are the same
if (userIndicatoren.getIndicatorId() == mIndicatoren[position].getId()) {
holder.indicatorCheckbox.setChecked(userIndicatoren.isCheck());
}
}
return convertView;
}
public void updateSource(UserIndicator[] userIndicatoren, Indicator[] indicators) {
mUserIndicatoren = userIndicatoren;
mIndicatoren = indicators;
}
private static class ViewHolder {
CheckBox indicatorCheckbox;
TextView indicatorItemLabel;
}
}
The Indicators are the data (like description) and the UserIndicators are the values if they are checked, attached to a user and also to a indicator. So I check after the TODO if a indicator is the same as a UserIndicator and then load in the checked value.
Here is my onClickListener with storing the values in the DB:
// When clicking on a list item
indicatorList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCheckBox = ((CheckBox) view.findViewById(R.id.checkBox));
if (mCheckBox.isChecked()) {
// IF USER_INDICATOR NOT EXISTS -> CREATE NEW, ELSE UPDATE
// Exists
if (dataSource.readUserIndicator(mIndicators[position - 1].getId(), mSharedPreferences.getInt("userId", 0))) {
for (int i = 0; i < mUserIndicators.length; i++) {
if (mIndicators[position - 1].getId() == mUserIndicators[i].getIndicatorId()) {
mUserIndicators[i].setCheck(false);
// Update userIndicator
dataSource.checkUserIndicator(mSharedPreferences.getInt("userId", 0), mIndicators[position - 1], mUserIndicators[i]);
}
}
// Does not exist
}
} else {
// Exists
if (dataSource.readUserIndicator(mIndicators[position - 1].getId(), mSharedPreferences.getInt("userId", 0))) {
for (int i = 0; i < mUserIndicators.length; i++) {
if (mIndicators[position - 1].getId() == mUserIndicators[i].getIndicatorId()) {
mUserIndicators[i].setCheck(true);
dataSource.checkUserIndicator(mSharedPreferences.getInt("userId", 0), mIndicators[position - 1], mUserIndicators[i]);
}
}
// Does not exist
} else {
UserIndicator userIndicator = new UserIndicator();
userIndicator.setCheck(true);
dataSource.createUserIndicator(mSharedPreferences.getInt("userId", 0), mIndicators[position - 1], userIndicator);
mUserIndicators = dataSource.readUserIndicators(mSharedPreferences.getInt("userId", 0));
// Search for the bijhorende indicator
for (int i = 0; i < mUserIndicators.length; i++) {
if (mUserIndicators[i].getIndicatorId() == mIndicators[position - 1].getId()) {
mUserIndicators[i].setCheck(true);
}
}
}
}
indicatorListAdapter.updateSource(mUserIndicators, mIndicators);
indicatorListAdapter.notifyDataSetChanged();
}
});
Aucun commentaire:
Enregistrer un commentaire