【发布时间】:2014-07-15 05:54:41
【问题描述】:
我有包含图像和文本的列表视图,我可以调用图库意图 onClick。
这里是我的自定义行:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label" >
</TextView>
</LinearLayout>
及主要代码:
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();
}
}
}
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);
}
}
如何将选定的图像设置为单击的列表项 imageView?找不到任何解决方案如何连接它们/ (如果我知道它们的目录,我知道如何加载图像(我在这里不需要它),但我不知道如何在意图之后设置它们)
【问题讨论】:
-
您是否在列表视图图像中设置了图像??
-
我需要在row.xml中将选中的图像设置为ImageView。在那之前我有@drawable/ic_launcher。
标签: android listview android-intent imageview uri