如何使用图像填充和维护listView?

时间:2018-04-19 21:31:13

标签: android android-layout listview imageview

我想设计一个listView,这样当用户添加照片时,它会自动使用图像名称及其缩略图更新listView。然而,我目前的尝试甚至不能渲染图像。是否可以为listView的每个单元格动态设置ImageView,并在向适配器添加新元素时更新它?

用于listView和添加按钮的XML:

[object Object]
[object Object]  
[object Object]
[object Object]
[object Object]

的活动:

<?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>

每个ListView单元格:

    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();
            }
        }

3 个答案:

答案 0 :(得分:2)

当您在OnActivityResult()上获得结果时,将其添加到currentPhotos, 然后调用notifyDataSetChanged()

答案 1 :(得分:2)

首先,我认为album.photo与创建适配器(currentPhotos)时传递的数组不是同一个数组。因此,将新照片添加到album.photo并不会反映listView中的该项目。

其次,正如用户开发人员指出的那样,每当您更改数据时,您需要通过adapter.notifyDataSetChanged()告诉适配器。但是在你的情况下,你总是添加到最后我认为最好拨打adapter.notifyDataInserted(currentPhotos.size() - 1)

答案 2 :(得分:1)

如果您正在处理本地存储或网络上的图像。使用像implementation 'com.squareup.picasso:picasso:2.71828'之类的lib可能是一个好主意,因为它可以处理基于图像和URL的异常,并且可以更容易地打开应用程序,因此可以最小化地使用数据。