作为标题我想在Fragment中创建一个带有自定义行的列表视图。我的代码如下。
下面是我的片段类,它已扩展到listfragment, 我对如何为此列表视图设置适配器感到困惑,我的代码如下所示。
Fragment class
public class CatFragActivity extends ListFragment {
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);
return rootview;
ListView mylistview = (ListView)getActivity().findViewById(R.id.CatList);
dbQuoteHelper = new QuoteHelper(this);
dbQuoteHelper.createDatabase();
dbQuoteHelper.openDataBase();
ourcursor = dbQuoteHelper.getcursor(a);
startManagingCursor(ourcursor);
adapter = new QuoteAdapter(ourcursor);
mylistview.setAdapter(adapter);
}
class QuoteAdapter extends CursorAdapter{
QuoteAdapter(Cursor c){
super(CatFragActivity.this,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);
}
}
static 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));
}
}
}
DatabaseHelper类
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;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
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)
);
}
}
答案 0 :(得分:0)
只需在return rootView
之前移动所有代码。
ListView mylistview = (ListView)rootView.findViewById(R.id.CatList);
dbQuoteHelper = new QuoteHelper(getActivity());
dbQuoteHelper.createDatabase();
dbQuoteHelper.openDataBase();
ourcursor = dbQuoteHelper.getcursor(a);
startManagingCursor(ourcursor);
adapter = new QuoteAdapter(ourcursor);
mylistview.setAdapter(adapter);
最后它将是
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.cat_frag, container, false);
ListView mylistview = (ListView)rootview .findViewById(R.id.CatList);
dbQuoteHelper = new QuoteHelper(getActivity());
dbQuoteHelper.createDatabase();
dbQuoteHelper.openDataBase();
ourcursor = dbQuoteHelper.getcursor(a);
startManagingCursor(ourcursor);
adapter = new QuoteAdapter(ourcursor);
mylistview.setAdapter(adapter);
return rootview;
}
答案 1 :(得分:0)
你onCreate永远不会执行到最后
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.cat_frag, container, false);
return rootview; //code STOPS here move this at the end of your onCreate