DBファイルに2列と3行を作成しました。次に、そのデータを 内に表示する必要がありますGridView
。コードにエラーはありませんが、シミュレーターでエラーが発生します"Force close"
。以下はコードです:
public class Grid_testActivity extends Activity {
// We need some kind of Adapter to made the connection between ListView UI component and SQLite data set.
//private ArrayAdapter<String> uGraduateListAdapter;
// We need this while we read the query using Cursor and pass data
//private ArrayList<UndergraduateDetailsPojo> pojoArrayList;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize UI components
GridView grid = (GridView) findViewById(R.id.grid);
TextView txt= (TextView) findViewById(R.id.textView1);
dbHelper db = new dbHelper(this);
Log.d("read data","reading");
List<UndergraduateDetailsPojo> c=db.getAllContacts();
grid.setAdapter((ListAdapter) txt);
}}
DBHelper クラス:
public class dbHelper extends SQLiteOpenHelper {
static String DB_PATH = "/data/data/com.apcog/databases/";
//database name
static String DB_NAME = "SAMPLE.sqlite";
//define table name
private static final String DB_TABLE="TEMP";
//define table entries
//private static final String ID="ID";
//private static final String NAME="NAME";
public dbHelper(Context context)
{
super(context, DB_NAME, null, 1);
}
public dbHelper(Context context, boolean copyDatabase)
{
this(context);
}
private boolean checkDataBaseExistence()
{
// database to be verified
SQLiteDatabase dbToBeVerified = null;
try
{
// get database path
String dbPath = DB_PATH + DB_NAME;
// try to open the database
dbToBeVerified = SQLiteDatabase.openDatabase(dbPath, null,
SQLiteDatabase.OPEN_READONLY);
}
catch (SQLiteException e)
{
// do nothing since the database does not exist
}
// in case it exists, close it
if (dbToBeVerified != null)
{
// close DB
dbToBeVerified.close();
}
// in case there is a DB entity, the DB exists
return dbToBeVerified != null ? true : false;
}
public List<UndergraduateDetailsPojo> getAllContacts() {
List<UndergraduateDetailsPojo> contactList = new ArrayList<UndergraduateDetailsPojo>();
// Select All Query
String selectQuery = "SELECT * FROM " + DB_TABLE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
UndergraduateDetailsPojo contact = new UndergraduateDetailsPojo();
contact.setID(cursor.getString(0));
contact.setName(cursor.getString(1));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Getting Count
public int getCount() {
String countQuery = "SELECT * FROM " + DB_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
別のクラス:
public class UndergraduateDetailsPojo {
private String _ID;
private String _NAME;
public UndergraduateDetailsPojo()
{
}
public UndergraduateDetailsPojo(String id,String name){
this._ID=id;
this._NAME = name;
}
//getting ID
public String getID() {
return this._ID;
}
//setting ID
public void setID(String ID) {
this._ID = ID;
}
//getting name
public String getName() {
return this._NAME;
}
//setting name
public void setName(String NAME) {
this._NAME = NAME;
}
}
ログキャットは次のとおりです。
05-28 19:22:26.372: WARN/dalvikvm(3971): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): FATAL EXCEPTION: main
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apcog/com.apcog.Grid_testActivity}: java.lang.ClassCastException: java.util.ArrayList
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.os.Looper.loop(Looper.java:123)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at java.lang.reflect.Method.invoke(Method.java:521)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at dalvik.system.NativeStart.main(Native Method)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): Caused by: java.lang.ClassCastException: java.util.ArrayList
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at com.apcog.Grid_testActivity.onCreate(Grid_testActivity.java:44)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-28 19:22:26.382: ERROR/AndroidRuntime(3971): ... 11 more
05-28 19:22:26.402: WARN/ActivityManager(70): Force finishing activity com.apcog/.Grid_testActivity
05-28 19:22:26.432: INFO/UsageStats(70): Deleting usage file : usage-20120514
05-28 19:22:26.722: DEBUG/dalvikvm(210): GC_FOR_MALLOC freed 8714 objects / 471808 bytes in 2976ms
05-28 19:22:26.932: WARN/ActivityManager(70): Activity pause timeout for HistoryRecord{45076730 com.apcog/.Grid_testActivity}
05-28 19:22:28.172: INFO/Process(3971): Sending signal. PID: 3971 SIG: 9
05-28 19:22:28.202: WARN/InputManagerService(70): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@450aa150
05-28 19:22:28.222: INFO/ActivityManager(70): Process com.apcog (pid 3971) has died.
05-28 19:22:32.691: DEBUG/dalvikvm(195): GC_EXPLICIT freed 156 objects / 7480 bytes in 132ms
05-28 19:22:37.265: WARN/ActivityManager(70): Activity destroy timeout for HistoryRecord{45076730 com.apcog/.Grid_testActivity}
私はtry..catchを追加しました...テキストビューを取得していますが、データがありません...これはDBコンテンツを表示するための正しいアプローチですか...
試す {
UndergraduateDetailsPojo(this,android.R.layout.simple_list_item_1,cursor,);
dbHelper db = 新しい dbHelper(this);
Log.d("読み取りデータ","読み取り"); リスト c=db.getAllContacts();
grid.setAdapter((ListAdapter) txt);
}
catch(Exception e)
{
}