2014-10-07 140 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