简体   繁体   English

从sqlite检索值到android listview

[英]Retrieve values from sqlite to android listview

     t1=(TextView)findViewById(R.id.s1);
     t2=(TextView)findViewById(R.id.s2);
     t3=(TextView)findViewById(R.id.s3);
     t4=(TextView)findViewById(R.id.s4);

        dbase=openOrCreateDatabase("reg_db", Context.MODE_PRIVATE, null);
        dbase.execSQL("create table if not exists registration(name       varchar(40),age number(5),casestudy varchar(100),gender varchar(10),phonenumber varchar(12),doctorname varchar(50),date DATE,time varchar(5))");


        Cursor c=dbase.query("registration",new String[]{"name","age","casestudy","gender","phonenumber","doctorname","date","time"}, null, null, null, null, null);
        while(c.moveToNext()){
        String  c1=c.getString(0);
        String  c2=c.getString(1);
        String  c3=c.getString(2);
        String  c4=c.getString(3);
        String  c5=c.getString(4);
        String  c6=c.getString(5);
        String  c7=c.getString(6);
        String  c8=c.getString(7);
        t1.setText(c1);
        t2.setText(c5);
        t3.setText(c7);
        t4.setText(c8);

I am able display only one row in my table how to display all rows in a list form... please specify thank you我只能在我的表格中显示一行如何以列表形式显示所有行...请指定谢谢

Use this before while loop在while循环之前使用这个

You need to set curser at First position您需要将光标设置在第一个位置

  if (c!= null) {

        //more to the first row
        c.moveToFirst();



while(c.moveToNext()){
        String  c1=c.getString(0);
        String  c2=c.getString(1);
        String  c3=c.getString(2);
        String  c4=c.getString(3);
        String  c5=c.getString(4);
        String  c6=c.getString(5);
        String  c7=c.getString(6);
        String  c8=c.getString(7);
        t1.setText(c1);
        t2.setText(c5);
        t3.setText(c7);
        t4.setText(c8);
}
}

Use this code :使用此代码:

 if(c!=null)
  {
    if (c.moveToFirst()) {
      do {
        String  c1=c.getString(0);
        String  c2=c.getString(1);
        String  c3=c.getString(2);
        String  c4=c.getString(3);
        String  c5=c.getString(4);
        String  c6=c.getString(5);
        String  c7=c.getString(6);
        String  c8=c.getString(7);
        t1.setText(c1);
        t2.setText(c5);
        t3.setText(c7);
        t4.setText(c8);

       } while (c.moveToNext());
     }
  }
if (c.moveToFirst()){
   do{
      // do what ever you want here
   }while(.moveToNext());
}
c.close();

I think you appear to be trying to populate the ListView, while a Listview will have another layout for each row/entry, this being pointed to by an adapter.我认为您似乎正在尝试填充 ListView,而 ListView 将为每一行/条目提供另一个布局,这是由适配器指向的。

That is, to populate a Listview you will need to have an adapter.也就是说,要填充 Listview,您需要有一个适配器。 The adapter will utilise an XML layout that defines each entry (row), these in addition to the layout containing the ListView and the activity.适配器将使用定义每个条目(行)的 XML 布局,这些布局是包含 ListView 和活动的布局。

Here's an example of a custom cursor adapter :-这是自定义光标适配器的示例:-

package mjt.shopper;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

/**
 * Created by Mike092015 on 6/02/2016.
 */
class AislesCursorAdapter extends CursorAdapter {
    public AislesCursorAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, 0);
    }
    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        View view = super.getView(position, convertview, parent);
        Context context = view.getContext();
        if (position % 2 == 0) {
            view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewroweven));
        } else {
            view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewrowodd));
        }
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView textviewaisleid = (TextView) view.findViewById(R.id.aisle_id_entry);
        TextView textviewaislename = (TextView) view.findViewById(R.id.aisle_name_entry);
        TextView textviewaisleorder = (TextView) view.findViewById(R.id.aisle_order_entry);
        TextView textviewaisleshopref = (TextView) view.findViewById(R.id.aisle_shopref_entry);

        textviewaisleid.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_ID_INDEX));
        textviewaislename.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_NAME_INDEX));
        textviewaisleorder.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_ORDER_INDEX));
        textviewaisleshopref.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_SHOP_INDEX));
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.activity_aisle_list_entry, parent, false);
    }
}

The getView method isn't important and can be ommitted (in the above it alternates the colours of the rows/entries). getView 方法并不重要,可以省略(在上面它交替行/条目的颜色)。

You will need the bindView method, obviously tailored to your needs (first half gets the views that are to be populated, the second half sets the views and you may notice that it looks similar to part of your code).您将需要 bindView 方法,显然是根据您的需要量身定制的(前半部分获取要填充的视图,后半部分设置视图,您可能会注意到它看起来与您的部分代码相似)。

You will also need the newView method which basically sets the layout that is to be used for each row/entry of the ListView (ie the following XML).您还需要 newView 方法,它基本上设置要用于 ListView 的每一行/条目的布局(即以下 XML)。

Note!笔记! ShopperDBHelper.AISLES_COLUMN_???_INDEX contains the index into the cursor ShopperDBHelper.AISLES_COLUMN_???_INDEX 包含进入游标的索引

here's an example XML (the one used by the above adapter) :-这是一个 XML 示例(上述适配器使用的那个):-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/standard_listview_row_padding_vertical"
    android:paddingLeft="@dimen/standard_listview_row_padding_horizontal"
    android:paddingRight="@dimen/standard_listview_row_padding_horizontal"
    android:paddingTop="@dimen/standard_listview_row_padding_vertical"
    tools:context=".AisleListEntryActivity">
    <!-- Aisle ID 4 debugging -->
    <TextView
        android:id="@+id/aisle_id_entry"
        android:layout_width="30sp"
        android:layout_height="@dimen/rowheight"
        android:textSize="@dimen/standard_listview_text_size"
        android:visibility="visible"/>
    <!-- Aisle Name -->
    <TextView
        android:id="@+id/aisle_name_entry"
        android:layout_width="@dimen/ailsename_text_size"
        android:layout_height="@dimen/rowheight"
        android:textSize="@dimen/standard_listview_text_size"
        android:textStyle="bold" />
    <!-- Aisle Order-->
    <TextView
        android:id="@+id/aisle_order_entry"
        android:layout_width="@dimen/ailseorder_text_size"
        android:layout_height="@dimen/rowheight"
        android:textSize="@dimen/standard_listview_text_size"/>
    <!-- Aisle Shopref 4 debugging -->
    <TextView
        android:id="@+id/aisle_shopref_entry"
        android:layout_width="50sp"
        android:layout_height="@dimen/rowheight"
        android:textSize="@dimen/standard_listview_text_size"
        android:visibility="gone"/>
</LinearLayout>

Here's an example of the code that gets the cursor and sets the listview to use the adapter (again as used by the above), this would be in your activity:-这是获取光标并设置列表视图以使用适配器的代码示例(同样如上文所述),这将在您的活动中:-

    Cursor aislescsr = shopperdb.getAislesPerShopAsCursor(csr.getInt(ShopperDBHelper.SHOPS_COLUMNN_ID_INDEX));
    final ListView lv = (ListView) findViewById(R.id.aalbclv01);
    AislesCursorAdapter aisleadapter = new AislesCursorAdapter(lv.getContext(), aislescsr, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    currentaca = aisleadapter;
    lv.setAdapter(aisleadapter);

The first line populates the cursor from the database.第一行从数据库填充游标。 The second line gets the ListView that will be populated with the data from the database.第二行获取将用数据库中的数据填充的 ListView。 The third line sets the adapter's cursor.第三行设置适配器的光标。 The fourth line is not relevant.第四行不相关。 The fifth line ties the adapter to the ListView.第五行将适配器与 ListView 联系起来。

There is a SimpleCursorAdapter but I've never used one, so can't really tell you about it/them.有一个 SimpleCursorAdapter 但我从来没有用过,所以不能真正告诉你它/它们。

Additionally CursorAdapter expects there to a column named _id, so you may have to use AS _id.此外,CursorAdapter 期望有一个名为 _id 的列,因此您可能必须使用 AS _id。

here's an example where I didn't have _id and use AS to set it (see 3rd line) :-这是一个示例,我没有 _id 并使用 AS 来设置它(请参阅第 3 行):-

        SQLiteDatabase db = this.getReadableDatabase();
        String sqlstr = " SELECT " + PRODUCTUSAGE_COLUMN_AISLEREF +
                " AS _id, " +
                PRODUCTUSAGE_COLUMN_PRODUCTREF + ", " +
                PRODUCTUSAGE_COLUMN_COST + ", " +
                PRODUCTUSAGE_COLUMN_BUYCOUNT + ", " +
                PRODUCTUSAGE_COLUMN_FIRSTBUYDATE + ", " +
                PRODUCTUSAGE_COLUMN_LATESTBUYDATE + ", " +
                PRODUCTUSAGE_COLUMN_MINCOST +
                " FROM " + PRODUCTUSAGE_TABLE_NAME +
                " ORDER BY _id;";
        return db.rawQuery(sqlstr, null);

make Method Like this in Database Class数据库类中像这样的方法

    // Getting All User
public List<UserModel> getAllUser() {
    List<UserModel> userList = new ArrayList<UserModel>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_USER;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            UserModel user = new UserModel();
            user.setId(cursor.getString(0));
            user.setName(cursor.getString(1));
            user.setEmail(cursor.getString(2));
            // Adding user to list
            userList.add(user);
        } while (cursor.moveToNext());
    }

    // return user list
    return userList;
}

So you call this method and you will get List type of model class所以你调用这个方法,你会得到模型类的 List 类型

after that you just have to pass in adapter and implement it之后你只需要传入适配器并实现它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM