So for example when I run ListViewAdapter.remove(ListViewAdapter.getItem(position))
when it renders it always rerenders by deleting the last row, even though the position parameter is always different. If I have 4 rows and i try to delete the third row, the position is 3, but it doesn't matter, it deletes the last row.
I have to reopen the activity in order to see the correct result? Why is that?
private ListView videoList;
private VideoListAdapter videoListAdapter;
My methods look something like this
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");
}
});
}
}
}
});
}
And this is my adapter
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;
}
}
And this is the method I call in order to update the ListView
public void updateListViewAdapter(int parentViewPosition){
videoListAdapter.remove(videoListAdapter.getItem(parentViewPosition));
videoListAdapter.notifyDataSetChanged();
}
Try this:
@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;
}
Without 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;
}