Question

I'm new to this android apps development and currently, I'm creating an app that will get an image uri from the gallery to the listview on my activity. I have no problem with that. I just want to add a feature that when I clicked an image uri from my listview, a bitmap thumbnail of that image uri will appear. Here is my code:

     ListView lv;
 ArrayList<Uri> array_list = new ArrayList<Uri>();
 ArrayAdapter<Uri> array_adapter;

 final int RQS_LOADIMAGE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    array_adapter = new ArrayAdapter<Uri>(MainActivity.this, android.R.layout.simple_list_item_1, array_list);
    lv = (ListView)findViewById(R.id.list);
    lv.setAdapter(array_adapter);

}

    public void btn_ai(View view) {
       Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
       startActivityForResult(intent, RQS_LOADIMAGE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK){

            switch(requestCode){

            case RQS_LOADIMAGE:
                Uri imageUri = data.getData();
                array_list.add(imageUri);
                array_adapter.notifyDataSetChanged();
                break; 
            }
        }
    }

}

Was it helpful?

Solution

First you need to set up a ViewHolder like this: How to implement a view holder?

Follow that example and you then set up OnClickListener using the ViewHolder and wallah. You can then set you method() to launch a new activity with the Bitmap etc.

EDIT: It is better to use a CustomAdapter as this will allow ViewHolder to be implemented. See these:

http://www.vogella.com/articles/AndroidListView/article.html#androidlists_adapterintro http://developer.android.com/reference/android/widget/Adapter.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top