我使用光标从数据库中获取但是当我试图在列表中显示数据时,它没有显示..
以下代码用于使用数据库中的游标访问数据:
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 :(得分:1)
Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cursor))
答案 1 :(得分:0)
您正在遍历Cursor并使用不同的值反复设置相同的变量,直到它最终到达游标的末尾。
相反,您需要在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_uname
和m_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));