Retrieve data from the database and set in the custom list

advertisements

Retrieve Data from the database and set in custom listview but only show the last data from DB in list view.

this is my code please solve my problem.

SQLiteDatabase myDb = openOrCreateDatabase(DB_Name,Context.MODE_PRIVATE, null);
try {
    c=myDb.rawQuery("select sid,name,num from Contact", null);
    if (c.moveToFirst()) {
        do {
            name1=c.getString(c.getColumnIndex("name")).toString();
            num1=c.getString(c.getColumnIndex("num")).toString();
            Log.d("string is",name1+num1);
        }
        while (c.moveToNext());
    }
    valnumber.add(name1+"<<"+num1+">>");
    custlistAdapter = new CustListview(Contact.this, R.layout.custlistview,valnumber);
    ShowCont.setAdapter(custlistAdapter);
    myDb.close();

CustListview.java

public class CustListview extends ArrayAdapter {

private final Activity context;
private final ArrayList<String> web;
 int layoutResourceId;
public CustListview(Activity context,int custlistview, List<String> val)
{
super(context, R.layout.custlistview, val);
this.context = context;
this.web = (ArrayList<String>) val;
}
@Override
public View getView(int position, View Convertview, ViewGroup parent) {
View row=Convertview;
    UserHolder holder = null;

    if(row==null)
    {
        LayoutInflater inflater=((Activity)context).getLayoutInflater();
        row=inflater.inflate(R.layout.custlistview, null, true);
        holder=new UserHolder();
        holder.imgdelete=(Button)row.findViewById(R.id.imagedelete);
        holder.txtTitle = (TextView) row.findViewById(R.id.txt);
        //LayoutInflater inflater = context.getLayoutInflater();
        //View rowView= inflater.inflate(R.layout.custlistview, null, true);
        row.setTag(holder);
    }
    else
        {
    holder=(UserHolder)row.getTag();
        }
    holder.txtTitle.setText(web.toString());
    holder.imgdelete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(getContext(), "delete", Toast.LENGTH_LONG).show();
}});
return row;
}
static class UserHolder {
     TextView txtTitle;
     Button imgdelete;
 }}


Try somthing like that

cursor = sqldb.query("Contact", null, null, null, null, null, "sid");
if (cursor != null && cursor.getCount() > 0) {
    if (cursor.moveToFirst()) {
          do {
            name1=cursor.getString(cursor.getColumnIndex("name"));
                    num1=cursor.getString(cursor.getColumnIndex("num"));
                    Log.d("string is",name1+num1);
          } while (cursor.moveToNext());
    }
}
cursor.close();