我完成了程序,动态显示sdcard
的所有图像。但现在,我希望从sdcard
动态显示单个图像,而不是显示所有图像。
我的编码如下
public class Gallery1Activity extends Activity {
// private ArrayList<String> imglist;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
ArrayList arr = new ArrayList();
super.onCreate(savedInstanceState);
setContentView(R.layout.gallerygrid);
GridView gv1=(GridView) this.findViewById(R.id.gridView1);
//gv1.setAdapter(new galleryImageAdapter(this));
arr = galldatabase();
gv1.setAdapter(new galleryImageAdapter(this,arr));
}
private ArrayList galldatabase() {
// TODO Auto-generated method stub
ArrayList ThumbsIDList = new ArrayList();
//Uri u=MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
/*String[] projection =new String[]{
Images.Thumbnails._ID,
Images.Thumbnails.DATA,
Images.Thumbnails.IMAGE_ID};*/
Cursor galleryimagecursor=managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,new String[]{
Images.Thumbnails._ID,
Images.Thumbnails.DATA} , null, null, null);
if(galleryimagecursor!=null&&galleryimagecursor.moveToFirst()){
String thumbsID;
String thumbsImageID;
String thumbsData;
int num=0;
do{
thumbsID=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID));
thumbsData=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails.DATA));
Log.i("BMP","size "+thumbsID+" "+thumbsData);
num++;
/*if(thumbsImageID!= null) {*/
ThumbsIDList.add(thumbsID);
/*ThumbsImageIDList.add(galleryimagecursor.getString(thumbsImageIDcol));
ThumbsDataList.add(galleryimagecursor.getString(thumbsDataCol));
}*/
}
while(galleryimagecursor.moveToNext());
}
return ThumbsIDList;
}
}
然后适配器代码如下
public class galleryImageAdapter extends BaseAdapter {
Context con;
private ArrayList<String> imgList;
private String thumbsID;
public galleryImageAdapter(Context c,ArrayList arr){
con=c;
imgList = arr;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if( convertView==null){
LayoutInflater li;
li = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=li.inflate(R.layout.galleryadapter,null);
final ImageView iv1=(ImageView)v.findViewById(R.id.galimage);
TextView tv1=(TextView)v.findViewById(R.id.galimagtext);
tv1.setText("Image"+position);
Log.d("imagevalue",imgList.get(position));
iv1.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+imgList.get(position)/*galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID)*//*imgList.get(position)*/));
}
else
v=convertView;
return v;
}
}
答案 0 :(得分:0)
你使用arr = galldatabase();
创建了另一个arraylist(newarr),只有1个元素。如果你想要显示第二张图像只需从arr arr复制该图像的拇指ID并将其存储在新的数组列表中
例如
ArrayList newarr = new ArrayList();
newarr.add(arr.get(random position));
将此列表分配给适配器,而不是分配给包含所有图像列表的原始数组列表
使用
gv1.setAdapter(new galleryImageAdapter(this,newarr));