我在Recycler View中显示服务器信息时遇到问题

时间:2019-09-24 15:18:21

标签: android android-studio

我想编写一个程序来从服务器检索信息并在Recycler View中显示它,但是我有两个问题。

  1. 当我将数据添加到表中时,它将被添加到“ Recycler View”列表中,但是当我删除数据时,列表不会更改。

2。无法加载每个部分的照片。

home.java

   public class Home extends Fragment {


public Home() {
    // Required empty public constructor
}

SwipeRefreshLayout srl;
RecyclerView rv;
FloatingActionButton add;
ArrayList<Post> al;
PostAdapter pa;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_home, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    srl = getActivity().findViewById(R.id.srl);
    rv = getActivity().findViewById(R.id.rv);
  //  add = getActivity().findViewById(R.id.add);

    AndroidNetworking.initialize(getContext());

    al = new ArrayList<>();
    pa = new PostAdapter(getContext(),al);

    rv.setHasFixedSize(true);
    rv.setItemAnimator(new DefaultItemAnimator());
    rv.setLayoutManager(new LinearLayoutManager(getContext()));

    rv.setAdapter(pa);

    update();

    srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            update();
        }
    });


}
private void update(){
    String id = al.size() == 0 ? "0" : al.get(0).getId();

    AndroidNetworking.post(Urls.host+Urls.post)
            .addBodyParameter("id",id)
            .build()
            .getAsObjectList(Post.class, new ParsedRequestListener<List<Post>>() {
                @Override
                public void onResponse(List<Post> response) {
                    srl.setRefreshing(false);
                    for (Post p : response){
                        al.add(0,p);
                        pa.notifyDataSetChanged();
                    }
                }

                @Override
                public void onError(ANError anError) {
                    srl.setRefreshing(false);
                }
            });


}

}

postadapter.java

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

Context context;
ArrayList<Post> al;
LayoutInflater inflater;

public PostAdapter(Context ctx,ArrayList<Post> arl){
    context = ctx;
    al = arl;
    inflater = LayoutInflater.from(context);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.rv_item,null,false);

    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(lp);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Post s = al.get(position);

    holder.title.setText(s.getTitle());
    holder.text.setText(s.getText());
    holder.date.setText(s.getDate());
    holder.image.setImageUrl(Urls.host+s.getImage());
    holder.image.setDefaultImageResId(R.drawable.material);
   holder.image.setErrorImageResId(R.drawable.material);


}

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

class ViewHolder extends RecyclerView.ViewHolder{

    ANImageView image;
    AppCompatTextView title, text, date;
    AppCompatImageView share;

    public ViewHolder(View itemView) {
        super(itemView);

        image = itemView.findViewById(R.id.image);
        title = itemView.findViewById(R.id.title);
        text = itemView.findViewById(R.id.text);
        date = itemView.findViewById(R.id.date);


    }
}

}

post.java

  public class Post {
String title,text, date,id;
int imageUrl;
public Post(int imageUrl, String title, String text, String time, String id) {
    this.imageUrl = imageUrl;
    this.title = title;
    this.text = text;
    this.date = time;
    this.id = id;
}

public int getImage() {
    return imageUrl;
}

public void setImage(int imageUrl) {
    this.imageUrl = imageUrl;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

1 个答案:

答案 0 :(得分:0)

尝试

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

Context context;
ArrayList<Post> al;
ArrayList<Post> newDataList;
LayoutInflater inflater;

public PostAdapter(Context ctx,ArrayList<Post> arl){
    context = ctx;
    al = arl;
    inflater = LayoutInflater.from(context);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.rv_item,null,false);

    RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    view.setLayoutParams(lp);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Post s = al.get(position);

    holder.title.setText(s.getTitle());
    holder.text.setText(s.getText());
    holder.date.setText(s.getDate());
    holder.image.setImageUrl(Urls.host+s.getImage());
    holder.image.setDefaultImageResId(R.drawable.material);
   holder.image.setErrorImageResId(R.drawable.material);


}

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

class ViewHolder extends RecyclerView.ViewHolder{

    ANImageView image;
    AppCompatTextView title, text, date;
    AppCompatImageView share;

    public ViewHolder(View itemView) {
        super(itemView);

        image = itemView.findViewById(R.id.image);
        title = itemView.findViewById(R.id.title);
        text = itemView.findViewById(R.id.text);
        date = itemView.findViewById(R.id.date);


    }


 public ArrayList<Post> getDataSet() {
        return al;
    }

   public void refresh(ArrayList<Post> list) {
        newDataList = new ArrayList<>();
        newDataList.addAll(list);
        al.clear();
        al.addAll(newDataList);
        notifyDataSetChanged();
    }


}

现在当您想添加或删除时,尝试获取adapter.getDataSet() 并使用adapter.refresh(yourList)

更新列表