Я не знаю, почему функция удаления перестала работать после того, как я реализовал базу данных searchview в своем классе java. Одна из этих функций будет работать, если я удалю одну из каждой функции (Поиск или Удалить). Пожалуйста, ребята, помогите мне решить эти проблемы. В этом проекте я также реализую onitemclicklistener как выбор для удаления данных.
ImagaAdapter.Class
package com.example.lasttic;
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context mContext;
private ArrayList<DatabaseImage> mUploads;
private ArrayList<DatabaseImage> arraylistFiltered;
private OnItemCLickListener mListener;
public ImageAdapter(Context context,ArrayList<DatabaseImage> uploads)
{
mContext = context;
mUploads = uploads;
arraylistFiltered = new ArrayList<> (mUploads);
}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.image_item,parent,false);
ImageViewHolder viewHolder = new ImageViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
final DatabaseImage uploadCur = mUploads.get(position);
holder.imageName.setText(uploadCur.getImgName());
Picasso.get().load(uploadCur.getImgUrl())
.placeholder(R.drawable.imagepreview)
.fit()
.centerCrop()
.into(holder.imageUpload);
holder.Detailkk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,TicketDetail.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
mContext.startActivity(intent);
}
});
/* holder.imageUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,ViewTicketImage.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
intent.putExtra("img_url", uploadCur.getImgUrl());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
mContext.startActivity(intent);
}
}); */
}
@Override
public int getItemCount() {
return mUploads.size();
}
public Filter getFilter()
{
return exampleFilter;
}
private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<DatabaseImage> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(arraylistFiltered);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (DatabaseImage item : arraylistFiltered) {
if (item.getImgName().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
mUploads.clear();
mUploads.addAll((List)results.values);
notifyDataSetChanged();
}
};
public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener
{
public TextView imageName;
public ImageView imageUpload;
public Button Detailkk;
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
imageName = itemView.findViewById(R.id.imageName);
imageUpload = itemView.findViewById(R.id.imageUpload);
Detailkk = itemView.findViewById(R.id.detail22);
itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
}
@Override
public void onClick (View v)
{
if (mListener != null)
{
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION)
{
mListener.onitemClick(position);
}
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select Action");
MenuItem delete = menu.add(Menu.NONE,1,1,"Delete");
MenuItem click = menu.add(Menu.NONE,2,2,"ViewImage");
delete.setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
if (mListener != null)
{
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION)
{
switch (item.getItemId())
{
case 1:
mListener.onDeleteClick(position);
return true;
case 2:
mListener.onitemClick(position);
return true;
}
}
}
return false;
}
}
public long getItemId(int position)
{
return position;
}
public interface OnItemCLickListener
{
void onitemClick (int position);
void onDeleteClick (int position);
}
public void setOnItemClickListener (OnItemCLickListener listener)
{
mListener = listener;
}
}
viewImageUploadClass
public class viewImageUpload extends AppCompatActivity implements ImageAdapter.OnItemCLickListener{
private RecyclerView mRecyleView;
private ImageAdapter mAdapter;
private ArrayList<DatabaseImage> mUploads;
private FirebaseStorage mStorage;
private SearchView mSsearch;
private ProgressBar progressBar;
private ValueEventListener mDBListener;
private DatabaseReference mDatabaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_image_upload);
mRecyleView = findViewById(R.id.recycleviewimage);
mRecyleView.setHasFixedSize(true);
mRecyleView.setLayoutManager(new LinearLayoutManager(this));
progressBar = (ProgressBar) findViewById(R.id.progress_circular);
mUploads = new ArrayList<>();
mAdapter = new ImageAdapter(viewImageUpload.this, mUploads);
mRecyleView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(viewImageUpload.this);
mStorage = FirebaseStorage.getInstance();
mDatabaseReference = FirebaseDatabase.getInstance().getReference("uploads");
mDBListener = mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUploads.clear();
for (DataSnapshot postSnapShot : dataSnapshot.getChildren()) {
DatabaseImage upload = postSnapShot.getValue(DatabaseImage.class);
upload.setkeys(postSnapShot.getKey());
mUploads.add(upload);
}
mAdapter = new ImageAdapter(getBaseContext(),mUploads);
mRecyleView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(viewImageUpload.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.INVISIBLE);
}
});
mSsearch = (SearchView)findViewById(R.id.mSearch);
mSsearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if(mAdapter!=null){
mAdapter.getFilter().filter(newText);
}
return false;
}
});
}
@Override
public void onitemClick(int position) {
Toast.makeText(this,"Hello",Toast.LENGTH_SHORT).show();
}
@Override
public void onDeleteClick(int position) {
DatabaseImage selectedItem = mUploads.get(position);
final String selectedKey = selectedItem.getkeys();
StorageReference imageRef = mStorage.getReferenceFromUrl(selectedItem.getImgUrl());
imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mDatabaseReference.child(selectedKey).removeValue();
Toast.makeText(viewImageUpload.this,"Delete Successfully",Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mDatabaseReference.removeEventListener(mDBListener);
}
}