2016-09-23 9 views
-4

sqliteからカスタムListView(java、Androidスタジオ、sqlite)にデータを表示したい。 しかしそれは強制停止です。私の主な活動からデータをSQLiteからアンドロイドスタジオのCustom ListViewに表示

画像:私のDatabaseクラスから first image http://s9.picofile.com/file/8268384250/1.PNG

写真: second image http://s8.picofile.com/file/8268384268/2.PNG

私を助けてください!!!!

+1

画像は添付されていません。 –

+1

例外スタックトレースを追加 – AmiNadimi

+1

コードの画像は含めないでください。それをコピーして質問に貼り付けます。 – buczek

答えて

0

は、まず、あなたのリストビュー用のカスタムアダプタを作成し、リストビューでそのアダプタを移入し、あなたのコードは、この ようにする必要がありますし、データを取得するためにしなければならない(私の英語は申し訳ありませんよくないですが、私はあなたの言葉を理解することができます)データベースこれはあなたのボタンになり、データをretriveそのボタンをクリックし、カーソル

public class UserDbHelper extends SQLiteOpenHelper { 
public static final String DATABASENAME = "DATABASENAME2"; 
public static final int DB_VERSION = 3; 
public static final String TABLE_NAME = "student"; 
public static final String DEVICE = "device"; //your coloumn 
public static final String ADDRESS = "address";//your coloumn 
public static final String Date = "Date"; //your coloumn 
public String CREATE_QUERY = "CREATE TABLE "+TABLE_NAME+"("+DEVICE+" TEXT,"+ADDRESS+" TEXT,"+Date+"TEXT);"; 


public UserDbHelper(Context context) 
{ 
    super(context,DATABASENAME,null,DB_VERSION); 


} 
@Override 
public void onCreate(SQLiteDatabase sqLiteDatabase) { 
    sqLiteDatabase.execSQL(CREATE_QUERY); 

} 

public List<Datalist> getdata() 
{ 
List<Datalist> result = new ArrayList<>(); 
Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM Table_name,null";); 
while (c.moveToNext()) { 
result.add(
       new Datalist(
         c.getString(c.getColumnIndex("YOUR COLOUMN")), 
         c.getString(c.getColumnIndex("YOUR COLOUMN")), 
         date 

       ) 
     ); 
    } 
    c.close(); 
    return result; 
} 

の形でそれを取ると、リストビュー

を使用してデータベースを作成)(データベースクラス名函内のメソッドを作成

List<Datalist> itemFromDatabase = getItemFromDatabase(); 
MyAdapter adapter = new MyAdapter(getApplicationContext(), 0, itemFromDatabase); 
listOfDatabaseObject.setAdapter(adapter); 
adapter.notifyDataSetChanged(); 
+0

ご回答いただきありがとうございますが、DataListは何ですか?クラス?邪悪なクラス? – REZA

+0

datalistは、カスタムアダプタにデータを提供するデータプロバイダです。 – Badprince

0

MainActivity.java

パブリッククラスMainActivityは{

DataBaseHelper myDbHelper; 
List<String> stringList; 
ListView listView; 
List<Mivejat> mivejatList; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myDbHelper = new DataBaseHelper(this); 

    stringList = new ArrayList<>(); 

    try { 

     myDbHelper.createDataBase(); 

    } catch (IOException ioe) { 

     throw new Error("Unable to create database"); 

    } 

    try { 

     myDbHelper.openDataBase(); 

    }catch(SQLException sqle){ 

     throw sqle; 

    } 

    myDbHelper = new DataBaseHelper(this); 
    mivejatList = myDbHelper.MiveName(); 

    for (int i = 0 ; 0 < mivejatList.size() ; i++) { 
     stringList.add(mivejatList.get(i).name); 
    } 
    listView = (ListView) findViewById(R.id.listViewMive); 

    mainListViewClass mainListViewClass = new mainListViewClass(MainActivity.this , stringList); 
    listView.setAdapter(mainListViewClass); 
    mainListViewClass.notifyDataSetChanged(); 

    myDbHelper.close(); 
} 

}

MainActivity.java

public class MainActivity extends AppCompatActivity { 

DataBaseHelper myDbHelper; 
List<String> stringList; 
ListView listView; 
List<Mivejat> mivejatList; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myDbHelper = new DataBaseHelper(this); 

    stringList = new ArrayList<>(); 

    try { 

     myDbHelper.createDataBase(); 

    } catch (IOException ioe) { 

     throw new Error("Unable to create database"); 

    } 

    try { 

     myDbHelper.openDataBase(); 

    }catch(SQLException sqle){ 

     throw sqle; 

    } 

    myDbHelper = new DataBaseHelper(this); 
    mivejatList = myDbHelper.MiveName(); 

    for (int i = 0 ; 0 < mivejatList.size() ; i++) { 
     stringList.add(mivejatList.get(i).name); 
    } 
    listView = (ListView) findViewById(R.id.listViewMive); 

    mainListViewClass mainListViewClass = new mainListViewClass(MainActivity.this , stringList); 
    listView.setAdapter(mainListViewClass); 
    mainListViewClass.notifyDataSetChanged(); 

    myDbHelper.close(); 
} 

}

AppCompatActivityを拡張します

DataBaseHelper.java

public class DataBaseHelper extends SQLiteOpenHelper { 

private static String DB_PATH = "/data/data/mivejat.rezaahmadpour.ir.mivejat/databases/"; 
private static String DB_NAME = "KhavasMiveha.db"; 
public static final int DB_VERS = 1; 
private SQLiteDatabase myDataBase; 
private final Context myContext; 
private static String TAG = "KhavasMiveha.db"; 

/** 
* Constructor Takes and keeps a reference of the passed context in order to 
* access to the application assets and resources. 
* 
* @param context 
*/ 
public DataBaseHelper(Context context) { 
    super(context, DB_NAME, null, DB_VERS); 
    this.myContext = context; 
} 

/** 
* Creates an empty database on the system and rewrites it with your own 
* database. 
*/ 
public boolean createDataBase() throws IOException { 
    boolean dbExist = checkDataBase(); 

    if (dbExist) { 
     Log.d(TAG, "database already exist"); 

     //Running this method causes SQLite class checking for if New DB Version Exists and runs upgrade method 
     getReadableDatabase(); 
     close(); 

     return true; 
    } else { 
     // By calling this method and empty database will be created into 
     // the default system path 
     // of your application so we are gonna be able to overwrite that 
     // database with our database. 
     this.getReadableDatabase(); 
     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
     return false; 
    } 
} 

/** 
* Check if the database already exist to avoid re-copying the file each 
* time you open the application. 
* 
* @return true if it exists, false if it doesn't 
*/ 
private boolean checkDataBase() { 
    SQLiteDatabase checkDB = null; 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } catch (SQLiteException e) { 

     // database does't exist yet. 
    } 

    if (checkDB != null) { 

     checkDB.close(); 
    } 
    return checkDB != null ? true : false; 
} 

/** 
* Copies your database from your local assets-folder to the just created 
* empty database in the system folder, from where it can be accessed and 
* handled. This is done by transferring bytestream. 
*/ 
private void copyDataBase() throws IOException { 
    // Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(DB_NAME); 
    // Path to the just created empty db 
    String outFileName = DB_PATH + DB_NAME; 
    // Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 
    // transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer)) > 0) { 
     myOutput.write(buffer, 0, length); 
    } 

    // Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 
} 

public void openDataBase() throws SQLException { 
    // Open the database 
    String myPath = DB_PATH + DB_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
      SQLiteDatabase.OPEN_READONLY); 
} 

@Override 
public synchronized void close() { 
    if (myDataBase != null) 
     myDataBase.close(); 
    super.close(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.d(TAG, "UPGRADING Database..."); 
    try { 
     copyDataBase(); 
    } catch (IOException e) { 
     throw new Error("Error copying database"); 
    } 

} 

public List<Mivejat> MiveName() { 
    String query = "SELECT Name FROM tbl_mive"; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(query, null); 
    List<Mivejat> result = new ArrayList<>(); 
    Mivejat m; 
    while (cursor.moveToNext()) { 
     m = new Mivejat(); 
     m.name = cursor.getString(0); 
     result.add(m); 
    } 
    return result; 
} 
関連する問題