将位图设置为listview项的特定imageView

时间:2014-07-15 08:26:04

标签: android listview bitmap imageview

我尝试将图片从图片意图设置为ImageView行的ListView,点击了该图片。将使用添加Menu按钮添加新行(我的row.xml包含ImageViewTextView) 这是我的代码:

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);
    }

}

如何将uniq行ImageView移至onActivityResult?最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

您可以将点击的视图分配给另一个变量。当您从活动中返回时,您将在该变量中找到最后分配的图像视图。

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));
}

这应该有效。但在设置“位图”之前,还要检查图像视图是否为空。