早上好,
我正在开发一个Android应用程序,我偶然发现了一些我无法解决的问题(搜索并没有帮助尝试和调整网上找到的代码,并且它可能已被回答)在我需要帮助之前,在我的具体案例中。
我的应用很简单,开始时启动(运行5秒)而不是主要活动(这是一个FragmentActivity)。在主要活动中,我有5个不同的ListFragments(现在我已经在列表中加载了一些随机文本以查看它的外观)。每个片段都有自己的数据库,它将填充ListFragment(我有1个数据库完成试用,然后继续制作其他数据库)。
对于1个数据库,我已经完成了我尝试使用适配器,处理程序和提供程序,但没有任何工作(ListFragment与数据库显示加载圈)。
然后我找到了教程“在应用程序中使用自己的数据库”,但这也没有帮助(更好的说我无法修改代码来处理我的项目)。
我所需要的只是:
以下是我的代码:
Splashscreen
public class Splash extends Activity {
private boolean bIsBackButtonPressed;
private static final int SPLASH_DURATION = 5000;
private static final int FM_NOTIFICATION_ID = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Testing app in execution")
.setContentText("Click here to go back in the app");
Intent notificationIntent = new Intent(this, Main.class);
notification.setOngoing(true);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, notification.build());
setContentView(R.layout.splash);
ImageView splashImage = (ImageView) findViewById(R.id.splash_screen_);
int orient = getWindowManager().getDefaultDisplay().getRotation();
if (orient == 0) {
splashImage.setBackgroundResource(R.drawable.splash_screen_port);
} else {
splashImage.setBackgroundResource(R.drawable.splash_screen_land);
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
finish();
if (!bIsBackButtonPressed) {
Intent intent = new Intent(Splash.this, Main.class);
Splash.this.startActivity(intent);
}
}
}, SPLASH_DURATION);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ImageView splashImage = (ImageView) findViewById(R.id.splash_screen_);
int orient = getWindowManager().getDefaultDisplay().getRotation();
if (orient == 0) {
splashImage.setBackgroundResource(R.drawable.splash_screen_port);
} else {
splashImage.setBackgroundResource(R.drawable.splash_screen_land);
}
}
@Override
public void onBackPressed() {
bIsBackButtonPressed = true;
finish();
super.onBackPressed();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(FM_NOTIFICATION_ID);
}
}
这是我的MainActivity
public class Main extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new 1List();
case 1:
return fragment = new 2List();
case 2:
return fragment = new 3List();
case 3:
return fragment = new 4List();
case 4:
return fragment = new 5List();
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.1);
case 1:
return getString(R.string.2);
case 2:
return getString(R.string.3);
case 3:
return getString(R.string.4);
case 4:
return getString(R.string.5);
}
return null;
}
}
private static final int FM_NOTIFICATION_ID = 0;
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Do you want to close the app?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Main.this.finish();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(FM_NOTIFICATION_ID);
}
})
.setNegativeButton("No", null)
.show();
}
}
这是我的第一个ListFragment(其他4个具有相同的确切内容 - 这应该填充我的数据库中存储在assets文件夹中的数据):
public class 1List extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] {
"Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Mac OS X", "Linux", "OS/2"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
正如我所提到的,我有一个扩展CursorAdapter的适配器 - 1ListDatabaseAdapter:
public class 1ListDatabaseAdapter extends CursorAdapter {
private LayoutInflater inflater;
private int s;
String TAG = "1ListDatabaseAdapter";
public 1ListDatabaseAdapter(Context context, Cursor c){
super(context, c);
inflater = LayoutInflater.from(context);
s = c.getCount();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name = (TextView) view.findViewById(R.id.name);
name.setText(cursor.getString(1));
cursor.moveToNext();
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
return inflater.inflate(R.layout.list_item, null);
}
}
我有一个处理程序1ListDatabaseHandler:
public class 1ListDatabaseHandler extends SQLiteOpenHelper {
private static final String TAG = 1ListDatabaseHandler.class.getSimpleName();
private static final String DATABASE_NAME = "1ldb.db";
private static final int DATABASE_VERSION = 1;
public static final String ID = "_id";
public static final String TABLE_NAME = "1list";
public static final String NAME = "name";
public static final String TYPE = "type";
public static final String ADDRESS = "adress";
public static final String PHONE = "phone";
public static final String MAIL = "email";
public static final String SITE = "website";
private static final String TABLE_CREATE = " CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( " + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ NAME + " STRING, " + TYPE + " STRING, " + ADDRESS + " STRING, " + PHONE + " STRING, " + MAIL + " STRING, " + SITE + " STRING ); ";
private static final String TABLE_DROP = "DROP TABLE IF EXISTS " + TABLE_NAME;
AlojamentoDatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// Log.i(TAG, "DatabaseHandler was initiated. ");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i(TAG, "1ListDatabaseHandler: " + db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading the database from version " + oldVersion + " to version " + newVersion);
db.execSQL(TABLE_DROP);
onCreate(db);
}
public void insert(String name, String type, String adress, String phone, String email, String website) {
long rowId = -1;
try {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME, name);
values.put(TYPE, type);
values.put(ADDRESS, adress);
values.put(PHONE, phone);
values.put(MAIL, email);
values.put(SITE, website);
rowId = db.insert(TABLE_NAME, null, values);
Log.i(TAG, "insert: " + name + ", " + type + ", " + adress + ", " + phone + ", " + email + ", " + website);
} catch (SQLiteException e) {
Log.i(TAG, "insert()", e);
} finally {
Log.i(TAG, "insert(): rowId=" + rowId);
}
}
public Cursor query() {
SQLiteDatabase db = getWritableDatabase();
return db.query(TABLE_NAME, null, null, null, null, null, ID + " ASC");
}
}
所有导入都是正确的,应用程序运行良好,因为它没有数据库(当然是一个可视的例子)现在我需要连接一个sqlite数据库来显示1ListFragment中的数据直到现在(在询问之前)这里的问题)我做了大量的研究试图弄清楚我自己(因为我的工作方式 - 哦是啊!)但总是抓着我的头,这么多代码我试图弥补和适应,没有什么工作,我真的觉得很简单我就是不能把手指放在上面。如何将我自己的预制数据库连接到我的应用程序并在1ListFragment中显示数据?
提前致谢。
答案 0 :(得分:0)
您正在使用ArrayAdapter,您必须使用您的adpater:
用以下代码替换1List类的de代码:
public class 1List extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
1ListDatabaseHandler Db = new 1ListDatabaseHandler(getActivity());
Cursor cursor = Db.query();
if (cursor != null) {
cursor.moveToFirst();
}
1ListDatabaseAdapter adapter = new 1ListDatabaseAdapter(getActivity(), cursor);
setListAdapter(adapter);
}
我看到的另一件事是您将1ListDatabaseHandler
命名为SQLiteOpenHelper class
并命名为其构造函数AlojamentoDatabaseHandler
。
将AlojamentoDatabaseHandler(Context context)
替换为1ListDatabaseHandler(Context context)
从cursor.moveToNext();
bindView
移除1ListDatabaseAdapter