【问题标题】:set bitmap to the specific imageView of listview item将位图设置为 listview 项的特定 imageView
【发布时间】:2014-07-15 08:26:04
【问题描述】:

我正在尝试将图片从图库意图设置为已单击的 ListView 行的 ImageView。将使用添加 Menu 按钮添加新行(我的 row.xml 有 ImageView 和 TextView) 这是我的代码:

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?最好的方法是什么?

【问题讨论】:

    标签: android listview bitmap imageview


    【解决方案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));
    }
    

    这应该可行。但还要在设置其位图之前检查图像视图是否为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多