0

私はalertDialogのリストビューを持っています。リストビューにカスタムresourceCursorAdapterを設定し、各行にtextView、date textView、およびimageというタイトルが含まれています。Android Eclipse AlertDialogのListViewスクロール時に特定のテキストビューを隠す

リストビューをスクロールすると、一部の行の日付textViewの一部が消えてしまうことを除いて、すべてがうまくいきます。私は彼らがなぜ消えているのか分かりません。それを修正する方法ではありません...アイテムの中には日付を表示するものではないので、日付を表示するかどうかを決定するif文がbindviewにあります。これはalertdialog以外のリストでも機能しますが、消滅して表示されることになっているいくつかの日付になります。誰がなぜこれが起こっているのか知っていますか?

警告ダイアログとリストビューのための私のコード:

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これが正しい答えです。 –

+0

最も優れています。答えと参照された読書をありがとう!私は、リストアイテムが文字通りそのままリサイクルされたことに気付かなかった。しかし、1つの質問...これは私の問題を修正しましたが、なぜ同じコードを使用して同じ問題を経験していないのですが、アラートダイアログの中にリストビューを持たないのですか?すべてのtextviewはうまく機能します。私はテストを行い、bindviewメソッドが呼び出されていて、alertdialog(スクロール時)で繰り返し呼び出されたときに印刷されましたが、スクロールしていなくても最初は数回でした。 – Nick

+0

アダプタが同じなら、それは同様に動作するはずです。多分異なるデータですか? – pawelzieba

関連する問題