2014-10-07 103 views
0

作为标题,我想创建一个列表视图与Fragment中的自定义行。我的代码如下。在片段android创建一个自定义列表视图

下面是我的片段类,它已扩展到listfragment, m困惑于如何设置此列表视图的适配器我的代码如下所示。

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

只是将所有这些代码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; 
} 
+0

Thnk你.. !!完成.. !!日蚀仍然显示在启动管理下的红色曲线(ourcursor) – 2014-10-07 08:11:04

+0

显示什么错误? – Piyush 2014-10-07 08:48:28

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 
+0

紧急你!!完成.. !!日蚀仍然显示在启动管理光标(ourcursor)下的红色曲线 – 2014-10-07 07:59:15