如何显示在android的sqlite的数据填充列表视图 [英] How to display list view populated from sqlite data in android

查看:248
本文介绍了如何显示在android的sqlite的数据填充列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建时由被点击一个按钮列表视图与由SQLite中的数据填充列表视图的android应用。我做了一些事情下面我没有得到任何错误,但它并没有表现出任何检索data.Please帮助我
这是我的活动,当一个按钮点击:

  fetch_database.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){                progressGenerator.start(fetch_database);
                media_player = media_player.create(DatabaseListView.this,R.raw.retrievingfromdatabase);
                media_player.start();
                的String [] =从{logindatabase_adapter.USER_NAME,logindatabase_adapter.USER_PASSWORD};
                INT []为= {R.id.txt_username,R.id.txt_pasword};
                光标= logindatabase_adapter.feching_Data();
                CursorAdapter的=新SimpleCursorAdapter(DatabaseListView.this,R.layout.listcell,光标,从,到);
                database_results.setAdapter(CursorAdapter的);
            }
        });

这是我的logindatabase适配器:

 包com.developer.milanandroid;进口android.content.ContentValues​​;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.view.View;公共类LoginDataBaseAdapter
{
        //数据库名称
        静态最后弦乐DATABASE_NAME =MilanloginRegistration.db;
        静态最终诠释DATABASE_VERSION = 1;
        公共静态最终诠释NAME_COLUMN = 1;
        // TODO:在您的表中每一列创建的公共领域。
        // SQL语句创建一个新的数据库。
        公共静态最后弦乐TABLE_NAME =MilanLoginregistration;
        公共静态最后弦乐ID =_ ID;
        公共静态最后弦乐USER_NAME =USERNAME;
        公共静态最后弦乐USER_PASSWORD =密码;
        静态最后弦乐DATABASE_CREATE =创建表+ TABLE_NAME +
                                     (+ ID +整数主键自动增量+USERNAME文本独一无二的,+ USER_PASSWORD +文本);;
        //变量来存放数据库实例
        公共SQLiteDatabase分贝;
        //使用该数据库的应用程序的语境。
        私人最终上下文的背景下;
        //数据库打开/升级助手
        私人DataBaseHelper dbHelper;
        公共LoginDataBaseAdapter(上下文_context)
        {
            上下文= _context;
            dbHelper =新DataBaseHelper(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }
        公共LoginDataBaseAdapter的open()抛出的SQLException
        {
            DB = dbHelper.getWritableDatabase();
            返回此;
        }
        公共无效的close()
        {
            db.close();
        }        公共SQLiteDatabase getDatabaseInstance()
        {
            返回分贝;
        }        公共无效insertEntry(用户名字符串,字符串密码)
        {
           ContentValues​​ newValues​​ =新ContentValues​​();            newValues​​.put(username的用户名);
            newValues​​.put(密码,密码);            //将该行插入你的表
            db.insert(MilanLoginregistration,空,newValues​​);
            ///Toast.makeText(context,提醒保存成功,Toast.LENGTH_LONG).show();
        }
        公众诠释deleteEntry(用户名字符串,字符串密码)
        {
            //字符串ID =将String.valueOf(ID);
            字符串,其中=USERNAME =?;
            INT numberOFEntriesDeleted = db.delete(MilanLoginregistration,其中,新的String [] {用户名,密码});
           // Toast.makeText(背景下,数字FO项中删除成功:+ numberOFEntriesDeleted,Toast.LENGTH_LONG).show();
            返回numberOFEntriesDeleted;
        }        公共光标feching_Data(){            的String [] =列{ID,USER_NAME,USER_PASSWORD};
            DB = dbHelper.getWritableDatabase();
            光标光标= db.query(TABLE_NAME,列,NULL,NULL,NULL,NULL,NULL);
            返回游标;        }
        公共字符串getSinlgeEntry(用户名字符串)
        {
            光标光标= db.query(MilanLoginregistration,NULL,USERNAME =?,新的String [] {用户名},NULL,NULL,NULL);
            如果(cursor.getCount()&。1)//用户名不存在
            {
                cursor.close();
                返回不存在;
            }
            cursor.moveToFirst();
            字符串密码= cursor.getString(cursor.getColumnIndex(密码));
            cursor.close();
            返回密码;
        }
        公共字符串checkSinlgeEntry(用户名字符串)
        {
            光标光标= db.query(MilanLoginregistration,NULL,USERNAME =?,新的String [] {用户名},NULL,NULL,NULL);
            如果(cursor.getCount()> = 1)//用户名存在
            {
                cursor.close();
                返回不存在;
            }
            cursor.close();
            返回;
        }
        公共无效updateEntry(USER_NAME字符串,字符串pasword)
        {
            //定义更新的行的内容。
            ContentValues​​ updatedValues​​ =新ContentValues​​();
            //指定值的每一行。
            updatedValues​​.put(username的,USER_NAME);
            updatedValues​​.put(密码,pasword);
            字符串,其中=USERNAME =?;
            db.update(MilanLoginregistration,updatedValues​​,在那里,新的String [] {USER_NAME});
        }
        / *公共无效显示(视图v){
            光标C = db.rawQuery(SELECT * FROM MilanloginRegistration,NULL);
            admin_settings_child.text_fetched_database_results.setText();
            c.moveToFirst();
            做{
                字符串username = c.getString(c.getColumnIndex(USERNAME));
                字符串密码= c.getString(1);
                admin_settings_child.text_fetched_database_results.append(\"USERNAME::-->\"+username+\"PASSWORD::-->\"+password+\"\
\");            }而(c.moveToNext());
        } * /
    }


解决方案
而不是重新发明轮子,我会建议

使用更强大的解决方案与所有建议的积木(光标适配器,内容提供商,数据装载机,... 的),以后它会为你节省很多的悲伤。它看起来有点开头曲,但它是值得推行。这种方法已被广泛宣传别处(拉尔斯·沃格尔,的钨Rittmeyer ,的 Udacity类(第4课) ...)

我把这样一个解决方案在 GitHub上,然后你可以自由地进行调查(附APK将展示它的行为的)。作为额外的奖励,也有一个人像/风景用不同的手机/平板电脑的布局处理。

好运

I am creating an android application that consists of a list view when a button was clicked list view was populated with the data consisting in sqlite. i did some thing below i am not getting any error but it was not showing any retrieved data.Please help me with this This is my activity when a button clicked:

fetch_database.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                progressGenerator.start(fetch_database);
                media_player = media_player.create(DatabaseListView.this, R.raw.retrievingfromdatabase);
                media_player.start();


                String[] from = {logindatabase_adapter.USER_NAME,logindatabase_adapter.USER_PASSWORD};
                int[] to = {R.id.txt_username,R.id.txt_pasword};
                cursor = logindatabase_adapter.feching_Data();
                cursoradapter = new SimpleCursorAdapter(DatabaseListView.this, R.layout.listcell, cursor, from, to);
                database_results.setAdapter(cursoradapter);
            }
        });

This is my logindatabase adapter:

package com.developer.milanandroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

public class LoginDataBaseAdapter 
{
        //Database name
        static final String DATABASE_NAME = "MilanloginRegistration.db";
        static final int DATABASE_VERSION = 1;
        public static final int NAME_COLUMN = 1;
        // TODO: Create public field for each column in your table.
        // SQL Statement to create a new database.
        public static final String TABLE_NAME="MilanLoginregistration";
        public static final String ID="_id";
        public static final String USER_NAME="USERNAME";
        public static final String USER_PASSWORD ="PASSWORD";


        static final String DATABASE_CREATE = "create table "+ TABLE_NAME +
                                     "( " +ID+" integer primary key autoincrement,"+"USERNAME text UNIQUE,"+USER_PASSWORD+" text); ";
        // Variable to hold the database instance
        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;
        // Database open/upgrade helper
        private DataBaseHelper dbHelper;
        public  LoginDataBaseAdapter(Context _context) 
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public  LoginDataBaseAdapter open() throws SQLException 
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }



        public void insertEntry(String username,String password)
        {
           ContentValues newValues = new ContentValues();

            newValues.put("USERNAME",username);
            newValues.put("PASSWORD",password);

            // Insert the row into your table
            db.insert("MilanLoginregistration",null,newValues);
            ///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }
        public int deleteEntry(String username,String password)
        {
            //String id=String.valueOf(ID);
            String where="USERNAME=?";
            int numberOFEntriesDeleted= db.delete("MilanLoginregistration", where, new String[]{username,password}) ;
           // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
            return numberOFEntriesDeleted;
        }

        public Cursor feching_Data(){

            String[] columns = {ID,USER_NAME,USER_PASSWORD};
            db = dbHelper.getWritableDatabase();
            Cursor cursor = db.query(TABLE_NAME, columns,null,null,null,null,null);
            return cursor;

        }
        public String getSinlgeEntry(String userName)
        {
            Cursor cursor=db.query("MilanLoginregistration", null, " USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
            cursor.close();
            return password;                
        }
        public String checkSinlgeEntry(String userName)
        {
            Cursor cursor=db.query("MilanLoginregistration", null, " USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()>=1) // UserName  Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.close();
            return "";              
        }
        public void  updateEntry(String user_name,String pasword)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.  
            updatedValues.put("USERNAME", user_name);
            updatedValues.put("PASSWORD",pasword);


            String where="USERNAME = ?";
            db.update("MilanLoginregistration",updatedValues, where, new String[]{user_name});             
        }   
        /*public void Display(View v){
            Cursor c = db.rawQuery("select * from MilanloginRegistration", null);
            admin_settings_child.text_fetched_database_results.setText("");
            c.moveToFirst();
            do{
                String username = c.getString(c.getColumnIndex("USERNAME"));
                String password = c.getString(1);
                admin_settings_child.text_fetched_database_results.append("USERNAME::-->"+username+"PASSWORD::-->"+password+"\n");

            }while(c.moveToNext());
        }*/
    }

解决方案

Instead of re-inventing the wheel, I would recommend to use more robust solution with all the recommended building blocks (cursor adapter, content provider, data loader, ...) It will save you a lot of grief later. It looks a bit convoluted at the beginning, but it is worth implementing. This approach has been widely publicized elsewhere (Lars Vogel, Wolfram Rittmeyer, Udacity Class (Lesson 4) ...)

I put one such solution on GitHub and you are free to investigate it (the attached APK will show how it behaves). As an added bonus, there is also a PORTRAIT / LANDSCAPE handling with different phone/ tablet layouts.

Good Luck

这篇关于如何显示在android的sqlite的数据填充列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆