Java 如何从boyh recyclerview和storage android studio中删除项目
这是我第一次使用recyclerview,当我调用函数i wan从存储和recyclerview中删除项目文件时,在添加函数时遇到问题 谢谢你帮助我 这是我第一次使用recyclerview,当我调用函数i wan从存储和recyclerview中删除项目文件时,在添加函数时遇到问题 谢谢你帮助我 这是适配器类:Java 如何从boyh recyclerview和storage android studio中删除项目,java,android,android-recyclerview,Java,Android,Android Recyclerview,这是我第一次使用recyclerview,当我调用函数i wan从存储和recyclerview中删除项目文件时,在添加函数时遇到问题 谢谢你帮助我 这是我第一次使用recyclerview,当我调用函数i wan从存储和recyclerview中删除项目文件时,在添加函数时遇到问题 谢谢你帮助我 这是适配器类: package net.simplifiedlearning.recyclerviewexample; import android.content.Context; import
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);
}
}
package net.simplifiedlearning.recycleveriewExample;
导入android.content.Context;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.TextView;
导入java.util.List;
公共类ProductAdapter扩展
RecyclerView.适配器{
//我们将使用此上下文来扩大布局
私有上下文mCtx;
//我们正在将所有产品储存在一个列表中
私有列表产品列表;
//使用构造函数获取上下文和产品列表
公共产品适配器(上下文mCtx,列表productList){
this.mCtx=mCtx;
this.productList=productList;
}
@凌驾
公共产品ViewHolder onCreateViewHolder(视图组父级,int-viewType){
//膨胀并返回我们的视图持有者
LayoutFlater充气机=LayoutFlater.from(mCtx);
视图=充气机。充气(R.layout.layout\u产品,空);
返回新的ProductViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(ProductViewHolder,int位置){
//获取指定位置的产品
产品=产品列表。获取(位置);
//将数据与viewholder视图绑定
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());
}
@凌驾
public int getItemCount(){
返回productList.size();
}
类ProductViewHolder扩展了RecyclerView.ViewHolder{
TextView textViewTitle、textViewShortDesc、textViewRating、textViewPrice;
图像视图图像视图;
公共产品视图持有者(视图项视图){
超级(项目视图);
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);
}
}
}
这是主要活动:
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);
}
}
package net.simplifiedlearning.recycleveriewExample;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了AppCompatActivity{
//存储所有产品的列表
列出产品清单;
//回收视图
回收视图回收视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//从xml获取recyclerview
recyclerView=(recyclerView)findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
//初始化产品列表
productList=新的ArrayList();
//将一些项目添加到我们的列表中
productList.add(
新产品(
1.
“苹果MacBook Air Core i5第五代-(8 GB/128 GB SSD/Mac OS)”,
“13.3英寸,银色,1.35千克”,
4.3,
60000,
R.drawable.macbook);
productList.add(
新产品(
1.
“Dell Inspiron 7000 Core i5第七代-(8 GB/1 TB HDD/Windows 10 Home)”,
“14英寸,灰色,1.659公斤”,
4.3,
60000,
德林斯皮隆);
productList.add(
新产品(
1.
“Microsoft Surface Pro 4核m3第6代-(4 GB/128 GB SSD/Windows 10)”,
“13.3英寸,银色,1.35千克”,
4.3,
60000,
R.可拉伸表面);
//创建recyclerview适配器
ProductAdapter=新的ProductAdapter(此,productList);
//将适配器设置为recyclerview
recyclerView.setAdapter(适配器);
}
}
要从回收商中删除并列出,请使用此代码
public void removeAt(int position) {
productList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, productList.size());
}
要实现它,请编写removeAt(getAdapterPosition())例如,如果您想在onClick中执行此操作,请在onClick Listener中使用code>
如果您有mp3文件,并且希望从filder中删除一个,则初始化文件myFile=new File(路径)。然后您可以使用.delete()方法;就像这样谢谢兄弟从列表中删除项目,但它不会从我的存储中删除我在列表视图中显示了一个mp3文件更新了我的答案,你没有提出你必须从文件夹中删除的问题,我如何获得路径?在你的代码中,你似乎有一个merch(一些小工具)列表。你不知道;不要将mp3添加到你的回收器中。要么我们没有所有的代码,要么这是另一个问题。如何删除(mp3)以编程方式创建文件。对于那些阅读您的代码但没有看到任何存储写入只是回收的人,我建议您在代码中询问另一个问题,您实际写入存储的位置,或者如果您对快速解决方案感兴趣,请参阅此链接和公认的答案