1

I have a listview with textview and checkbox. The text values are stored in a HashMap. Data is retrieved from a DB. Map key is the ID and Map value is a Location. When I pass the HashMap to the adapter, it starts printing from position 0 (zero). Because of this I get a null text(with no value) and miss one value(the last one) in the HashMap. Can't I use a HashMap as the data source?

EDIT: Here is the code

public class LocationActivity extends Activity{

myAdapter myA;
Map<Integer,String> locLables;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.locationmain);

    ListView listview = (ListView) findViewById(R.id.listView1);

    String selectQuery = "SELECT  * FROM " + DatabaseHandler.TABLE_LOCATIONLABLES;
    SQLiteDatabase db = new DatabaseHandler(this).getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    locLables = new HashMap<Integer, String>();

    if(cursor != null){
        if (cursor.moveToFirst()) {
            do {

                locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
                System.out.println(cursor.getString(0)+"  "+ cursor.getString(1));
            } while (cursor.moveToNext());
        }
    }
    cursor.close();db.close(); 
    //System.out.println(locLables);
    myA = new myAdapter(this, locLables, listview);
    listview.setAdapter(myA);



    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            if(arg2 == 0 )
                arg2 = arg2+1;
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), locLables.get(arg2), Toast.LENGTH_SHORT).show();

        }
    });
}

Here is the Adapter class

class myAdapter extends BaseAdapter{

ListView listView;
Activity cntx;
Map<Integer,String> locations;
List<Integer> locids = new LinkedList<Integer>();

public myAdapter(Activity context,Map<Integer,String> locLables, ListView lv){
    cntx = context;
    locations = locLables;
    listView = lv;
    System.out.println(locations);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return locations.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View row=null;
    final int posi = position;

    LayoutInflater inflater=cntx.getLayoutInflater();
    row=inflater.inflate(R.layout.locationmain_entry, null);

    TextView tv = (TextView)row.findViewById(R.id.textView12);
    final CheckBox cb = (CheckBox)row.findViewById(R.id.checkBox12);

    if(locids.contains(posi))
        cb.setChecked(true);

    //here I get a null value as the first element and misses the last value..

        tv.setText(locations.get(position));

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                System.out.println("Chedked" + posi+ locations.get(posi));
                //cb.setChecked(true);
                if(!locids.contains(posi))
                    locids.add(posi);
                //System.out.println(locids.get(posi));
            }else if(!isChecked){
                //cb.setChecked(false);
                if(locids.contains(posi))
                    locids.remove(new Integer(posi));
                System.out.println("NOt checked" +posi + locations.get(posi));
            }
        }
    });

    return row;
}

It runs for the number of elements in the Map. As it is using a 0 (by position variable) it misses the last value as well.

user340
  • 375
  • 12
  • 28

1 Answers1

0

What you can do is

ArrayList<Hashmap<Integer,String>> myList = new ArrayList<Hashmap<Integer,String>>();

 if(cursor != null){
    if (cursor.moveToFirst()) {
        do {
            locLables = new HashMap<Integer, String>();
            locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
            myList.add(locLables);
           } while (cursor.moveToNext());
    }
}
cursor.close();db.close(); 
myA = new myAdapter(this, myList, listview);
listview.setAdapter(myA);

You will have to change your "myAdapter" Adapter's constructor to hold "ArrayList> myList" instead of "Map locLables"..

This will help... Let me know if the problem still persists..

Gautam Mandsorwale
  • 1,580
  • 1
  • 18
  • 27