如何使用RecyclerView获取http://jsonplaceholder.typicode.com/users

时间:2017-08-16 08:21:05

标签: java android android-recyclerview retrofit2 recycler-adapter

我想提取数据http://jsonplaceholder.typicode.com/users,但我的代码只能找到一个人(姓名:Leanne Graham,用户名:Bret)我希望得到所有用户。我创建了ServiceGenerator.java Call> placeHolderModel();也许这段代码可能会出错。

MainActivity.java

 public class MainActivity extends AppCompatActivity {

 TextView textView ;
 RecyclerView recyclerView ;
 SimpleRecyclerAdapter simpleRecyclerAdapter ;
 ArrayList<Person> arrayList ;
 private LinearLayoutManager layoutManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textView);

    layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(layoutManager);
    arrayList=new ArrayList<Person>();


    Factory.getInstance().placeHolderModel().enqueue(new Callback<List<PlaceHolderModel>>() {
        @Override
        public void onResponse(Call<List<PlaceHolderModel>> call, Response<List<PlaceHolderModel>> response) {
            textView.setText(response.body().get(0).name);
            simpleRecyclerAdapter= new SimpleRecyclerAdapter(response.body());
            recyclerView.setAdapter(simpleRecyclerAdapter);



        }

        @Override
        public void onFailure(Call<List<PlaceHolderModel>> call, Throwable t) {

        }
    });


}
 }

SimpleRecyclerAdapter.java

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

public static class ViewHolder extends RecyclerView.ViewHolder{
    public TextView isim ;
    public TextView soyIsim ;
    public CardView cardView ;

    public ViewHolder(View itemView) {
        super(itemView);
        isim = itemView.findViewById(R.id.textIsım);
        soyIsim=itemView.findViewById(R.id.textSoyIsim);
        cardView = itemView.findViewById(R.id.cardView);

    }
}


List<PlaceHolderModel> placeHolderModel;

public SimpleRecyclerAdapter(List<PlaceHolderModel> placeHolderModel) {
    this.placeHolderModel = placeHolderModel;
}

@Override
public SimpleRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_custom,parent,false);
    ViewHolder viewHolder = new ViewHolder(v);
    return  viewHolder ;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.isim.setText(placeHolderModel.get(position).name);
    holder.soyIsim.setText(placeHolderModel.get(position).username);
}

@Override
public int getItemCount() {
    return placeHolderModel.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

ServiceGenerator.java

public interface ServiceGenerator {
@GET("/users")
Call<List<PlaceHolderModel>> placeHolderModel();

  }

非常感谢!

您的建议对我很重要!

0 个答案:

没有答案