2015-05-05 170 views
0

我正在創建一個android應用程序,其中包含一個列表視圖,當一個按鈕被點擊時列表視圖被包含在sqlite中的數據填充。我做了一些事情下面我沒有得到任何錯誤,但它並沒有表現出任何檢索data.Please幫我這個 這是我的活動當按鈕點擊:如何顯示列表視圖填充從android中的sqlite數據填充

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

這是我logindatabase適配器:

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()); 
     }*/ 
    } 
+0

你有feching_Data的設置是在存在的情況下,沒有數據或者空?它會在之後移動到第一個入口嗎?看起來沒有,你應該確保你實現這些安全衛士。 – Kat

回答

1

而不是重新發明輪子,我會建議使用與所有建議的積木更強大的解決方案(光標適配器,內容提供商,數據加載,...),它會爲你節省很多的後來的悲傷。一開始看起來有些複雜,但值得一試。這種方法已經被廣泛宣傳的其他地方(Lars VogelWolfram RittmeyerUdacity Class (Lesson 4) ...)

我把這樣一個解決方案上GitHub,你可以自由進行調查(附加APK將展示它的行爲)。作爲額外的好處,還有一個人像/風景處理不同的手機/平板電腦佈局。

好運