2015-10-16 99 views
-1

我已經創建GridView與圖像和複選框通過創建GridView的自定義佈局。Gridview與圖像和複選框

我要做的是,當用戶點擊圖片時,特定的複選框也應該被選中。

最後,當用戶單擊我在同一活動中添加的按鈕「保存」時,所選圖像應顯示在另一活動中。

我的適配器類如下:

public class MyAdapter extends BaseAdapter { 
private Context context; 
private int imgarray[]; 

MyAdapter(Context context, int imgarray[]) { 
    this.context = context; 
    this.imgarray = imgarray; 
} 

@Override 
public int getCount() { 
    return imgarray.length; 
} 

@Override 
public Object getItem(int position) { 
    return imgarray[position]; 
} 

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

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View grid; 

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

    if (convertView == null) { 

     grid = new View(context); 
     grid = inflater.inflate(R.layout.customgrid, null); 

     ImageView imageView = (ImageView) grid.findViewById(R.id.grid_item_image); 
     CheckBox chk = (CheckBox) grid.findViewById(R.id.grid_item_checkbox); 

     imageView.setImageResource(imgarray[position]); 
    } else { 
     grid = (View) convertView; 
    } 

    return grid; 
}} 

回答

-1

如果您正在加載從不同的URL的圖像,然後臨時存儲圖像的URL中的字符串類型變量,並將其發送給使用intent下一個活動/片段或bundle

如果您要從SD卡/手機存儲加載圖像,請將圖像URI或圖像的路徑作爲字符串存儲,然後發送到上述方法的下一個活動。

當您從可繪製文件夾訪問圖像時,請按照以下兩種方法之一以字符串形式訪問圖像路徑。

String imageUri = "drawable://" + R.drawable.image; 

或者

Uri path = Uri.parse("android.resource://com.yourpackage.name/" + R.drawable.icon); 
Uri otherPath = Uri.parse("android.resource://com.yourpackage.name/drawable/icon"); 

String path = path.toString(); 
String path = otherPath .toString(); 

一旦你得到所有的圖像路徑字符串,存儲在一個ArrayList所有字符串。

現在,假設您在列表視圖的第一個位置點擊複選框,將一個字符串變量與該列表列表的特定位置的路徑數據進行存儲(使用arrayList.get(position))。

將此字符串(包含圖像路徑)發送到下一個活動。現在在下一個要在圖像視圖中加載此圖像的活動,

File imgFile = new File(imagePathString); 

if(imgFile.exists()){ 

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 

myImage.setImageBitmap(myBitmap); 

} 

希望這有助於。

+0

那很好,但如何檢查出哪些圖像被選中? –

+0

你是如何獲得圖像?你有一個ArrayList或類似的東西的所有圖像網址? –

+0

從可繪製文件夾中,通過創建R.drawable.imagetitle的int數組。 –