我正在使用RecyclerView显示动态数据,目前,我正在从strings.xml文件中获取数据,但是我想从JSON中获取数据并使用AsyncTask在recyclerview中对其进行更新。谁能告诉我该怎么做?请帮我。
这是我的RecyclerAdapter.class
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
ArrayList<NewModel>newModels;
Context context;
public RecyclerAdapter(ArrayList<NewModel>newModels, Context context){
this.newModels = newModels;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recy1_layout, parent,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.imageView.setImageResource(newModels.get(position).getSociallogo());
holder.textView.setText(newModels.get(position).getSocailtext());
}
@Override
public int getItemCount() {
return newModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView =itemView.findViewById(R.id.thumbimg);
textView = itemView.findViewById(R.id.thumb_desc);
}
}
}
数据设置程序NewModel.class:
public class NewModel implements Parcelable {
int thumbimage;
String thumbtext;
public NewModel(int thumbimage,String thumbtext){
this.thumbimage= thumbimage;
this.thumbtext = thumbtext;
}
public int getSociallogo(){
return thumbimage;
}
public String getSocailtext(){
return thumbtext;
}
public NewModel(Parcel ins) {
// put your data using = in.readString();
this.thumbtext= ins.readString();
this.thumbimage = ins.readInt();
}
public static final Creator<NewModel> CREATOR = new Creator<NewModel>() {
@Override
public NewModel createFromParcel(Parcel ins) {
return new NewModel(ins);
}
@Override
public NewModel[] newArray(int size) {
return new NewModel[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(thumbtext);
parcel.writeInt(thumbimage);
}
}
MainActivity.java
TypedArray thumbimages =getResources().obtainTypedArray(R.array.thumnail_images);
String[] thumbdesc = getResources().getStringArray(R.array.thumbnail_desc);
newModels = new ArrayList<>();
for (int i =0; i<thumbimages.length(); i++){
NewModel newModel =new NewModel(thumbimages.getResourceId(i, 0), thumbdesc[i]);
newModels.add(newModel);
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false);
readapter= new RecyclerAdapter(newModels,getContext());
recyclerView1.setLayoutManager(linearLayoutManager);
recyclerView1.setItemAnimator(new DefaultItemAnimator());
recyclerView1.setAdapter(readapter);
问题来源:Stack Overflow
您必须从api获取数据,将其解析为ArrayList,然后使用notifyDataSetChanged进行更新。
private void fetchData() {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://url.com/api/data").build();
try {
//Parse data to newModels
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
System.out.print("End of content");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// notify adapter
readapter.notifyDataSetChanged();
}
};
task.execute(id);
}
}
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。