2011-06-06 49 views
1

我正在從數據庫中使用遊標,但是當我試圖在列表中顯示的數據,它沒有顯示使用光標在列表中的數據..如何顯示在Android的

下面的代碼是訪問使用光標從數據庫中的數據:

protected void showCustomerdetails() { 

    try 
    { 
     Cursor cursor = m_dbManager.getValues("customer", new String[]{"name","mobile_phone" }, null, null, null, null); 
     if(cursor != null) 
     { 
      cursor.moveToFirst(); 
      int i = 0; 
      while(cursor.isAfterLast() == false) 
      { 
       m_uname = cursor.getString(cursor.getColumnIndex("name")); 
       Log.d("debug","getting the name from cursor"); 
       m_mobnum = cursor.getString(cursor.getColumnIndex("mobile_phone")); 
       cursor.moveToNext(); 
       i++; 
      } 
     } 
     cursor.close(); 
    } 
    catch(SQLException e) 
    { 
     e.printStackTrace(); 
    } 
} 

現在下面的代碼是在列表中添加的數據---你是通過迭代光標

class IconicAdapter extends ArrayAdapter<String> 
{ 
Activity context; 
IconicAdapter(Activity context) 
{ 
    super(context, R.layout.custoemrs_list); 
    this.context=context; 
} 

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    View row = null; 
    try 
    { 
      LayoutInflater inflater=context.getLayoutInflater(); 
      row=inflater.inflate(R.layout.custoemrs_list, null); 
      TextView des=(TextView)row.findViewById(R.id.custusername); 
      des.setText(m_uname); 

      TextView qty=(TextView)row.findViewById(R.id.custmobnum); 
      qty.setText(m_mobnum); 



    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return row; 
} 
} 

回答

0

並通過設置相同的變量和Ø ver使用不同的值,直到它最終到達光標的末尾。

相反,您需要在getView()方法中每次調用時讀取一次。

看看這一部分:

while(cursor.isAfterLast() == false) 
{ 
m_uname = cursor.getString(cursor.getColumnIndex("name")); // <-- BAD! 
Log.d("debug","getting the name from cursor"); 
m_mobnum = cursor.getString(cursor.getColumnIndex("mobile_phone")); // <-- BAD! 
cursor.moveToNext(); 
i++; // <-- UNUSED! 
} 

您沒有使用i任何東西在這裏。相反,您正在不斷地取代m_unamem_mobnum的值。一個簡單的方法,它的工作是建立一個連接的ArrayList和做

ArrayList<String> m_uname = new ArrayList<String>(); 
// 

while(cursor.isAfterLast() == false) 
{ 
m_uname.add(cursor.getString(cursor.getColumnIndex("name"))); 
Log.d("debug","getting the name from cursor"); 
m_mobnum.add(cursor.getString(cursor.getColumnIndex("mobile_phone"))); 
cursor.moveToNext(); 
} 

你可以這樣使用它作爲

des.setText(m_uname.get(position)); 
+0

如何?你能解釋嗎? – 2011-06-06 15:19:23

+0

查看上面的更新回答 – Aleadam 2011-06-06 15:29:16

+0

感謝您的回覆。但仍然沒有顯示... – 2011-06-07 04:55:29

0
Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cursor))