我在android v7中的recycleview有时会显示列表。当我从手机重启应用程序时,它不显示列表。它可以计算所有音频文件,但是当我将其设置为textview时,它只显示列表视图中添加的最后一个数据。我的代码如下。有人可以告诉我如何使用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);
}
}
答案 0 :(得分:0)
我认为最好的办法是将异步任务从适配器中移出,然后在活动的onCreate
回调中启动它。异步任务完成后,您将创建适配器并将其提供给recyclerview。