2

this is my first time using recyclerview and i have problems to add a function when i call it i wan to delete items file from storage and recyclerview thanks for helping me

this is my first time using recyclerview and i have problems to add a function when i call it i wan to delete items file from storage and recyclerview thanks for helping me

this is the adapter class :

package net.simplifiedlearning.recyclerviewexample;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

public class ProductAdapter extends 
RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {


//this context we will use to inflate the layout
private Context mCtx;

//we are storing all the products in a list
private List<Product> productList;

//getting the context and product list with constructor
public ProductAdapter(Context mCtx, List<Product> productList) {
    this.mCtx = mCtx;
    this.productList = productList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //inflating and returning our view holder
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.layout_products, null);
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
    //getting the product of the specified position
    Product product = productList.get(position);

    //binding the data with the viewholder views
    holder.textViewTitle.setText(product.getTitle());
    holder.textViewShortDesc.setText(product.getShortdesc());
    holder.textViewRating.setText(String.valueOf(product.getRating()));
    holder.textViewPrice.setText(String.valueOf(product.getPrice()));

    holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));

}


@Override
public int getItemCount() {
    return productList.size();
}


class ProductViewHolder extends RecyclerView.ViewHolder {

    TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
    ImageView imageView;

    public ProductViewHolder(View itemView) {
        super(itemView);

        textViewTitle = itemView.findViewById(R.id.textViewTitle);
        textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
        textViewRating = itemView.findViewById(R.id.textViewRating);
        textViewPrice = itemView.findViewById(R.id.textViewPrice);
        imageView = itemView.findViewById(R.id.imageView);
    }
}

}

this is the main activity :

package net.simplifiedlearning.recyclerviewexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

//a list to store all the products
List<Product> productList;

//the recyclerview
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //getting the recyclerview from xml
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    //initializing the productlist
    productList = new ArrayList<>();


    //adding some items to our list
    productList.add(
            new Product(
                    1,
                    "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)",
                    "13.3 inch, Silver, 1.35 kg",
                    4.3,
                    60000,
                    R.drawable.macbook));

    productList.add(
            new Product(
                    1,
                    "Dell Inspiron 7000 Core i5 7th Gen - (8 GB/1 TB HDD/Windows 10 Home)",
                    "14 inch, Gray, 1.659 kg",
                    4.3,
                    60000,
                    R.drawable.dellinspiron));

    productList.add(
            new Product(
                    1,
                    "Microsoft Surface Pro 4 Core m3 6th Gen - (4 GB/128 GB SSD/Windows 10)",
                    "13.3 inch, Silver, 1.35 kg",
                    4.3,
                    60000,
                    R.drawable.surface));

    //creating recyclerview adapter
    ProductAdapter adapter = new ProductAdapter(this, productList);

    //setting adapter to recyclerview
    recyclerView.setAdapter(adapter);
}
}
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

1

To delete from Recycler and List use this code

public void removeAt(int position) {
    productList.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, productList.size());
}

And to implement it write removeAt(getAdapterPosition()); inside your onClickListener for example if you want to do it onClick If you have mp3 files and you want to delete one from your filder then initialize File myFile = new File(path). Then you can use .delete() method.For example, myFile.delete(); just like that

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • thanks bro the item delete from the liste but it not delete from my the storage i have a mp3 files show in listview – Oualid Oukassou Nov 24 '17 at 21:20
  • Updated my answer, you didn't put in your question that you have to delete from the folder too – Rainmaker Nov 25 '17 at 06:08
  • how can i get the path ? – Oualid Oukassou Nov 25 '17 at 19:21
  • in your code it looks like you have a list of merch(some gadgets). You don;t add mp3 to your recycler. Either we don"t have all the code or it's another question. How to delete (mp3) files programmatically. For the sake of people who read your code and don't see any storage writing just recycler I suggest you ask another question with the code where you actually write to storage or If you're interested just in quick solution see this link and the accepted answer https://stackoverflow.com/questions/3554722/how-to-delete-internal-storage-file-in-android – Rainmaker Nov 25 '17 at 19:35