我想设计一个listView,当用户添加照片时,它会自动用图像的名称和缩略图更新listView。但是,我当前的尝试甚至没有渲染图像。是否可以为listView的每个单元格动态设置ImageView,并在向适配器添加新元素时对其进行更新?
listView和add按钮的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Photos"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/view_album_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<ListView android:layout_marginTop="?android:attr/actionBarSize"
android:id="@+id/photos_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:choiceMode="singleChoice"
android:listSelector="@android:color/darker_gray" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/add_photo_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="25dp"
android:layout_marginEnd="25dp"
android:clickable="true"
android:src="@drawable/plus"/>
</RelativeLayout>
活动:
public class ViewAlbum extends AppCompatActivity {
public Album activeAlbum;
private ListView photoList;
private ArrayList<String> currentPhotos = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private int currentSelected = -1;
public static final int PICK_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_album);
final Context context = getApplicationContext();
Toolbar albumBar = findViewById(R.id.view_album_bar);
setSupportActionBar(albumBar);
photoList = findViewById(R.id.photos_list);
Bundle bundle = getIntent().getExtras();
String albumName = bundle.getString("album_name");
activeAlbum = Album.loadAlbum(this.getFilesDir().getPath()+"/"+albumName+".txt");
for(Photo photo: activeAlbum.photos){
ImageView thumbnail = findViewById(R.id.photo_thumbnail);
if(thumbnail == null){
System.out.println("why?");
//Don't understand why it is null, can't set image
}
currentPhotos.add(photo.name);
}
adapter = new ArrayAdapter<String>(this, R.layout.photo_cell,
R.id.photo_name,currentPhotos);
photoList.setAdapter(adapter);
photoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
currentSelected = position;
}
});
FloatingActionButton addPhotoBtn = (FloatingActionButton)findViewById(R.id.add_photo_btn);
addPhotoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK
&& data != null && data.getData() != null) {
Uri uri = data.getData();
Photo newPhoto = new Photo("name", uri.getPath());
activeAlbum.photos.add(newPhoto);
activeAlbum.saveAlbum();
}
}
每个ListView单元格:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator">
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/photo_thumbnail"
android:layout_width="106dp"
android:layout_height="77dp" />
</LinearLayout>
<TextView
android:id="@+id/photo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Album Name"
android:textColor="#0984e3"
android:textSize="25dip"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>