2014-02-08 42 views
0

我對Android的新手,原諒我,如果基本的問題無法顯示在一個ListView項目(使用ResourceCursorAdapter和SQLite)

我做的嘗試是使用填充有來自數據光標從SQLite的填充的ListView適配器(從ResourceCursorAdapter繼承的類)。

我readed和嘗試了一些例子,但不能使ListView中顯示任何行。

我所看到的調試是「PedidosAdapter」(來自ResourceCursorAdapter派生)的「bindView」方法不會被調用......不知道爲什麼。

有人可以幫我解決這個問題?

我做什麼: - 我創建表_id字段(否則錯誤) - 我從listActivity和活動產生的主要活動,對雙方沒有表現出什麼...

我上傳的測試代碼上https://www.dropbox.com/s/g1dlff4esafgeiy/PedidosLH.zip如果要檢查它沒有直接讀到這裏粘貼代碼...

LAYOUT -- "activity_main.xml" 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.i4.pedidoslh.MainActivity"> 

    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/list_view" /> 

</LinearLayout> 

MainActivity代碼:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     PedidosSQLiteHelper pdb = 
       new PedidosSQLiteHelper(this, PedidosSQLiteHelper.DATABASE_NAME, null, 1); 

     SQLiteDatabase db = pdb.getWritableDatabase(); 
     // Generate example data... 
     if(db != null) 
     { 
      //Insertamos 5 usuarios de ejemplo 
      for(int i=1; i<=5; i++) 
      { 
       //Generamos los datos de prueba 
       int cod_mov_cc = i; 
       int nr_comprob = 10000+i; 
       int nro_clie = 5000+i; 

       //Insertamos los datos en la tabla ENCPEDID 
       db.execSQL(
         "INSERT INTO " + PedidosSQLiteHelper.TABLE_ENCPEDID + " (" 
           + PedidosSQLiteHelper.KEY_ENCPEDID_COD_MOV_CC + ", " 
           + PedidosSQLiteHelper.KEY_ENCPEDID_NR_COMPROB + ", " 
           + PedidosSQLiteHelper.KEY_ENCPEDID_NRO_CLIE + ") " + 
         "VALUES (" + cod_mov_cc + "," + nr_comprob + "," + nro_clie + ")"); 

       db.execSQL(
         "INSERT INTO " + PedidosSQLiteHelper.TABLE_ITMPEDID + " (" 
           + PedidosSQLiteHelper.KEY_ITMPEDID_NRO_ITEM + ", " 
           + PedidosSQLiteHelper.KEY_ITMPEDID_COD_MOV_CC + ", " 
           + PedidosSQLiteHelper.KEY_ITMPEDID_NR_COMPROB + ", " 
           + PedidosSQLiteHelper.KEY_ITMPEDID_COD_PROD + ", " 
           + PedidosSQLiteHelper.KEY_ITMPEDID_CANTIDAD + ", " 
           + PedidosSQLiteHelper.KEY_ITMPEDID_PENDIENTE + ", " 
           + PedidosSQLiteHelper.KEY_ITMPEDID_PORC_CUMPL + ", " 
           + PedidosSQLiteHelper.KEY_ITMPEDID_DESCRIP + ") " + 
         "VALUES (" 
           + i + "," 
           + cod_mov_cc + "," 
           + nr_comprob + ",'" 
           + "prd" + i + "'," 
           + i + "," 
           + i + "," 
           + "0" + "," 
           + "'" + "desc" + i + "'" + ")"); 
      } 

      //Cerramos la base de datos 
      db.close(); 
     } // db != null 

     fillData(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {  ...  } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { ... } 

    private void fillData() { 

     PedidosSQLiteHelper pdb = 
       new PedidosSQLiteHelper(this, PedidosSQLiteHelper.DATABASE_NAME, null, 1); 

     SQLiteDatabase db = pdb.getReadableDatabase(); 

     String sql = "SELECT _id, cod_mov_cc, nr_comprob FROM ENCPEDID"; 
     Cursor c = db.rawQuery(sql, null); 

     if (c.getCount() == 0) android.util.Log.w("deb", "fillData COUNT = 0"); 
     startManagingCursor(c); 

     // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listPessoas, PESSOAS); 
     PedidosAdapter pa = new PedidosAdapter(MainActivity.this, c); 

     ListView lv = (ListView) findViewById(R.id.list_view); 
     lv.setAdapter(pa); // setListAdapter(pa) if listActivity... 

     stopManagingCursor(c); 
     c.close(); 
    } 

    private class PedidosAdapter extends ResourceCursorAdapter { 

     public PedidosAdapter(Context context, Cursor cur) { 
      super(context, R.layout.row_pedidos, cur); 
      //mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     @Override 
     public View newView(Context context, Cursor cur, ViewGroup parent) { 
      LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      return li.inflate(R.layout.row_pedidos, parent, false); 
      //View item = mInflater.inflate(R.layout.row_pedidos, null); 
     } 

     @Override // this method is never called!! 
     public void bindView(View view, Context context, Cursor cur) { 

      TextView txtPedido = (TextView) view.findViewById(R.id.txtPedido); 
      TextView txtCliente = (TextView) view.findViewById(R.id.txtCliente); 
      //CheckBox cbListCheck = (CheckBox) view.findViewById(R.id.list_checkbox); 

      txtPedido.setText(
        cur.getString(cur.getColumnIndex("PEDIDO"))); 

      txtCliente.setText(
        cur.getString(cur.getColumnIndex(PedidosSQLiteHelper.KEY_ENCPEDID_NRO_CLIE))); 

      //cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(Datenbank.DB_STATE))==0?  false:true)))); 

      //String idname = cur.getString(cur.getColumnIndex("_id")); 
      //view.setTag(idname); 

     } // bindView 
    } // adapter 
} // class 

row_pedidos.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- android:textColor="@android:color/primary_text_dark" --> 
    <TextView android:text="Mensaje principal" 
     android:id="@+id/txtPedido" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="@android:style/TextAppearance.Large" 
     android:layout_marginTop="5dp" 
     /> 

    <!-- android:textColor="@android:color/secondary_text_dark" --> 
    <TextView android:text="mensaje debajo" 
     android:id="@+id/txtCliente" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="@android:style/TextAppearance.Small" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:layout_marginBottom="5dp" /> 
</LinearLayout> 

回答

1

卸下fillData()這幾行:

​​

光標傳遞給你的ResourceCursorAdapater後,該遊標需要保持開放,因爲它是什麼適配器從到讀取數據在您滾動列表時創建其視圖。如果關閉它,它將變得無效,並且適配器不再有任何數據顯示,因此您的ListView是空的。

你必須startManagingCursor呼叫允許活動採取基於活動的生命週期管理光標生命週期的服務。如果您的活動已停止,它會自動調用deactivate(),如果您的活動重新啓動,它將爲您調用requery(),並且當您的活動被銷燬時,它將被關閉。所以你不需要這兩行代碼,因爲系統正在爲你處理所有這些事情。

+0

你不知道我是如何試圖使它沒有運氣的工作,從來沒有那個鑫卡特已經光標的活動週期期間打開,我missunderstood適配器是如何工作的-without瞭解這個 - 太多的文檔,因爲通常在桌面上應用程序需要檢索數據時,它保持對內存和緊密的聯繫...... – FabianSilva

+0

感謝你回答了我這麼多和指導我什麼我作了非常錯誤的,我是用這個掙扎超過3天。 如果可以濫用你的幫助,你可以推薦我一些書閱讀有關android編程將不勝感激:D再次感謝。 – FabianSilva

+1

非常歡迎。你所做的並不是非常錯誤,只是一個簡單的錯誤/誤解,我們都是這樣做的。我個人讀過「Reto Meier專業Android 4應用程序開發」作爲起點,但要警告一些內容已經過時。那裏有很多書 - 看亞馬遜,閱讀評論並做出選擇...... – NigelK