从数据库中检索数据并在自定义列表视图中设置

时间:2013-11-27 18:21:38

标签: android sqlite android-listview

从数据库中检索数据并在自定义列表视图中设置,但仅在列表视图中显示DB中的最后一个数据。

这是我的代码请解决我的问题。

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

公共类CustListview扩展了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;
 }}

2 个答案:

答案 0 :(得分:0)

尝试类似的东西

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

答案 1 :(得分:0)

您是否检查了从数据库中获取的姓名和号码。 您的代码只在数组中添加一行,您必须在while循环中移动。

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"));
            num1=c.getString(c.getColumnIndex("num"));
            Log.d("string is",name1+num1);
           valnumber.add(name1+"<<"+num1+">>");

        }
        while (c.moveToNext());
    }
    custlistAdapter = new CustListview(Contact.this, R.layout.custlistview,valnumber);
    ShowCont.setAdapter(custlistAdapter);
    myDb.close();