2014-03-24 57 views
1

我正在使用擴展BAseAdapter以在列表視圖中顯示數組值的CustomGridAdapter。但是在CustomGridAdapter的getView中,數組值的位置不在流中。在getView方法中返回不正確的位置

public class CustomGridAdapter extends BaseAdapter { 

    private Context context; 
    private final String[] gridValues; 
    private final String[] gridImage; 

    //Constructor to initialize values 
    public CustomGridAdapter(Context context, String[] gridValues, String[] gridImage) { 
     this.context = context; 
     this.gridValues = gridValues; 
     this.gridImage=gridImage; 
    } 

    @Override 
    public int getCount() { 

     // Number of times getView method call depends upon gridValues.length 
     return gridValues.length; 
    } 

    @Override 
    public Object getItem(int position) { 

     return gridValues[position]; 
    } 

    @Override 
    public long getItemId(int position) { 

     return 0; 
    } 


    // Number of times getView method call depends upon gridValues.length 

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

     //LayoutInflator to call external grid_item.xml file 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View gridView; 

     if (convertView == null) { 

      gridView = new View(context); 

      // get layout from grid_item.xml 
      gridView = inflater.inflate(R.layout.grid_item, null); 

      // set value into textview 

      TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label); 
      textView.setText(gridValues[position]); 
      Log.i(new Integer(position).toString(),gridValues[position]); 
      ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
      String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position]; 
      Bitmap bitmap = BitmapFactory.decodeFile(path, options); 
      grp_icon.setImageBitmap(bitmap); 
      // set image based on selected text 

      ImageView imageView = (ImageView) gridView 
        .findViewById(R.id.grid_item_image); 

      String mobile = gridValues[position]; 



     } else { 
      gridView = (View) convertView; 
     } 

     return gridView; 
    } 
} 

在customGridAdapter在現在的位置的getView方法是在0,1,2,0,4順序爲什麼我2而不是3後得到0?

回答

2
@Override 
public long getItemId(int position) { 

    return position; //return position here 
} 

讓你的getView如,

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

    //LayoutInflator to call external grid_item.xml file 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View gridView; 

    if (convertView == null) { 

     gridView = new View(context); 

     // get layout from grid_item.xml 
     gridView = inflater.inflate(R.layout.grid_item, null); 

     // set value into textview 

     TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label); 
     textView.setText(gridValues[position]); 
     Log.i(new Integer(position).toString(),gridValues[position]); 
     ImageView grp_icon=(ImageView)gridView.findViewById(R.id.grid_item_image);   



    } else { 
     gridView = (View) convertView; 
    } 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     String path="/sdcard/Letsmeet/letsmeet_media/group_images/"+gridImage[position]; 
     Bitmap bitmap = BitmapFactory.decodeFile(path, options); 
     grp_icon.setImageBitmap(bitmap); 
     // set image based on selected text 

     ImageView imageView = (ImageView) gridView 
       .findViewById(R.id.grid_item_image); 

     String mobile = gridValues[position]; 

    return gridView; 
} 

你converView爲空只在屏幕上最初顯示次數的數量。一旦你滾動他們converView != null,他們回收。因此,將代碼移到它外面。

+0

在移動textView.setText(gridValues [position])後工作;對外線 – DharanBro

+0

是的,我錯過了移動。 :)我們應該始終將findViewById()代碼保存在if(convertView == null){}條件中。所有與應用setText()等值相關的代碼應該超出此條件。您還應該考慮ViewHolder模式。例如 - http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html –

0

你只需要使用像這樣的,你會把它作爲一個例子,修改自定義適配器,這樣你可以得到的輸出把我想你已經做了在轉換視圖

你只是刪除條件的錯誤convetview ... 和使用這樣

public class CustomAdapter extends ArrayAdapter<Sample> { 

public ArrayList<Sample> mlist; 
public Context context; 
public LayoutInflater inflater; 

public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) { 
    super(context, resource); 
    this.mlist = mlist; 
    this.context = context; 
    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getPosition(Sample item) { 
    return super.getPosition(item); 
} 

@Override 
public Sample getItem(int position) { 
    return mlist.get(position); 
} 

@Override 
public int getCount() { 
    return mlist.size(); 
} 

@Override 
public long getItemId(int position) { 
    return super.getItemId(position); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    convertView = inflater.inflate(R.layout.listitem, null); 
    TextView text1 = (TextView) convertView.findViewById(R.id.item1); 
    TextView text2 = (TextView) convertView.findViewById(R.id.item2); 
    text1.setText(mlist.get(position).getListitem1()); 
    text2.setText(mlist.get(position).getListitem2()); 
    text2.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // you just put your Logic here And use this custom adapter to 
      // load your Data By using this particular custom adapter to 
      // your listview 

     } 
    }); 
    return convertView; 
} 

    } 
0

這是關係到ListView和基於滾動位置等因素GC /性能原因ListView控件的顯示效果繼承類如何。在文件中規定,不保證訂單或位置。爲了解決這個問題,建議您使用ID而不是項目的位置來引用。

@Override 
public long getItemId(int position) { 
    return super.getItemId(position); 
} 

欲瞭解更多信息,請參閱: https://www.youtube.com/watch?v=wDBM6wVEO70