0
我試圖設置一個圖像從圖庫意圖到ImageView
行ListView
行,被點擊。新行會與附加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
?什麼是最好的方式來做到這一點?
太好了,謝謝! – user1835337
不客氣:) – eluleci