[英]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.