2012-08-14 53 views
1

我有一個帶有textview和複選框的列表視圖。文本值存儲在一個HashMap中。數據從數據庫中檢索。 Map鍵是ID,Map值是一個Location。當我將HashMap傳遞給適配器時,它從位置0(零)開始打印。正因爲如此,我得到了一個null文本(沒有值),並在HashMap中遺漏了一個值(最後一個)。我不能使用HashMap作爲數據源嗎?帶有HashMap的ListView適配器

編輯:這裏是代碼

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(); 

     } 
    }); 
} 

這裏是適配器類

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; 
} 

它運行在地圖元素的數量。由於它使用0(通過位置變量),它也會錯過最後一個值。

+0

是的,你可以使用HashMap.May是你的代碼的一些錯誤。你可以顯示你的代碼來找到它 – 2012-08-14 03:56:34

回答

0

你可以做的是

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); 

你將不得不改變你的「myAdapter」適配器的構造函數來持有,而不是 「地圖locLables」 「的ArrayList> myList中」 ..

這將有助於...讓我知道如果問題仍然存在..