2013-03-12 32 views
0

我想讀取'empname'和'地點'從sqlite數據庫和列表到列表視圖 我試過給定的代碼,但代碼顯示錯誤'不可訪問代碼「可以幫助我找出問題。從sqlite數據庫讀取數據,並通過哈希映射添加到列表視圖

public void listEmps() 
{ 
    this.myList.clear(); 
    HashMap<String, String> map = new HashMap<String, String>(); 
    //HashMap map = new HashMap(); 

    Cursor cursor = this.dbAdapter.ListEmp(); 
    int i; 
    if((cursor !=null)&&(cursor.getCount()>0)) 
    { 
     cursor.moveToFirst(); 
     i=0; 
     if(i>cursor.getCount()) 
     { 
      cursor.close(); 
      myAdapter = new SimpleAdapter(this, myList, R.layout.listadapter, new String[]{"name","place"}, 
        new int[]{R.id.textViewName,R.id.textViewPlace}); 
      this.lvEmp.setAdapter(myAdapter); 
      this.myAdapter.notifyDataSetChanged(); 

     } 
    } 
    while(true) 
    { 
     return; 

     map.put("name", cursor.getString(cursor.getColumnIndex("EmpName"))); 
     map.put("place", cursor.getString(cursor.getColumnIndex("place"))); 
     this.myList.add(map); 
     map = new HashMap(); 
      if (!cursor.isLast()) 
       cursor.moveToNext(); 
      i++; 
      break; 
      cursor.close(); 

    } 
} 

回答

0

您可以使用do->while這樣的:

 public void listEmps() 
{ 


    this.myList.clear(); 
    //HashMap map = new HashMap(); 

    Cursor cursor = this.dbAdapter.ListEmp(); 

if(cursor.moveToFirst()){ 
        do{ 
         HashMap<String, String> map = new HashMap<String, String>(); 
         map.put("name", cursor.getString(cursor.getColumnIndex("EmpName"))); 
         map.put("place", cursor.getString(cursor.getColumnIndex("place"))); 
         this.myList.add(map); 

        }while (cursor.moveToNext()); 
       } 

       cursor.close(); 
     myAdapter = new SimpleAdapter(this, myList, R.layout.listadapter, new String[]{"name","place"}, 
       new int[]{R.id.textViewName,R.id.textViewPlace}); 
     this.lvEmp.setAdapter(myAdapter); 
     this.myAdapter.notifyDataSetChanged(); 


} 

希望這有助於你。

0

你的代碼是不可達becouse代碼片斷:

while(true) 
{ 
    return; 

,這意味着一切的背後該行的代碼的不可達... 首先,去除

return; 

此外海事組織這是一個非常糟糕的做法,宣佈與while(true)無限循環。與break;您將離開過程

break; 
cursor.close(); 

: 刪除過,並嘗試:

while (!cursor.isLast()) 

另一個地方無法訪問的代碼。 cursor.close();永遠不會被達成。

+0

我在while循環結束時給出了返回值。仍然存在問題..如何解決它? – 2013-03-12 11:57:36

+0

然後請更新您的問題... – 2013-03-12 12:06:52