2016-04-19 9 views
0

私はアダプタで6セルの読み込みグリッドビューを持っています。私は各セルをクリックすると、写真を撮るかギャラリーからイメージを選択するかのいずれかのイメージを追加します。イメージを選択した後、グリッドビューは空白のみを表示しています。私は1つのセルに画像を設定しますが、別のセルに移動すると、前の選択はなくなります。どうしたらいいの?私が何か間違っているなら、私を案内してください。グリッドビューでのカスタムアダプタのリフレッシュandroid

if (convertView == null) { 
     grid = new View(mContext); 
     grid = inflater.inflate(R.layout.fpc_document_view, null); 
     TextView textView = (TextView) grid.findViewById(R.id.grid_text); 
     imageView = (ImageView) grid.findViewById(R.id.grid_image); 
     if (fileList.size() == 0) { 
      textView.setText(DOCUMENT_NAME_LIST[position].toString()); 
      for (int i = 0; i <= 6; i++) { 
       imageView.setImageResource(R.mipmap.ic_add_document); 
      } 
     } else { 

      Bitmap bitmapResized = null; 
      for (int i = 0; i < fileList.size(); i++) { 
       if (!fileList.get(i).equals("")) { 
        System.out.println("fileList here ,,,," + fileList.get(i).toString()); 
        Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_add_document); 
        bitmapResized = ((BitmapDrawable) drawable).getBitmap(); 
       } else { 
        Uri selectedImageUri = Uri.fromFile(fileList.get(i)); 
        bitmapResized = ImageRelatedStuff.convertURIToBitmap(selectedImageUri, mContext); 

        if (bitmapResized != null) { 
         Bitmap bitmapTemp = bitmapResized; 
         bitmapResized = null; 
         bitmapResized = ImageRelatedStuff.getResizedBitmap(bitmapTemp, 500, 500, 0); 
        } 
       } 
       imageView.setImageBitmap(ImageRelatedStuff.getRoundedCornerBitmap(bitmapResized, 15)); 
      } 
     } 
    } else { 
     grid = convertView; 
     imageView = (ImageView) grid.findViewById(R.id.grid_image); 

    } 

答えて

0
public class MyAdapter extends BaseAdapter{ 

private final int GRID_COUNT = 6; 

// you need an array save bitmap with position 
Bitmap[] bitmapArray; 

public MyAdapter(){ 
    bitmapArray = new Bitmap[GRID_COUNT]; 
} 

@Override 
public int getCount() { 
    return 6; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null){ 
     // inflater your layout 
    } 
    ImageView imageView = (ImageView) convertView.findViewById(R.id.grid_image); 
    // get the bitmap in array by position 
    Bitmap bitmap = bitmapArray[position]; 
    // when bitmap is null ,show default picture 
    if (bitmap == null){ 
     imageView.setBackgroundResource(R.drawable.ic_default); 
    }else{ 
     imageView.setImageBitmap(bitmap); 
    } 
    return convertView; 
} 

// activity or fragment use this method call adapter refresh 
public void setBitmap(int position, Bitmap bitmap){ 
    bitmapArray[position] = bitmap; 
    notifyDataSetChanged(); 
} 
} 

更新私の答え、希望のヘルプあなた

+0

私はgetcountメソッドで戻り値は常に6ですが、ファイルサイズは1です。ギャラリーからイメージを選択したときはいつでも1です。ファイルサイズは1ですが、常に6回ループしています。 – saravanan

+0

@saravanan getCount()メソッドはfileList.size()を返す必要がありますか? fileListのカウントが変更されたときは、adapter.notifyDataSetChanged()を呼び出す必要があります。 – HelloBird

+0

最初は、空のイメージ6のアイコンを各6つのセルに表示する必要があります。私は画像を入れるために各セルをクリックしなければならない。私がfilelist.size()を置くと、初めて1つのセルだけが表示され、2回目は2つのセルしか表示されません。どのようにすべての6つのアイコンを表示することができますまた、私は各セルに画像を追加することができるはずですか?..私を得ることができます?.. pls助けて.. – saravanan

2

変更するには、このようなあなたのコード:あなたのconvertViewではありませんあなたは上の何でもイメージを設定していないヌル等しいとき

if (convertView == null) { 
    grid = new View(mContext); 
    grid = inflater.inflate(R.layout.fpc_document_view, null); 
    TextView textView = (TextView) grid.findViewById(R.id.grid_text); 
    imageView = (ImageView) grid.findViewById(R.id.grid_image); 
} else { 
    grid = convertView; 
    imageView = (ImageView) grid.findViewById(R.id.grid_image); 

} 
if (fileList.size() == 0) { 
     textView.setText(DOCUMENT_NAME_LIST[position].toString()); 
     for (int i = 0; i <= 6; i++) { 
      imageView.setImageResource(R.mipmap.ic_add_document); 
     } 
    } else { 

     Bitmap bitmapResized = null; 
     for (int i = 0; i < fileList.size(); i++) { 
      if (!fileList.get(i).equals("")) { 
       System.out.println("fileList here ,,,," + fileList.get(i).toString()); 
       Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_add_document); 
       bitmapResized = ((BitmapDrawable) drawable).getBitmap(); 
      } else { 
       Uri selectedImageUri = Uri.fromFile(fileList.get(i)); 
       bitmapResized = ImageRelatedStuff.convertURIToBitmap(selectedImageUri, mContext); 

       if (bitmapResized != null) { 
        Bitmap bitmapTemp = bitmapResized; 
        bitmapResized = null; 
        bitmapResized = ImageRelatedStuff.getResizedBitmap(bitmapTemp, 500, 500, 0); 
       } 
      } 
      imageView.setImageBitmap(ImageRelatedStuff.getRoundedCornerBitmap(bitmapResized, 15)); 
     } 
    } 

問題がありますimageView。そのため、convertViewがnullである2番目のセルでは、nullではない前のセルでイメージを取得していますが、何も得られません。

+0

@ ramesh..thanks答え...私はそれをチェックします... – saravanan

+0

まだ解決されていない@ラメッシュ。私がsdカードから1つのイメージを選んだ場合、6つのセルすべてが同じイメージだけを表示しています。しかし、私の結果は、その画像などに追加される特定のセルです。 – saravanan

関連する問題