I have a custom ListView with the first four elements being buttons. When I click a button it goes into a new activity, I do whatever in the new activity, and then return. When I return, I want the button that I hit to be checked off, however sometimes (and only sometimes) it checks off the wrong box and i can’t figure out why. Here is my code for my custom Adapter:
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_BUTTON = 0;
private static final int TYPE_INFO = 1;
private static final int TYPE_PICTURE = 2;
private static final int TYPE_MAX_COUNT = 3;
private int totalCount = 0;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addButton(final Button btn) {
mButtons.add(btn);
totalCount = totalCount + 1;
notifyDataSetChanged();
}
public void addInfo(final String info) {
mInfo.add(info);
totalCount = totalCount + 1;
notifyDataSetChanged();
}
public void addPicture(final Bitmap pic) {
mPictures.add(pic);
totalCount = totalCount + 1;
notifyDataSetChanged();
}
public void addItem(final String item) {
mData.add(item);
totalCount = totalCount + 1;
notifyDataSetChanged();
}
@Override
public int getCount() {
return totalCount;
}
@Override
public String getItem(int position) {
return "TEST 5000";
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
if (position <= 3) {
return TYPE_BUTTON;
}
if (mInfo.size() + 3 >= position) {
return TYPE_INFO;
} else {
return TYPE_PICTURE;
}
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView " + position + " " + convertView);
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_BUTTON:
convertView = mInflater.inflate(R.layout.button, null);
holder.btn = (Button)convertView.findViewById(R.id.btn);
holder.btn.setText(mButtons.get(position).getText().toString());
holder.btn.setLayoutParams(new LinearLayout.LayoutParams(mButtons.get(position).getLayoutParams().width, mButtons.get(position).getLayoutParams().height));
break;
case TYPE_INFO:
convertView = mInflater.inflate(R.layout.incident_summary_items, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
holder.textView.setText(mInfo.get(position-4));
break;
case TYPE_PICTURE:
convertView = mInflater.inflate(R.layout.image, null);
holder.imageView = (ImageView)convertView.findViewById(R.id.pic);
holder.imageView.setImageBitmap(mPictures.get(position-4-mInfo.size()));
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
switch(type) {
case TYPE_BUTTON:
holder.btn.setText(mButtons.get(position).getText().toString());
holder.btn.setLayoutParams(new LinearLayout.LayoutParams(mButtons.get(position).getLayoutParams().width, mButtons.get(position).getLayoutParams().height));
break;
case TYPE_INFO:
holder.textView.setText(mInfo.get(position-4));
break;
case TYPE_PICTURE:
holder.imageView.setImageBitmap(mPictures.get(position-4-mInfo.size()));
break;
}
}
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
public ImageView imageView;
public Button btn;
}
and here is my code where i set the checkbox and call the new activity:
public void myClickHandler(View v)
{
//get the row the clicked button is in
vwParentRow = (LinearLayout)v.getParent();
btnChild = (Button)vwParentRow.getChildAt(0);
if (btnChild.getText().toString().equals("Take Information")) {
btnChild.setCompoundDrawablesWithIntrinsicBounds(null,null,mCheckMark,null);
Intent intent = new Intent(this, Info.class);
startActivityForResult(intent, 1);
}
if (btnChild.getText().toString().equals("Take Pictures")) {
btnChild.setCompoundDrawablesWithIntrinsicBounds(null,null,mCheckMark,null);
Intent intent = new Intent(this, Pictures.class);
startActivityForResult(intent, 2);
}
}
I found if i commented out the lines
holder.btn.setText(mButtons.get(position).getText().toString());
holder.btn.setLayoutParams(new LinearLayout.LayoutParams(mButtons.get(position).getLayoutParams().width, mButtons.get(position).getLayoutParams().height));
in the else statement of my custom List Adapter, the checklist checked the correct button, but the buttons would change order in the list.
As your using startActivityForResult for new activity you can return the value from that and on your parent where your buttons are use onResume to set the button status as disabled.