getView()不能正常工作

时间:2013-10-21 13:47:12

标签: android baseadapter

我将开发一个APP,我正在使用自定义警报对话框。数据来自数据库到此警报对话框。

 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);

ABove行调用我的Adapter类。现在我要在适配器类中显示我的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

那么问题出在哪里我无法得到请一些身体帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

我相信它是因为你使用了getView错误,getView不会在被多次调用后被调用。每次调用getView时都会循环移动光标,因此在循环浏览2个列表后,只显示光标中的最后一项。

如果要从游标中获取数据,则应使用SimpleCursorAdapter,然后覆盖bindView,并在每次调用时使用方法中的游标变量

修改

基本上沿着这个

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

是您真正获取数据所需的一切

答案 1 :(得分:0)

我认为这是你的路线:

if (position != 0)

适配器的第一行是0,因此它不会设置第一行数据的数据。