在我的应用程序中,我使用了两个片段,其中第一个片段用于在listview中显示sqlite数据库中的数据。那么,我该怎么办?
这是我的主要活动,名为CategoryList.java
package com.my.quote;
public class CategoryList extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction().add(R.id.menuContainer, new MenuFrag()).commit();
}
}
}
下面是菜单片段,其中包含两个按钮。单击第一个按钮时,类别片段将获得下面的代码,我已经开始了:
package com.my.quote;
public class CatFragActivity extends Fragment {
private QuoteHelper dbQuoteHelper = null;
private Cursor ourcursor = null;
private QuoteAdapter adapter = null;
public int a = 0;
public CatFragActivity(){
}
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.cat_frag, container, false);
ListView mylistview = (ListView)getActivity().findViewById(R.id.CatList);
dbQuoteHelper = new QuoteHelper(getActivity());
dbQuoteHelper.createDatabase();
dbQuoteHelper.openDataBase();
ourcursor = dbQuoteHelper.getcursor(a);
adapter = new QuoteAdapter(ourcursor);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> mylistview, View v, int position,
long id) {
Intent i = new Intent("com.my.quote.QuoteActivity");
Bundle b = new Bundle();
b.putInt("id", (int) id);
i.putExtras(b);
startActivity(i);
}
});
return rootview;
}
class QuoteAdapter extends CursorAdapter{
QuoteAdapter(Cursor c){
super(getActivity(),c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c) {
QuoteHolder holder = (QuoteHolder)row.getTag();
holder.populateFrom(c,dbQuoteHelper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
QuoteHolder holder = new QuoteHolder(row);
row.setTag(holder);
return(row);
}
}
class QuoteHolder{
private TextView name = null;
QuoteHolder(View row){
name=(TextView)row.findViewById(R.id.quoteText);
}
void populateFrom(Cursor c, QuoteHelper r){
name.setText(r.getName(c));
}
}
}
数据库助手类如下:
package com.my.quote;
public class QuoteHelper extends SQLiteOpenHelper{
private static final String DATABASE_PATH="/data/data/com.my.quote/databases/";
private static final String DATABASE_NAME="store.db";
private static final int SCHEMA_VERSION=1;
private static final String TABLE_NAME="quotes";
private static final String TABLE_NAME_CAT="categories";
private static final String COLUMN_ID="_id";
private static final String COLUMN_TITLE="body";
private static final String COLUMN_CAT="name";
public static int i;
//private static final String COLUMN_CAT="4";
public SQLiteDatabase dbsqlite;
private final Context mycontext;
public QuoteHelper(Context context) {
super(context,DATABASE_NAME,null,SCHEMA_VERSION );
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();
copyDBFromResources();
}
}
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 copyDBFromResources() {
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 databse");
}
}
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 Cursor getcursor(int a){
if(a==0){
SQLiteQueryBuilder querybuilder = new SQLiteQueryBuilder();
querybuilder.setTables(TABLE_NAME_CAT);
String[] asCloumsToReturn = new String[] {COLUMN_ID,COLUMN_CAT};
Cursor mcursor = querybuilder.query(dbsqlite, asCloumsToReturn, null, null, null, null, null);
return mcursor;
}
else{
int i = a;
SQLiteQueryBuilder querybuilder = new SQLiteQueryBuilder();
querybuilder.setTables(TABLE_NAME);
String[] asCloumsToReturn = new String[] {COLUMN_ID,COLUMN_TITLE};
String column ="category_id ="+ i;
Cursor mcursor = querybuilder.query(dbsqlite, asCloumsToReturn, column, null, null, null, null);
return mcursor;
}
}
public String getName(Cursor c){
return(c.getString(1));
}
public Cursor getDetailByTerm(String id) {
String[] args={id};
return(getReadableDatabase()
.rawQuery("SELECT _id, body FROM quotes WHERE _id=?",
args));
}
public Cursor getDetailsByCategory(String id){
String[] args={id};
return(getReadableDatabase()
.rawQuery("SELECT _id, body from quotes WHERE category_id = ?", args)
);
}
}