2017-11-24 3 views
2

이것은 처음으로 recyclerview를 사용하는 것이며 저장소 및 recyclerview에서 항목 파일을 삭제하려고 할 때 함수를 추가하는 데 문제가 있습니다. 나를 도와 주셔서 감사합니다.boyh recyclerview 및 storage android studio에서 항목을 삭제하는 방법

내가 항목을 삭제 스토리지에서 파일을 저에게

이를 도와 감사 recyclerview 완 호출하는 기능을 추가하는 문제를이 내 처음 recyclerview을 사용하고 내가해야하는 어댑터 클래스입니다 :

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); 
    } 
} 
012 3,516,

}

이 주요 활동 :

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); 
} 
} 

답변

0

가 휴지통에서 삭제하고 목록이 코드

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

그리고 당신이 원하는 경우 예를 들어 당신의 OnClickListener를 내부 removeAt(getAdapterPosition()); 쓰기 구현을 사용하여 onClick MP3 파일이 있고 파일러에서 파일을 삭제하려면 myFile = new File (path) 파일을 초기화하십시오. 그런 다음 .delete() 메서드를 사용할 수 있습니다. 예를 들어, myFile.delete(); 그 것처럼

+0

감사합니다. liste에서 항목을 삭제했지만 저장소에서 삭제되지 않았습니다. 목록보기에 mp3 파일이 있습니다. –

+0

내 대답이 업데이트되었습니다. 질문에서 삭제해야 할 내용을 입력하지 않았습니다. 폴더도 – Rainmaker

+0

저장 용량을 읽을 수있는 권한을 잊지 마세요. – Rainmaker