2015-09-28 76 views
0

我不擅長編程,我需要你的幫助。我只能在textview中顯示數據。它只顯示數據庫SQLite中的最後一個數據。在Listview中顯示來自SQLite的數據

這是我databasehelper

public class DatabaseHelper extends SQLiteOpenHelper { 
private static String DB_PATH = "/data/data/com.example.jathniel.myapplication/databases/"; 
private static String DB_NAME = "example.sqlite"; 
private SQLiteDatabase myDataBase; 
private Context myContext = null; 

public DatabaseHelper(Context context) { 
    super(context, DB_NAME, (SQLiteDatabase.CursorFactory) null, 1); 
    this.myContext = context; 
} 

public void createDataBase() throws IOException { 
    boolean dbExist = this.checkDataBase(); 
    if(!dbExist) { 
     this.getReadableDatabase(); 

     try { 
      this.copyDataBase(); 
     } catch (IOException var3) { 
      throw new Error("Error"); 
     } 
    } 

} 

public void copyDataBase() throws IOException { 
    InputStream myInput = this.myContext.getAssets().open(DB_NAME); 
    String outFileName = DB_PATH + DB_NAME; 
    FileOutputStream myOutput = new FileOutputStream(outFileName); 
    byte[] buffer = new byte[1024]; 

    int length; 
    while((length = myInput.read(buffer)) > 0) { 
     myOutput.write(buffer, 0, length); 
    } 

    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 
} 

public boolean checkDataBase() { 
    SQLiteDatabase checkDB = null; 

    try { 
     String e = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(e, (SQLiteDatabase.CursorFactory)null, 0); 
    } catch (SQLiteException var3) { 
     ; 
    } 

    if(checkDB != null) { 
     checkDB.close(); 
    } 

    return checkDB != null; 
} 

public void openDataBase() throws SQLException { 
    String myPath = DB_PATH + DB_NAME; 
    this.myDataBase = SQLiteDatabase.openDatabase(myPath, (SQLiteDatabase.CursorFactory)null, 0); 
} 

public synchronized void close() { 
    if(this.myDataBase != null) { 
     this.myDataBase.close(); 
    } 

    super.close(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 


public void createDatabase() { 
} 

這是dataDB

public class DataDB { 
DatabaseHelper con; 
String name,lastname; 

public DataDB() { 
} 

public String getNameDB(Context context) throws SQLException { 
    this.con = new DatabaseHelper(context); 

    try { 
     this.con.createDataBase(); 
    } catch (IOException var4) { 
     ; 
    } 

    if (!this.con.checkDataBase()) { 
     return "ERROR"; 
    } else { 
     this.con.openDataBase(); 
     SQLiteDatabase db = this.con.getWritableDatabase(); 

     for (Cursor cursor = db.rawQuery("SELECT name FROM exampleTable", (String[]) null); cursor.moveToNext(); this.name = cursor.getString(0)) { 

     } 

     this.con.close(); 
     return this.name; 


    } 

} 
public String getLNameDB(Context context) throws SQLException { 
    this.con = new DatabaseHelper(context); 

    try { 
     this.con.createDataBase(); 
    } catch (IOException var4) { 
     ; 
    } 

    if(!this.con.checkDataBase()) { 
     return "ERROR"; 
    } else { 
     this.con.openDataBase(); 
     SQLiteDatabase db = this.con.getWritableDatabase(); 

     for (Cursor cursor = db.rawQuery("SELECT lastname FROM exampleTable", (String[]) null); cursor.moveToNext(); this.lastname = cursor.getString(0)) { 

     } 


     this.con.close(); 
     return this.lastname; 
    } 
    } 

我有一個主要活動

public class MainActivity extends Activity { 
DataDB data = new DataDB(); 
ListView list; 
ArrayAdapter<String> listAdapter; 

public MainActivity() { 
} 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    list = (ListView) findViewById(R.id.listView); 


    ArrayList<String> name = new ArrayList<String>(); 
    listAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, name); 



    try { 
      listAdapter.add(data.getNameDB(this)); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
    } 

} 
+0

使用'SimpleCursorAdapter' – pskink

回答

0

我想你應該改變

this.name = cursor.getString(0) 

this.name += cursor.getString(0) 

如果你想顯示在ListView的數據庫中的所有名稱,請按照this tutorial。 如您在本教程中看到的,你需要創建一個數組名這樣

ArrayList<String> name = new ArrayList(); 

代替String name;

對於將數據添加到數組列表:

for (Cursor cursor = db.rawQuery("SELECT lastname FROM exampleTable", (String[]) null); cursor.moveToNext(); this.name.add(cursor.getString(0))) { 

希望這有助於

+0

如何通過列表視圖顯示它?當我嘗試將textview轉換爲列表視圖時出現錯誤 –

+0

我沒有看到你的代碼中的列表視圖,只是textview –

+0

是的,但首先我使用listview和我得到錯誤,所以我只用textview –

相關問題