我想用JSON数组加载我的recyclerview。我为此使用排球。
到目前为止,我正在使用StringRequest
。
我要发布ID,作为回应,我将获得JSON数组。
我收到响应,但是我的ArrayList
显示错误
statuslist ='this' is not available
下面是我的代码。
private ArrayList<Main_lead_data> statusList = new ArrayList<>();
private RecyclerView mainleadRecycler;
private Main_lead_Adpater mainleadRecycler_Adpter;
mainleadRecycler = findViewById(R.id.idRecyclerViewHorizontalList);
mainleadRecycler.addItemDecoration(new DividerItemDecoration(MainActivity.this, LinearLayoutManager.HORIZONTAL));
mainleadRecycler_Adpter = new Main_lead_Adpater(this,statusList);
LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
mainleadRecycler.setLayoutManager(horizontalLayoutManager);
mainleadRecycler.setAdapter(mainleadRecycler_Adpter);
public void getLeadStatus(){
final ProgressDialog progressBar = new ProgressDialog(MainActivity.this);
progressBar.setMessage("Loading..");
progressBar.show();
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST,leadstatus_url,
new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(!response.isEmpty()){
try {
JSONArray jsonarray=new JSONArray(response);
statusList =new ArrayList<Main_lead_data>();
for(int i=0;i<jsonarray.length();i++)
{
JSONObject jsonobject = jsonarray.getJSONObject(i);
Main_lead_data data=new Main_lead_data();
data.setStatusName((jsonobject.optString("name")));
data.setStatusCount(jsonobject.optString("data"));
data.setStaus_id(jsonobject.optString("sid"));
statusList.add(data);
}
progressBar.dismiss();
} catch (JSONException e) {
progressBar.dismiss();
Toast.makeText(MainActivity.this,"Server Not Responding..Please Try Again",Toast.LENGTH_SHORT).show();
Log.e("catchException",e.toString());
e.printStackTrace();
}
}
else {
progressBar.dismiss();
Toast.makeText(MainActivity.this,"Server Not Responding",Toast.LENGTH_SHORT).show();
Log.e("error","Empty Response");
}
}
},
new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error",error.toString());
progressBar.dismiss();
Toast.makeText(MainActivity.this,"Server Not Responding",Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("company_id",companyId );
return params;
}
};
requestQueue.add(stringRequest);
}
我还想知道如何在JsonArrayRequest方法中传递post参数并获得响应。
以下是适配器类:
public class Main_lead_Adpater extends RecyclerView.Adapter<Main_lead_Adpater.ViewHolder> {
private Context context;
private ArrayList<Main_lead_data> lead_data ;
LinearLayout view_linear;
public Main_lead_Adpater(Context context,ArrayList<Main_lead_data> lead_data) {
this.context = context;
this.lead_data=lead_data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_lead_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Main_lead_data lead_status = lead_data.get(position);
holder.textCount.setText(lead_status.getStatusCount());
holder.textName.setText(String.valueOf(lead_status.getStatusName()));
holder.textId.setText(String.valueOf(lead_status.getStaus_id()));
view_linear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public int getItemCount() {
return lead_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textCount, textName, textId;
public ViewHolder(View itemView) {
super(itemView);
textCount = itemView.findViewById(R.id.total_count);
textName = itemView.findViewById(R.id.status_name);
textId = itemView.findViewById(R.id.sid_tv);
view_linear=(LinearLayout)itemView.findViewById(R.id.view_linear);
}
}
}
以下是JSON响应:
[
{
"name": "New Enquiry",
"data": "23",
"sid": "1"
},
{
"name": "Interested",
"data": "4",
"sid": "2"
},
{
"name": "Not Interested",
"data": "5",
"sid": "3"
},
{
"name": "Proposal Sent",
"data": "5",
"sid": "4"
},
{
"name": "Ongoing",
"data": "8",
"sid": "6"
},
{
"name": "Converted Client",
"data": "6",
"sid": "7"
},
{
"name": "Lost",
"data": "27",
"sid": "8"
}]
谢谢。
答案 0 :(得分:0)
尝试在获取arraylist之后使用适配器的invoke构造函数,然后设置适配器。这将起作用
答案 1 :(得分:0)
您需要用一个空列表更新您已经拥有的适配器列表。从服务器获得响应后,将列表添加到适配器。
将此方法添加到适配器
public void addItems(ArrayList<Main_lead_data> dataList){
lead_data.addAll(dataList);
notifyDataSetChanged();
}
此方法会将所有数据添加到列表中,并将数据填充到RecyclerView中。
像这样更改您的onResponse
代码:
if(!response.isEmpty()){
try {
JSONArray jsonarray=new JSONArray(response);
// No need to reinitialize statusList here. You already initialize this
// statusList = new ArrayList<Main_lead_data>();
for(int i=0;i<jsonarray.length();i++)
{
JSONObject jsonobject = jsonarray.getJSONObject(i);
Main_lead_data data = new Main_lead_data();
data.setStatusName((jsonobject.optString("name")));
data.setStatusCount(jsonobject.optString("data"));
data.setStaus_id(jsonobject.optString("sid"));
statusList.add(data);
}
mainleadRecycler_Adpter.addItems(statusList);
progressBar.dismiss();
} catch (JSONException e) {
progressBar.dismiss();
Toast.makeText(MainActivity.this,"Server Not Responding..Please Try Again",Toast.LENGTH_SHORT).show();
Log.e("catchException",e.toString());
e.printStackTrace();
}
}
else {
progressBar.dismiss();
Toast.makeText(MainActivity.this,"Server Not Responding",Toast.LENGTH_SHORT).show();
Log.e("error","Empty Response");
}
}
答案 2 :(得分:0)
删除statusList =new ArrayList<Main_lead_data>();
块中的StringRequest
行。
在mainleadRecycler_Adpter.notifyDataSetChanged()
块的progressBar.dismiss();
之前添加StringRequest
行。
答案 3 :(得分:0)
您可以使用jsonschema2pojo 从JSON或JSON-Schema生成普通的旧Java对象。 http://www.jsonschema2pojo.org/
实现'com.google.code.gson:gson:2.6.2' 批注@SerializedName的库
Gson gson =新的Gson();
Mainleaddata mainleaddata = gson.fromJson(response.toString(),Mainleaddata.class);
答案 4 :(得分:0)
我不知道出了什么问题,我刚刚创建了一个新项目,只是为了测试它并进行一些小的更改。调试状态列表时出现相同的错误,但我得到了输出。
下面是MAinactivity的代码:
IQueryable<Customer> myCustomers = ...
IEnumerator<Customer> customerEnumerator = myCustomers.GetEnumerator();
while (await customerEnumerator.MoveNextAsync())
{
Customer customer = customerEnumerator.Current;
Process(customer);
}