显示SD卡中的图像列表

时间:2011-04-03 17:07:38

标签: android

我正在尝试向用户显示SD卡上的图像列表,允许他们选择多个图像,然后将这些选定的图像存储在一个阵列中。你能帮我解决这个问题吗?

公共类selectimages扩展了Activity {
    // ---要显示的图像---     整数[] imageIDs = {             R.drawable.a_1,             R.drawable.a_2,             R.drawable.a_3,             R.drawable.a_4,             R.drawable.a_5,             R.drawable.a_6,             R.drawable.a_7
    };

@Override    
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.selectimages);

    ListView listview = (ListView) findViewById(R.id.listview);
    listview.setAdapter(new ImageAdapter(this));

    listview.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, 
        View v, int position, long id) 
        {                
            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();
        }
    });        
}

public class ImageAdapter extends BaseAdapter 
{
    private Context context;

    public ImageAdapter(Context c) 
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(imageIDs[position]);
        return imageView;
    }
}    

}

2 个答案:

答案 0 :(得分:0)

您可以使用自定义列表视图布局将这些图像加载到列表视图中,然后在每个图像旁边放置一个复选框...每当用户勾选一个复选框时,这意味着他选择了该行列表视图中的图像。这就是你想要的吗?

答案 1 :(得分:0)

看看,获取Listview的选中值:previous question

达伦罗宾斯写道:

int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
 if (checked.get(i)) {
  String item = cont_list.get(i);
  /* do whatever you want with the checked item */
 }