简体   繁体   中英

How to List SQlite database in a listview

I have implemented this code into my app to allow the saving of a name and then displaying that name in a ListView . I think I have added the names to the database but it displays a blank ListView .

SQLDbHelper :

public class SqlDbHelper extends SQLiteOpenHelper
{
    public static final String DATABASE_TABLE = "PHONE_CONTACTS";


    public static final String COLUMN1 = "name";

    private static final String SCRIPT_CREATE_DATABASE = "create table "
            + DATABASE_TABLE + " (" + COLUMN1
            + " text not null);";

    public SqlDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                       int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(SCRIPT_CREATE_DATABASE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

SqlHandler :

public class SqlHandler
{
    public static final String DATABASE_NAME = "MY_DATABASE";
    public static final int DATABASE_VERSION = 1;
    Context context;
    SQLiteDatabase sqlDatabase;
    SqlDbHelper dbHelper;

    public SqlHandler(Context context) {

        dbHelper = new SqlDbHelper(context, DATABASE_NAME, null,
                DATABASE_VERSION);
        sqlDatabase = dbHelper.getWritableDatabase();
    }

    public void executeQuery(String query) {
        try {

            if (sqlDatabase.isOpen()) {
                sqlDatabase.close();
            }

            sqlDatabase = dbHelper.getWritableDatabase();
            sqlDatabase.execSQL(query);

        } catch (Exception e) {

            System.out.println("DATABASE ERROR " + e);
        }

    }

    public Cursor selectQuery(String query) {
        Cursor c1 = null;
        try {

            if (sqlDatabase.isOpen()) {
                sqlDatabase.close();

            }
            sqlDatabase = dbHelper.getWritableDatabase();
            c1 = sqlDatabase.rawQuery(query, null);

        } catch (Exception e) {

            System.out.println("DATABASE ERROR " + e);

        }
        return c1;

    }

}

List Adapter:

public class PlantListAdapter extends BaseAdapter
{

    Context context;
    ArrayList<PlantListitems> contactList;

    public PlantListAdapter(Context context, ArrayList<PlantListitems> list) {

        this.context = context;
        contactList = list;
    }

    @Override
    public int getCount() {

        return contactList.size();
    }

    @Override
    public Object getItem(int position) {

        return contactList.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        PlantListitems contactListItems = contactList.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.plant_list_row, null);

        }

        TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
        tvName.setText(contactListItems.getName());


        return convertView;
    }

}

ListView activity to display database:

SqlHandler sqlHandler;
//private ArrayList<String> addArray;
//public static final String PREFS = "examplePrefs";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mygarden_list);
    listView = (ListView) findViewById(R.id.mygardenlist);

  sqlHandler = new SqlHandler(this);

    //addArray = new ArrayList<>();

    ArrayList<PlantListitems> contactList = new ArrayList<>();
    //contactList.clear();
    String query = "SELECT * FROM PHONE_CONTACTS ";
    Cursor c1 = sqlHandler.selectQuery(query);
    if (c1 != null && c1.getCount() != 0) {
        if (c1.moveToFirst()) {
            do {
                PlantListitems plantListItems = new PlantListitems();


                plantListItems.setName(c1.getString(c1
                        .getColumnIndex("name")));


            } while (c1.moveToNext());
        }
    }
    c1.close();

    PlantListAdapter plantListAdapter = new PlantListAdapter(mygardenMain.this, contactList);
    listView.setAdapter(plantListAdapter);

}

contactList is empty.

do {
            PlantListitems plantListItems = new PlantListitems();


            plantListItems.setName(c1.getString(c1
                    .getColumnIndex("name")));

            //Add the plantListItem object to the ContactList arrayList.
            contactList.add(plantList);




        } while (c1.moveToNext());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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