2011-11-12 48 views
0

我正在使用以下自定義適配器來填充來自數據庫光標的我的列表控件。 我無法理解爲什麼這個代碼在構造函數中崩潰,特別是當超級被調用時。使用DB光標查詢自定義適配器

public class ListAdaptor extends SimpleCursorAdapter { 

    private Cursor dataCursor; 
    private LayoutInflater mInflater; 

    class ViewHolder { 

     public TextView label = null; 
     public CheckBox chkBx = null; 
     public TextView price = null; 
     public TextView weight = null; 
    } 


    //constructor 
    public ListAdaptor(Context context, int layout, Cursor dataCursor, String[] from, int[] to) { 

     super(context, layout, dataCursor, from, to); 
     this.dataCursor = dataCursor; 
     mInflater = LayoutInflater.from(context); 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     // A ViewHolder keeps references to children views to avoid unneccessary calls 
     // to findViewById() on each row. 
     ViewHolder holder; 

     // When convertView is not null, we can reuse it directly, there is no need 
     // to reinflate it. We only inflate a new View when the convertView supplied 
     // by ListView is null. 

     if (convertView == null) { 

      // Inflate the view 
      convertView = mInflater.inflate(R.layout.listviewlyt, null); 

      // Get the ID's of the views 
      TextView tmpLbl  = (TextView)convertView.findViewById(R.id.label); 
      CheckBox tmpChkBx = (CheckBox)convertView.findViewById(R.id.chkbox); 
      TextView tmpPrc  = (TextView)convertView.findViewById(R.id.labelPrice); 
      TextView tmpWt  = (TextView)convertView.findViewById(R.id.labelWt); 


      // Creates a ViewHolder and store references to the two children views 
      // we want to bind data to. 
      holder = new ViewHolder(); 

      holder.label = tmpLbl; 
      holder.chkBx = tmpChkBx; 
      holder.price = tmpPrc; 
      holder.weight = tmpWt; 

      // Set the Tag 
      convertView.setTag(holder); 

     } 
     else { 
      // Get the ViewHolder back to get fast access to the TextView 
      // and the ImageView. 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     // Bind the data efficiently with the holder. 
     // Cursor to current item 

     dataCursor.moveToPosition(position); 
     String keyWrd = dataCursor.getString(2); 
     String price = dataCursor.getString(3); 

     TextView labelRef = holder.label; 
     CheckBox chbxRef = holder.chkBx; 
     TextView labelPrc = holder.price; 
     TextView labelWt = holder.weight; 

     labelRef.setText(keyWrd); 
     labelPrc.setText(price); 
     //chbxRef.setChecked(refObj.flag); 
     //labelWt.setText(refObj.wt); 

     return convertView; 
    } 
} 

有人能幫我找到原因嗎?

+0

POST錯誤日誌,請。 – hovanessyan

+0

螺紋[<3>主](暫停)\t \t \t \t ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,意圖)線:2494 \t \t \t \t ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,意圖)線:2512 \t \t \t \t ActivityThread.access $ 2200(ActivityThread,ActivityThread $ ActivityRecord,Intent)line:119 \t \t \t \t ActivityThread $ H.handleMessage(Message)line:1863 \t \t \t \t ActivityThread $ H(處理程序).dispatchMessage(消息)線:99 \t \t \t \t Looper.loop()線:123 \t \t \t \t ActivityThread.main(字符串[])線:4363 – Phoenix

+0

什麼是例外嗎? –

回答

0

最大的可能是這條線可能在向問題

mInflater = LayoutInflater.from(context); 
+0

謝謝Rajdeep。我明白了這一點。問題在於我沒有在我的數據庫中使用_Id,因爲光標無法工作。另外,我發現從CusrorAdaptor擴展適配器類並重寫BindView和NewView成員函數會更容易。 – Phoenix