0

I am trying to display a list of images on the SD card to the user, allow them to select multiple images and then store those selected images in an array. Can you provide me any help with this?

public class selectimages extends Activity {
//---the images to display--- Integer[] 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;
    }
}    

}

Troy
  • 3
  • 3
  • I was able to load images from the sd card to the gridview, but I could not figure out a way to allow for the selection of multiple images. So now im trying to load them using listview and that is where im having the problem. – Troy Apr 03 '11 at 17:14
  • 1
    It'd be easier to answer this question if you posted the code that doesn't do what you want it to, and explain what you want it to do. Are you having the problem doing multiple selection, or even loading them into the listview at all? – Turnsole Apr 03 '11 at 17:19
  • I was able to get this code to work and display images from a folder in listview. – Troy Apr 03 '11 at 17:24
  • Sorry. Edited original message with source code. – Troy Apr 03 '11 at 17:39

2 Answers2

0

You could load those images to the listview with custom listview layout, afterwards put beside each images a checkbox...whenever user tick a checkbox which means he selected the image on that row of listview. Is that what you want?

ForeverNights
  • 631
  • 1
  • 7
  • 34
  • Yes, this is what I was looking for. I also want to store those selections in an array. I cant seem to post my source code here. What I was doing before was reading from a folder of images to a listview, but I want to read from an sd card and allow for multiple selection. – Troy Apr 03 '11 at 17:30
  • so did I answer your question? Or do you need further assistance? – ForeverNights Apr 03 '11 at 17:39
  • Well, yes you did answer my question. But I am not sure how to code this to work correctly. – Troy Apr 03 '11 at 17:42
0

Behold, getting the checked values of a Listview: previous question.

Darren Robins wrote:

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 */
 }
Community
  • 1
  • 1
Turnsole
  • 3,422
  • 5
  • 30
  • 52