如何更新RecyclerView适配器数据(视频获取存储)

时间:2017-11-04 07:46:19

标签: java android video

我正在制作一个视频项目,我想显示一个特定于存储视频的活动,并获取下载文件夹中的所有视频并显示它。

这是我的视频终端和Adapter_VideoFolder和model_video文件

运行应用程序下载文件夹时所有视频显示和 点击"删除按钮"特定视频和使用Adapter_VideoFolder删除文件并更改notifydataadpter

  Adapter_VideoFolder.this.notifyDataSetChanged();

使用此换行适配器数据和删除文件消失

但这里的问题是当我关闭应用程序并再次打开时, 即使下载文件夹文件已被删除,也会再次显示已删除的文件

所以请帮助我

Model_Video.java

public class Model_Video {
String str_path,str_thumb;
boolean boolean_selected;

public String getStr_path() {
    return str_path;
}

public void setStr_path(String str_path) {
    this.str_path = str_path;
}

public String getStr_thumb() {
    return str_thumb;
}

public void setStr_thumb(String str_thumb) {
    this.str_thumb = str_thumb;
}

public boolean isBoolean_selected() {
    return boolean_selected;
}

public void setBoolean_selected(boolean boolean_selected) {
    this.boolean_selected = boolean_selected;
}}

Adapter_VideoFolder.java

public class Adapter_VideoFolder extends RecyclerView.Adapter<Adapter_VideoFolder.ViewHolder> 
{




ArrayList<Model_Video> al_video;
Context context;
Activity activity;

public String getPackageName(Context context) 
{
    return context.getPackageName();
}

public Adapter_VideoFolder(Context context, ArrayList<Model_Video> al_video, Activity activity) 
{
    this.al_video = al_video;
    this.context = context;
    this.activity = activity;
}

public static class ViewHolder extends RecyclerView.ViewHolder 
{
    public ImageView iv_image;
    RelativeLayout rl_select;
    public Button b3;

    public ViewHolder(View v) 
    {
        super(v);
        iv_image = (ImageView) v.findViewById(R.id.iv_image);
        rl_select = (RelativeLayout) v.findViewById(R.id.rl_select);
        b3 =(Button) v.findViewById(R.id.button2);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_videos, parent, false);
    ViewHolder viewHolder1 = new ViewHolder(view);
    return viewHolder1;
}

@Override
public void onBindViewHolder(final ViewHolder Vholder, final int position)
{
    Glide.with(context).load("file://" + al_video.get(position).getStr_thumb()).skipMemoryCache(false).into(Vholder.iv_image);
    Vholder.rl_select.setBackgroundColor(Color.parseColor("#FFFFFF"));
    Vholder.rl_select.setAlpha(0);

    Vholder.b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder dialog = new AlertDialog.Builder(Adapter_VideoFolder.this.activity);
            dialog.setCancelable(false);
            dialog.setTitle("Dialog on Android");
            dialog.setMessage("Are you sure you want to delete this entry?" );
            dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() 
            {
                @Override
                public void onClick(DialogInterface dialog, int id) 
                {

                    File file = new File(al_video.get(position).getStr_path());
                    file.delete();
                    Adapter_VideoFolder.this.al_video.remove(position);
                    Adapter_VideoFolder.this.notifyDataSetChanged();

                }
            }).setNegativeButton("Cancel ", new DialogInterface.OnClickListener() 
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {

                }
            });
            final AlertDialog alert = dialog.create();
            alert.show();
        }
    });
}
@Override
public int getItemCount() {
    return al_video.size();

}}

Videofolder.java

public class videofolder extends AppCompatActivity{

ArrayList<Model_Video> al_video = new ArrayList<>();
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewLayoutManager;
private static final int REQUEST_PERMISSIONS = 100;

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

    init();
}

private void init()
{
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
    recyclerViewLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
    recyclerView.setLayoutManager(recyclerViewLayoutManager);
    fn_video();
}


public void fn_video() {

    int int_position = 0;
    Uri uri;
    Cursor cursor;
    int column_index_data, column_index_folder_name,column_id,thum;

    String absolutePathOfImage = null;
    uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

    String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME,MediaStore.Video.Media._ID,MediaStore.Video.Thumbnails.DATA};


    String selection=MediaStore.Video.Media.DATA +" like?";
    String[] selectionArgs=new String[]{"%Download%"};


    final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
    cursor = getApplicationContext().getContentResolver().query(uri, projection, selection, selectionArgs, orderBy + " DESC");

    column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
    column_id = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
    thum = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);

    while (cursor.moveToNext()) {
        absolutePathOfImage = cursor.getString(column_index_data);
        Log.e("Column", absolutePathOfImage);
        Log.e("Folder", cursor.getString(column_index_folder_name));
        Log.e("column_id", cursor.getString(column_id));
        Log.e("thum", cursor.getString(thum));

        Model_Video obj_model = new Model_Video();
        obj_model.setBoolean_selected(false);
        obj_model.setStr_path(absolutePathOfImage);
        obj_model.setStr_thumb(cursor.getString(thum));

        al_video.add(obj_model);

        obj_adapter = new Adapter_VideoFolder(getApplicationContext(),al_video,videofolder.this);
        obj_adapter.notifyDataSetChanged();
        recyclerView.setAdapter(obj_adapter);


    }

}}

0 个答案:

没有答案