【发布时间】: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