简体   繁体   English

我的Recycleview显示列表和完整列表仅间歇地显示我添加到ArrayList的最后一项

[英]My Recycleview display list and complete list only intermittently displays the last item I add to my ArrayList

My recycleview in android v7 displays the list sometimes. 我在android v7中的recycleview有时会显示列表。 It doesn't display the list when I restart the app from my phone. 从手机重新启动应用程序时,它不会显示列表。 It can count all the audio files but when I set it to textview, it just displays only the last data added in the list view. 它可以计算所有音频文件,但是当我将其设置为textview时,它仅显示列表视图中添加的最后一个数据。 My code is below. 我的代码如下。 Can someone tell me how to display Recycle list data concurrently with using the Androi's new AsyncListUtil. 有人可以告诉我如何使用Androi的新AsyncListUtil同时显示回收列表数据。

//MainActivity.java
package fragment.dev.concurrentmedia;

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

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    RecAdapter adapter;

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

        recyclerView = (RecyclerView) findViewById (R.id.recycleview);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager (getApplicationContext ());
        recyclerView.setLayoutManager (layoutManager);
        recyclerView.setItemAnimator (new DefaultItemAnimator ());
        recyclerView.setHasFixedSize (true);

        adapter = new RecAdapter (getApplicationContext ());
        recyclerView.setAdapter (adapter);
    }
}

//AudioSync.java
package fragment.dev.concurrentmedia;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by Jagdish on 6/19/2016.
 */
public class AudioSync extends AsyncTask<Context, Void, List<AudioModel>> {
    public Response delegate = null;
    private String selection;
    private Uri uri;
    private Cursor cursor;
    @Override
    protected List<AudioModel> doInBackground (Context... params) {
        List<AudioModel> modelList = new ArrayList<> ();
        AudioModel model = new AudioModel ();
        try {
            selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
            uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String projection[] = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION};
            cursor = params[0].getContentResolver ().query (uri, projection, selection, null, null);
            cursor.moveToFirst ();
            int i = 0;
            do {
                model.setPath (cursor.getString (cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DATA)));
                model.setDisplayName (cursor.getString (cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DISPLAY_NAME)));
                model.setDuration (cursor.getString (cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DURATION)));
                modelList.add (model);
            } while (cursor.moveToNext ());
            return modelList;
        } catch (Exception e) {
            L.l ("EXCEPTION", e.getStackTrace ());
            return modelList;
        }
    }
    @Override
    protected void onPostExecute (List<AudioModel> audioModels) {
        delegate.processFinish (audioModels);
    }
    public interface Response {
        void processFinish (List<AudioModel> output);
    }
}

//RecAdapter.java
package fragment.dev.concurrentmedia;

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.TextView;

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

/**
 * Created by Jagdish on 6/19/2016.
 */
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.MyViewHolder> implements AudioSync.Response {
    public List<AudioModel> models = new ArrayList<> ();

    public RecAdapter (Context context) {
        AudioSync sync = new AudioSync ();
        sync.delegate = this;
        sync.execute (context);
    }

    @Override
    public MyViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from (parent.getContext ()).inflate (R.layout.recycle_layout, parent, false);
        return new MyViewHolder (itemView);
    }

    @Override
    public void onBindViewHolder (MyViewHolder holder, int position) {
        AudioModel audioModel = models.get (position);
        holder.txtName.setText (audioModel.getDisplayName ());
        holder.txtDuration.setText (audioModel.getDuration ());
    }

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

    @Override
    public void processFinish (List<AudioModel> output) {
        for (int i = 0; i < output.size (); i++) {
            models.add (output.get (i));
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView txtName;
        public TextView txtDuration;

        public MyViewHolder (View itemView) {
            super (itemView);
            txtName = (TextView) itemView.findViewById (R.id.txtName);
            txtDuration = (TextView) itemView.findViewById (R.id.txtDuration);
        }
    }
}

//AudioModel.java
package fragment.dev.concurrentmedia;

/**
 * Created by Jagdish on 6/19/2016.
 */
public class AudioModel {
    private String displayName;
    private String duration;
    private String path;

    public String getDisplayName () {
        return displayName;
    }

    public void setDisplayName (String displayName) {
        this.displayName = displayName;
    }

    public String getDuration () {
        return duration;
    }

    public void setDuration (String duration) {
        this.duration = duration;
    }

    public String getPath () {
        return path;
    }

    public void setPath (String path) {
        this.path = path;
    }
}

//L.java
package fragment.dev.concurrentmedia;

import android.util.Log;

/**
 * Created by Jagdish on 6/19/2016.
 */
public class L {
    public static void l (String detail, Object msg) {
        Log.d ("JR", detail + " : " + msg);
    }
}

I think the best thing here is to move the async task out from the adapter and start it in the activity's onCreate callback. 我认为最好的办法是将异步任务从适配器中移出,并在活动的onCreate回调中启动它。 As soon as the async task completes, you create the adapter and provide it to the recyclerview. 异步任务完成后,就可以创建适配器并将其提供给recyclerview。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的Hashmap对象值列表对象仅返回我放在ArrayList中的最后一个对象 <Map<String,Object> &gt;&gt;(); - My Hashmap object value list object return only last object i was put in ArrayList<Map<String,Object>>>(); 如何将显示方法添加到Java链表实现中,该实现仅显示列表中的前10个项和后10个项? - How to add a display method to a Java Linked List implementation that displays only the first and the last 10 items on the list? RecycleView在开始时仅显示某些项目,之后在添加新项目时不显示它们吗? - RecycleView displays only some item in the beginning, after that when I add new item is not showing them? 使用我的editText,我想从数组列表中查看保存的项目列表,并显示有关该项目的几行详细信息 - using my editText i want to see saved list of items from my arraylist and display a couple of lines of detail about the item 对话框仅显示数组列表中的最后一项 - dialog box displays only the last item in the array list 我的ArrayList无法识别我添加到列表中的内容 - My ArrayList won't recognize what I add to the list 为什么我的 ArrayList 包含最后添加到列表中的项目的 N 个副本? - Why does my ArrayList contain N copies of the last item added to the list? 如何为列表中的每个项目添加文本视图? - How can I add a text view for every item in my list? 如何为我的列表项添加附加值 - How do I add additional value to my list item 无法显示我的列表视图中的项目 - Cant display the item of my List View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM