ListView Adapter 总是删除最后一行

ListView Adapter always removes the last row

因此,例如当我 运行 ListViewAdapter.remove(ListViewAdapter.getItem(position)) 呈现时,它总是通过删除最后一行来重新呈现,即使位置参数总是不同的。如果我有 4 行并且我尝试删除第三行,位置是 3,但没关系,它删除了最后一行。 我必须重新打开 activity 才能看到正确的结果?这是为什么?

    private ListView videoList;
    private VideoListAdapter videoListAdapter;

我的方法看起来像这样

videoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                parentViewPosition = position;

                view.findViewById(R.id.deleteButton).setVisibility(View.VISIBLE);
                onButtonCliCk(view);
                makeButtonInvisible(view);
                return true;
            }
        });
    public void deleteElement(final String fileName) {
        folderPath.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
            @Override
            public void onSuccess(ListResult listResult) {
                for (StorageReference storageReference : listResult.getItems()) {
                    if (storageReference.getName().equals(fileName)) {
                        storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(getApplicationContext(), "Element sters cu succes",
                                        Toast.LENGTH_LONG).show();

                                updateListViewAdapter(parentViewPosition);

                                deleteDatabaseDownloadReference(mDatabase);
                                uploadAllDownloadURIS();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                System.out.println("STERGEREA A ESUAT");
                            }
                        });
                    }
                }
            }
        });
    }

这是我的适配器

package com.example.navigation.ViewVideos;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.navigation.R;
import com.google.firebase.storage.StorageReference;

import java.util.ArrayList;
import java.util.Objects;

public class VideoListAdapter extends ArrayAdapter<StorageReference> {

    private static final String TAG = "VideoListAdapter";

    private Context mContext;

    private int mResource;

    public VideoListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<StorageReference> objects) {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        LayoutInflater inflater = LayoutInflater.from(mContext);

        if (vi == null) {
            String folderName = Objects.requireNonNull(getItem(position), "folderName must not be null").getName();
            String bucket = Objects.requireNonNull(getItem(position), "bucket must not be null").getBucket();
            String path = Objects.requireNonNull(getItem(position)).getPath();
            vi = inflater.inflate(mResource, parent, false);
            holder = new ViewHolder();

            holder.folderName = (TextView) vi.findViewById(R.id.videoTextView);
            holder.folderName.setText(folderName);

            holder.bucket = (TextView) vi.findViewById(R.id.videoTextView2);
            holder.bucket.setText(bucket);

            holder.path = (TextView) vi.findViewById(R.id.videoTextView3);
            holder.path.setText(path);


        } else {
            holder = (ViewHolder) vi.getTag();
        }

        return vi;
    }

    private static class ViewHolder {
        private TextView folderName;
        private TextView bucket;
        private TextView path;
    }

}

这是我为更新 ListView 而调用的方法

   public void updateListViewAdapter(int parentViewPosition){
        videoListAdapter.remove(videoListAdapter.getItem(parentViewPosition));
        videoListAdapter.notifyDataSetChanged();
    }

试试这个:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View vi = convertView;
    ViewHolder holder;

    if (vi == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        vi = inflater.inflate(mResource, parent, false);
        holder = new ViewHolder();
        holder.folderName = (TextView) vi.findViewById(R.id.videoTextView);
        holder.bucket = (TextView) vi.findViewById(R.id.videoTextView2);
        holder.path = (TextView) vi.findViewById(R.id.videoTextView3);

        vi.setTag(holder);
    } else {
        holder = (ViewHolder) vi.getTag();
    }

    String folderName = Objects.requireNonNull(getItem(position), "folderName must not be null").getName();
    String bucket = Objects.requireNonNull(getItem(position), "bucket must not be null").getBucket();
    String path = Objects.requireNonNull(getItem(position)).getPath();
    holder.folderName.setText(folderName);
    holder.bucket.setText(bucket);
    holder.path.setText(path);

    return vi;
}

没有 ViewHolder:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

if (convertView == null) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);
}

TextView folderName = (TextView) convertView.findViewById(R.id.videoTextView);
TextView bucket = (TextView) convertView.findViewById(R.id.videoTextView2);
TextView path = (TextView) convertView.findViewById(R.id.videoTextView3);

String folderName = Objects.requireNonNull(getItem(position), "folderName must not be null").getName();
String bucket = Objects.requireNonNull(getItem(position), "bucket must not be null").getBucket();
String path = Objects.requireNonNull(getItem(position)).getPath();
folderName.setText(folderName);
bucket.setText(bucket);
path.setText(path);

return convertView;
}