0

Trying to use a loop to set the text of 12 checkboxes from a db query. Would like to substitute "add1" with an array value and loop through all 12 instead of spelling out each one. Any ideas of how to do this?

Here is the code I am trying to modify:

add1Text= (CheckBox) findViewById(R.id.add1);
if (cursor.getString(cursor.getColumnIndex("add1")) == null) {
    add1Text.setVisibility(View.GONE);
}
else {
    add1Text.setText(cursor.getString(cursor.getColumnIndex("add1")));
}
Toto
  • 89,455
  • 62
  • 89
  • 125

2 Answers2

1

Please note: everything below is off the top of my head, I can't test it right now. I'll test it later when I get a chance.

I think you'll need to keep track of which column to associate with each CheckBox... I'm presuming it's something like this:

Column: add1 => Checkbox: add1Text Column: add2 => Checkbox: add2Text and so on and so forth.

In this circumstance, you'll need to manually keep track of them, possibly in an array. I'd suggest making a Pair class that you can use. I've altered the class from this post [ A Java collection of value pairs? (tuples?) ]

  public class Pair<L,R> {

  private final L left;
  private final R right;

  public Pair(L left, R right) {
    this.left = left;
    this.right = right;
  }

  public L getLeft() { return left; }
  public R getRight() { return right; }

  @Override
  public int hashCode() { return left.hashCode() ^ right.hashCode(); }

  @Override
  public boolean equals(Object o) {
    if (o == null) return false;
    if (!(o instanceof Pair)) return false;
    Pair pairo = (Pair) o;
    return this.left.equals(pairo.getLeft()) &&
           this.right.equals(pairo.getRight());
  }

}

Now, you'll need to make a List (or similar) containing the pairs that you want.

List<Pair<CheckBox, String>> list = new ArrayList<Pair<CheckBox, String>>;
list.add(new Pair<CheckBox, String>((CheckBox) findViewById(R.id.add1), "add1");   
list.add(new Pair<CheckBox, String>((CheckBox) findViewById(R.id.add2), "add2");
list.add(new Pair<CheckBox, String>((CheckBox) findViewById(R.id.add3), "add3");

and so on and so forth

Then you can iterate through the List using something like

foreach (Pair<CheckBox, String> item in list)
{
    if (cursor.getString(cursor.getColumnIndex(item.getLeft()) == null)
    {
        item.getRight().setVisibility(View.GONE);
    }
    else
    {
        item.getRight().setText(cursor.getString(cursor.getColumnIndex(item.getLeft()));
    }
}
Community
  • 1
  • 1
joshhendo
  • 1,964
  • 1
  • 21
  • 28
  • was hoping for something a little more compact. Like: 'code'for (int i =0; i < pList.length(); i++){ fList[i]= (TextView) findViewById(R.id.pList[i]);; fList[i].setText(cursor.getString(cursor.getColumnIndexOrThrow( pList[i]))); }'code' I am trying to minimize my code. – John Dingley Feb 06 '12 at 05:24
  • I'll look into another way you can do it soon, but you will need to store an array of the columns (if the alternate method I'm thinking of will work.) – joshhendo Feb 06 '12 at 05:26
  • Already have them one for the column names and on for the checkbox name. – John Dingley Feb 06 '12 at 05:27
0

Got it! Forgot that I was dealing with objects and also realized I needed a third array. Here is what I came up with.

  • cList contains column names
  • fList are the objects (in this case CheckBoxes)
  • pList are the names of the objects I am selecting from the layout.

    Object fList[]={add1Text,add2Text,add3Text};
    int pList[]={R.id.add1,R.id.add2,R.id.add3};
    cList = cursor.getColumnNames();
    
    for (int i =0; i < fList.length; i++){
        fList[i] = (CheckBox) findViewById(pList[i]);
        if (cursor.getString(cursor.getColumnIndex(cList[i])) == null) {
            ((TextView) fList[i]).setVisibility(View.GONE);
        }
        else {
            ((TextView) fList[i]).setText(cList[i] + " - " + cursor.getString(cursor.getColumnIndex( cList[i])));
        }
    }
    

Sets the CheckBox text to ( Column name - Value )

Toto
  • 89,455
  • 62
  • 89
  • 125