如何显示数据从ArrayList到RecycleView?

时间:2020-07-23 03:42:35

标签: android android-recyclerview android-json

我像这样的代码从JSON读取数据到ArrayList。

MySQLConnect.java

public List<ComputerService> getData(){
    String url = URL + GET_URL;
    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            showJSON(response);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(main, error.getMessage().toString(), LENGTH_LONG).show();
        }
    }

    );

    RequestQueue requestQueue = Volley.newRequestQueue(main.getApplicationContext());
    requestQueue.add(stringRequest);

    return computerServicesArrayList;
}

public void showJSON(String response){
    String data_mysql = "";
    computerServicesArrayList.clear();
    try{
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray("data");

        for(int i=0; i < result.length(); i++){
            JSONObject collectData = result.getJSONObject(i);
            String id_job = collectData.getString("id_job");
            String type_job = collectData.getString("type_job");
            String place_more_job = collectData.getString("place_more_job");

            computerservice = new ComputerService(id_job, type_job, place_more_job);
            computerServicesArrayList.add(computerservice);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

在MainActitvity中,我将这样的数据从MySQLConnect设置为Myadapter。

private  MyAdapter myAdapter;
private MySQLConnect mySQLConnect;
public static List<ComputerService> computerServicesArrayList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtAdd = (EditText)findViewById(R.id.txtAdd);
    btnAdd = (Button)findViewById(R.id.btnAdd);
    rcyViewData = (RecyclerView)findViewById(R.id.rcyViewData);

    mySQLConnect = new MySQLConnect(MainActivity.this);
    computerServicesArrayList =  mySQLConnect.getData();
    mAdapter = new MyAdapter(this, computerServicesArrayList);
}

在我的适配器中,我尝试像这样将数据从JSON传输到RecycleView。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    List<ComputerService> computerServicesArrayList;
    Context context;

    public MyAdapter(Context context, List<ComputerService> computerServicesArrayList) {
        this.context = context;
        this.computerServicesArrayList = computerServicesArrayList;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.myText1.setText(computerServicesArrayList.toString());
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView myText1, myText2;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            myText1 = itemView.findViewById(R.id.txtTitle);
            myText2 = itemView.findViewById(R.id.txtDetail);

        }
    }
}

在MyAdapter函数的onBindViewHolder中,我不知道如何将数据显示给holder.myText1.setText()。

如何将ArraList数据显示到RecycleView?

2 个答案:

答案 0 :(得分:0)

getItemCount()返回0会告诉适配器您没有要显示的项目,并且不会触发onCreateViewHolder或onBindViewHoldergetItemCount()应该返回computerServicesArrayList.size以显示数据

答案 1 :(得分:0)

您的适配器getItemCount始终返回0

做这样的事情:

@Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ComputerService obj = computerServicesArrayList.get(position));
        holder.myText1.setText(obj.getTypeJob());
        holder.myText2.setText(obj.getJobId());

    }

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

您的ComputerService.java模型:

public class ComputerService{
    String jobId;
    String typeJob;
    String placeModeJob;
    
    ComputerService(String jobId,String typeJob,String placeModeJob) {
     this.jobId = jobId;
     this.typeJob = typeJob;
     this.placeModeJob = placeModeJob;
    }

    public String getJobId() {
        return jobId;
    }

    public String getPlaceModeJob() {
        return placeModeJob;
    }

    public String getTypeJob() {
        return typeJob;
    }
}