2012-01-20 9 views
3

リストビューをスクロールすると画像が表示されます(この時点では指定した国の1つだけです)。行。リストビューは画像付きですがリストをスクロールすると間違った行に表示されます

例: "france"の行には、フランスのフラグが表示されますが、同じフラグが他の行に表示されます。すべての行にあるわけではありません。現時点では、私はフランスの旗でテストしているだけです。

public class MyListAdapter extends SimpleCursorAdapter{ 

    private Cursor cur; 
    private Context context; 
    private SQLiteDatabase db; 
    private static final String ASSETS_DIR = "images/"; 
    private String Flag; 

    public MyListAdapter (Context context, Cursor c, SQLiteDatabase db) { 
     super(context, R.layout.countries_list_item, c, new String[] {}, new int[] {}); 
     this.cur=super.getCursor(); 
     this.context = context; 
     this.db=db; 

    } 

    private class ViewHolder { 
     TextView txtTitle, txtDate; 
     ImageView countryIcon; 
    } 



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

     ViewHolder holder = null; 
     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.countries_list_item, null); 

      holder = new ViewHolder(); 
      holder.txtTitle = (TextView) convertView.findViewById(R.id.lastName); 
      holder.txtDate = (TextView) convertView.findViewById(R.id.firstName); 
      holder.countryIcon = (ImageView) convertView.findViewById(R.id.country_icon); 


      convertView.setTag(holder); 
      } 
      else { 
      holder = (ViewHolder) convertView.getTag(); 
      } 

     this.cur.moveToPosition(position); 

     holder.txtTitle.setText(this.cur.getString(this.cur.getColumnIndex("CountryID"))); 
     holder.txtDate.setText(this.cur.getString(this.cur.getColumnIndex("Country"))); 
     Flag =(this.cur.getString(this.cur.getColumnIndex("CountryImage"))); 
     Flag=Flag.trim(); 

     // Set country icon usign File path 
     try { 
      String imgFilePath = ASSETS_DIR +""+Flag; 
      Bitmap bitmap = BitmapFactory.decodeStream(this.context.getResources().getAssets().open(imgFilePath));    
      holder.countryIcon.setImageBitmap(bitmap); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return convertView; 
    } 
} 
+1

http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkboxthrough-list-list/7738854#7738854 –

答えて

0

ビューがListViewsconvertViewを経由してリサイクルますので、これが起こっています。行内のViewsを参照するのに役立つホルダーがすでにありますので、ImageViewをつかんで、アダプターのデータに基づいてイメージを適切なイメージに設定する必要があります。これは、リストビュー内のアイテムは、どのアイテムが表示されているかわからないため、ListView内のビューであることがわかります。あなたはそれが何を表しているかを「知っている」ように、助けを与える必要があります。

リストビューのビューのリサイクルは非常に一般的な混乱の原因です。 Checkboxes in a ListView - When I select the first, it checks the last and vice versa

+0

こんにちは。ラグジュアリー。私はRowData名で空のクラスを作成しましたが、 "RowData object = getModel(position);" getView()で、getmodelでエラーを出してください。私は他の例を試してみて、何が問題なのかを見てみましょう。ありがとうございました – filoli

関連する問題