我是初学者,刚开始了解android编程。
我正在使用 SlidingMenu 库,并尝试在片段中列出项目,如图所示。(显示的项目只是字符串)
http://imageshack.us/photo/my-images/46/screenshot2013021419134.png/
但是,当我尝试从预制数据库中提取数据并列出所提到的项目时,我遇到了永无止境的加载。
http://imageshack.us/photo/my-images/577/screenshot2013021923094.png/
这是我的代码..
public class MainActivity extends SlidingFragmentActivity {
public static int THEME = R.style.Theme_Sherlock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// All related with slidingMenu
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.primary_view);
getFragmentManager().beginTransaction()
.replace(R.id.primary_view, new FragList())
.commit();
SlidingMenu sm = getSlidingMenu();
sm.setMode(SlidingMenu.LEFT_RIGHT);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setSecondaryMenu(R.layout.secondary_view);
getFragmentManager().beginTransaction()
.replace(R.id.secondary_view, new WorshipFragList())
.commit();
sm.setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setSlidingActionBarEnabled(false);
}
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
if (null != searchView )
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
return super.onCreateOptionsMenu(menu);
}
}
class SongHelper extends SQLiteOpenHelper {
private static String DATABASE_PATH = "";
private static final String DATABASE_NAME = "DataDB";
public static final String TABLE_NAME = "song";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public SQLiteDatabase dbSqlite;
private final Context myContext;
Cursor cursor;
public SongHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
DATABASE_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
createDB();
}
private void createDB() {
boolean dbExist = DBExists();
if(!dbExist) {
this.getReadableDatabase();
copyDBFromResource();
}
}
private boolean DBExists() {
SQLiteDatabase db = null;
try {
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "Database not found");
}
if (db!=null) {
db.close();
}
return db!= null ? true : false;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outputStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;
try {
inputStream = myContext.getAssets().open(DATABASE_NAME);
outputStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch(IOException e) {
throw new Error("Problem copying database from resource file.");
}
}
public void openDatabase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (dbSqlite != null)
{
dbSqlite.close();
}
super.close();
}
public ArrayList<String> getTableColumn(String TABLE_NAME, String[] column){
ArrayList<String> aL = new ArrayList<String>();
cursor = dbSqlite.query(TABLE_NAME, column, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
aL.add(cursor.getString(0));
cursor.moveToNext();
}
System.out.println(aL);
cursor.close();
return aL;
}
public String getName(Cursor c) {
return(c.getString(1));
}
}
public class FragList extends ListFragment {
SongHelper dbSongHelper = null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dbSongHelper = new SongHelper(getActivity());
}
}
非常感谢帮助找到解决此问题的方法..
谢谢 :)