开发者

how to show the data in list using cursor in android

开发者 https://www.devze.com 2023-03-10 13:27 出处:网络
I am getting the from the database using cursor but when i am trying to show the data in list, it is not showing..

I am getting the from the database using cursor but when i am trying to show the data in list, it is not showing..

the below code is for accessing the data using cursor from database:

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

now the below code is for adding the data 开发者_开发百科in the list---

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


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


You are iterating through the Cursor and setting the same variable over and over with a different value, until it finally reaches the end of the cursor.

Instead, you need to read it once per call in the getView() method.

look into this part:

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

You are not using i for anything here. Rather, you are replacing the values of m_uname and m_mobnum constantly. A simple way to make it to work is to create a couple of ArrayList and do

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

You would then use it as

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

精彩评论

暂无评论...
验证码 换一张
取 消