2013-03-12 38 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