简体   繁体   中英

set bitmap to the specific imageView of listview item

I'm trying to set an image from Gallery intent to the ImageView of ListView row, that was clicked. New rows will be added with the add Menu Button (my row.xml have ImageView and TextView ) here is my code:

public class MyActivity extends ListActivity {

    private ListView lv;
    private ArrayList<String> itemArray;
    private ArrayAdapter<String> itemAdapter;

    //new gallery intent privates
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        itemArray = new ArrayList<String>();
        itemArray.clear();

        itemAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.label, itemArray);
        setListAdapter(itemAdapter);

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Toast.makeText(this, selectedImagePath, Toast.LENGTH_SHORT).show();

                Bundle extras = data.getExtras();
                if (extras != null) {

                    ImageView mImg = (ImageView) findViewById(R.id.icon);
                    mImg.setImageBitmap(BitmapFactory.decodeFile(selectedImagePath));
                }

            }
        }
    }

    public String getPath(Uri uri) {
        if( uri == null ) {
            return null;
        }
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        return uri.getPath();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();

        // select a file
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
    }

    protected void addItemList() {
        // TODO Auto-generated method stub
        itemArray.add(0,"step");
        itemAdapter.notifyDataSetChanged();
    }


    // menu options
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        menu.add("add");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        addItemList();
        Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
        return super.onOptionsItemSelected(item);
    }

}

How can I move uniq row ImageView to the onActivityResult ? What is the best way to do that?

You can assign the clicked view to another variable. And when you return from the activity you'll find the last assigned image view in that variable.

ImageView lastClickedRowImage;

...

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    // find the image view from clicked row and assign to lastClickedRowImage
    // ex: v.findViewById(R.id.image_to_change)
    lastClickedRowImage = v.findViewById(R.id.image_to_change);
    ...
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if conditions...
    lastClickedRowImage.setImageBitmap(BitmapFactory.decodeFile(selectedImagePath));
}

This should work. But also check for the image view is null or not before setting its' bitmap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM