0

我有一個alertDialog內的列表視圖。我使用自定義的resourceCursorAdapter填充列表視圖,並且每行都包含標題textView,日期textView和圖像。AlertDialog中的Android Eclipse ListView在滾動時隱藏某些文本視圖

一切運作良好,除了它看起來,當你滾動列表視圖,一些行中的某些日期textView消失。我已經找出它們爲什麼消失了,而不是如何修復它...一些項目並不意味着顯示日期,所以我有一個關於bindview的if語句來決定是否顯示日期。這適用於alertdialog之外的任何列表,但它會使某些日期顯示爲消失。有誰知道爲什麼會發生這種情況?

我的警告對話框和ListView代碼:

itemSendPickerDialog = new Dialog(this); 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Select Item to Send"); 

     ListView lv = new ListView(this); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long i) { 


       } 
      }); 
     AlertDialog sendOptionAlert = sendOptionBuilder.create(); 

         sendOptionAlert.show(); 

        System.out.println("ID is: " + iid + " and position is: " + position); 
        itemSendPickerDialog.dismiss(); 
        } 
      }); 
     Cursor c = mDbHelper.fetchItemsByDate(id); 
     c.moveToFirst(); 

     int i = R.layout.send_item_menu_row; 
     MyListAdapter ia = new MyListAdapter(this, i, c); 
     lv.setAdapter(ia); 

     builder.setView(lv); 
     itemSendPickerDialog = builder.create(); 
     itemSendPickerDialog.show(); 

而且我的自定義listAdapter:

class MyListAdapter extends ResourceCursorAdapter { 
    public MyListAdapter(Context context, int i, Cursor cursor) { 
     super(context, i, cursor); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 

     TextView title = (TextView) view.findViewById(R.id.item_name); 

     title.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_TITLE))); 

     Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     int width = display.getWidth(); 
     width = width - 150; 
     ViewGroup.LayoutParams params = title.getLayoutParams(); 
     params.width = width; 
     title.setLayoutParams(params); 

     String cat = cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_CATEGORY)); 

     if (cat.equalsIgnoreCase("trip notes")) { 
      LinearLayout ll = (LinearLayout) view.findViewById(R.id.item_datetime_holder); 
      ll.setVisibility(View.INVISIBLE); 
     } 
     TextView date = (TextView) view.findViewById(R.id.item_date); 
     date.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_DEP_DATE))); 

     TextView time = (TextView) view.findViewById(R.id.item_time); 
     time.setText(cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_DEP_TIME))); 

     ImageView iv = (ImageView) view.findViewById(R.id.image_icon); 
     if (iv != null) { 
      int index = cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_TYPE); 

      String type = cursor.getString(index); 
      if (type != null) { 

      } else { 
       type = "notes"; 
      } 

      iv.setImageResource(getTypeResource(type)); 
     } 

    } 

    public static int getTypeResource(String type) { 
     if (type.equalsIgnoreCase("flight")) { 
      return R.drawable.airplaneicon; 
     } 
     if (type.equalsIgnoreCase("boat/ship")) { 
      return R.drawable.boatshipicon; 
     } 
     if (type.equalsIgnoreCase("bus")) { 
      return R.drawable.busicon; 
     } 
     if (type.equalsIgnoreCase("rail")) { 
      return R.drawable.railicon; 
     } 
     if (type.equalsIgnoreCase("auto or other rentals")) { 
      return R.drawable.caricon; 
     } 
     if (type.equalsIgnoreCase("other transportation")) { 
      return R.drawable.othertransicon; 
     } 
     if (type.equalsIgnoreCase("hotel")) { 
      return R.drawable.hotelicon; 
     } 
     if (type.equalsIgnoreCase("resort")) { 
      return R.drawable.resorticon; 
     } 
     if (type.equalsIgnoreCase("bed & breakfast")) { 
      return R.drawable.bandbicon; 
     } 
     if (type.equalsIgnoreCase("camp ground")) { 
      return R.drawable.campgroundicon; 
     } 
     if (type.equalsIgnoreCase("vacation rental")) { 
      return R.drawable.vacationrentalicon; 
     } 
     if (type.equalsIgnoreCase("other lodging")) { 
      return R.drawable.otherlodgingicon; 
     } 
     if (type.equalsIgnoreCase("rail")) { 
      return R.drawable.railicon; 
     } 
     if (type.equalsIgnoreCase("cruise")) { 
      return R.drawable.cruiseicon; 
     } 
     if (type.equalsIgnoreCase("tour")) { 
      return R.drawable.touricon; 
     } 
     if (type.equalsIgnoreCase("dining")) { 
      return R.drawable.diningicon; 
     } 
     if (type.equalsIgnoreCase("spa")) { 
      return R.drawable.spaicon; 
     } 
     if (type.equalsIgnoreCase("class")) { 
      return R.drawable.classicon; 
     } 
     if (type.equalsIgnoreCase("other activity")) { 
      return R.drawable.eventicon; 
     } else { 
      return R.drawable.notesicon; 
     } 
    } 

} 

,使文本消失的特定代碼在bindview覆蓋:

String cat = cursor.getString(cursor.getColumnIndex(TripsDbAdapter.KEY_ITEM_CATEGORY)); 

     if (cat.equalsIgnoreCase("trip notes")) { 
      LinearLayout ll = (LinearLayout) view.findViewById(R.id.item_datetime_holder); 
      ll.setVisibility(View.INVISIBLE); 
     } 

回答

1

ListView中的視圖被回收,當行在底部消失時它正在出現列表的頂部。這不是重新創建,而是這樣的優化。在bindView()中,您只設置當前值。

您忘記了將視圖設置爲可見。

LinearLayout ll = (LinearLayout) view.findViewById(R.id.item_datetime_holder); 
    if (cat.equalsIgnoreCase("trip notes")) { 
     ll.setVisibility(View.INVISIBLE); 
    } else { 
     ll.setVisibility(View.VISIBLE); 
    } 

值得一看:http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

+1

打我給它:d這是一個正確的答案。 – 2012-03-06 15:03:08

+0

最優秀。非常感謝您的答覆和參考讀物!我沒有意識到列表項目是按照原樣回收的。一個問題,雖然...這解決了我的問題,但爲什麼我沒有遇到同樣的問題使用相同的代碼,但沒有在警報對話框內的列表視圖?所有的textview都能很好地工作。我做了一個測試,並在bindview方法被調用時打印出來,並且它在alertdialog中重複調用(當滾動時),但是最初不在一個時(即使在滾動時)出現幾次。 – Nick 2012-03-06 15:51:21

+0

如果適配器是相同的,那麼它應該類似。也許不同的數據? – pawelzieba 2012-03-06 20:40:16

相關問題