2013-10-21 73 views
0

我要開發一個應用程序,我正在使用自定義警報對話框。數據已經從數據庫傳送到此警報對話框。getView()不工作,因爲我假設

menu_to_showdialogCursor = dh.rawQuery("SELECT _id ,item_name,Item_cost FROM order_customer WHERE item_name LIKE ? AND Item_cost LIKE ?", new String[]{"%","%"}); 
OrderListAdapter orderAdapter = new OrderListAdapter(MainScreen.this,menu_to_showdialogCursor); 
orderList.setAdapter(orderAdapter); 

上述行叫我的適配器class.Now餘馬要告訴我的getView()在適配器類

 public View getView(int position, View convertView, ViewGroup parent) { 
    OrderViewHolder orderViewHolder = null; 
    if (convertView == null) { 
     orderViewHolder = new OrderViewHolder(); 

      convertView = inflater.inflate(R.layout.order_list_row, null); 
      orderViewHolder.setTvTitle((TextView) convertView 
        .findViewById(R.id.orderTitle)); 
      orderViewHolder.setTvPrice((TextView) convertView 
        .findViewById(R.id.orderPrice)); 
      orderViewHolder.setIvDelete((ImageButton) convertView 
        .findViewById(R.id.deleteOrder)); 


     convertView.setTag(orderViewHolder); 

    } else { 
     orderViewHolder = (OrderViewHolder) convertView.getTag(); 
    } 

    if (position != 0) 

    { 
    System.out.println(" value of position :"+position); 
    List lit = new ArrayList(); 
    List litp = new ArrayList(); 

// System.out.println(" value of postion of cursor : "); 
    OrderViewHolder odr_obj = null; 
     if (oStarterCursor.moveToFirst()) {  
      do{  
       odr_obj = new OrderViewHolder(); 
      title = oStarterCursor.getString(oStarterCursor.getColumnIndex("item_name")); 
      System.out.println("value of title :"+title); 

      lit.add(title); 
      price = oStarterCursor.getString(oStarterCursor.getColumnIndex("Item_cost")); 
      System.out.println("value of price :"+price); 
      litp.add(price); 
      }while(oStarterCursor.moveToNext()); 
     } 
     // _id = oStarterCursor.getInt(oStarterCursor.getColumnIndex("_id")); 
      if (title != null) { 
       title = title.trim(); 
       Iterator it = lit.iterator();  
       while (it.hasNext()) { 
        int i =0; 
        //System.out.println(" data iterator "+it.next().toString()); 
        String test =it.next().toString(); 
        System.out.println(" iterator vavue :"+test); 
        orderViewHolder.getTvTitle().setText(test); 

       } 

       orderViewHolder.getTvTitle().setTextColor(R.color.black); 
       orderViewHolder.getTvTitle().setTextSize(12); 
       orderViewHolder.getTvTitle().setTypeface(Typeface.DEFAULT); 
       orderViewHolder.getTvTitle().setGravity( 
         Gravity.CENTER_VERTICAL);  
      } 
      if (price != null) { 
       price = price.trim(); 
       orderViewHolder.getTvPrice().setText(price + ".00"); 
       orderViewHolder.getTvTitle().setTextColor(R.color.black); 
       orderViewHolder.getTvTitle().setTextSize(12); 
       orderViewHolder.getTvTitle().setTypeface(Typeface.DEFAULT); 
       orderViewHolder.getTvTitle().setGravity(
         Gravity.CENTER_VERTICAL); 
      } 
      //_id = oStarterCursor.getInt(oStarterCursor.getColumnIndex("_id")); 




     //convertView.setTag(R.id.orderTitle, _id); 
     if (orderViewHolder.getIvDelete() != null) { 
      //orderViewHolder.getIvDelete().setTag(R.id.orderTitle, _id); 
     } 

    } 
    return convertView;} 

問題是,在我的數據的基礎上有兩排在下面的圖像.. enter image description here

但在我的警報對話框中只顯示一行兩次。我沒有得到哪裏是邏輯錯誤。爲了方便,我將顯示我的屏幕截圖enter image description here

那麼問題我無法得到請有些身體幫助我解決這個問題。

回答

0

我相信它是因爲你使用getView錯誤,getView不會被調用多次被調用。每次調用getView時都會循環顯示光標,因此在循環遍歷2個列表後,只顯示光標中的最後一項。

如果你是從一個光標,你應該使用SimpleCursorAdapter,然後覆蓋bindView,只是每次它叫

編輯

基本上沿行使用遊標變量的方法獲取數據這

oStarterCursor.moveToPosition(position); 
orderViewHolder.getTvTitle().setText(oStarterCursor.getString(oStarterCursor.getColumnIndex("item_name"))); 

是所有你需要得到的數據真的

+0

感謝您的回覆........ – DJhon

0

我認爲這是你的行:

if (position != 0) 

適配器的第一行是0,所以它不會對數據的第一行設置數據。