代码之家  ›  专栏  ›  技术社区  ›  user1775500

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

  •  0
  • user1775500  · 技术社区  · 7 年前

    我想设计一个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>
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   user8077377    7 年前

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

    其次,正如用户dev所指出的,无论何时更改数据,都需要通过 adapter.notifyDataSetChanged() .然而,就你的情况而言,你总是在结尾加上我认为最好打电话 adapter.notifyDataInserted(currentPhotos.size() - 1)

        2
  •  2
  •   dev    7 年前

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

        3
  •  1
  •   Racer    7 年前

    如果您正在处理本地存储或web上的图像。使用lib-like implementation 'com.squareup.picasso:picasso:2.71828' 这可能是一个好主意,因为它可以处理基于图像和URL的异常,并且可以捕获数据,以便更容易地打开应用程序,并且可以最小限度地使用内存。