输出仅显示列表的最后一项 即, 16:15-16:30 16:15-16:30 它应该显示在哪里 16:00-16:15 16:15-16:30
json输出
{
"slots": [{
"1": {
"slot": "16:00-16:15",
"value": "1",
"ar_index": "1"
},
"2": {
"slot": "16:15-16:30",
"value": "2",
"ar_index": "2"
}
}]
}
这是用于获取json输出并使用适配器在recyclerview中设置它的postexecute代码 我获取这两个插槽并显示在recyclerview中,但仅在列表的最后一次显示。无法理解代码的问题
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
List<DataBook> data=new ArrayList<>();
pdLoading.dismiss();
try {
JSONObject jsonObject = new JSONObject(result);
System.out.println("jsonObject_1 = "+jsonObject);
if(jsonObject != null){
JSONArray jsonArray = jsonObject.getJSONArray("slots");
System.out.println("jsonArray_2 = "+jsonArray);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
System.out.println("jsonArray.length() = "+jsonArray.length());
System.out.println("i_value = "+i);
JSONObject elem = jsonArray.getJSONObject(i);
System.out.println("elem = " + elem);
for (int j = 1; j <= elem.length(); j++) {
System.out.println("elem.length() = "+elem.length());
System.out.println("j_value = "+j);
JSONObject elem1 = elem.getJSONObject(String.valueOf(j));
System.out.println("elem1 = " + elem1);
DataBook fishData = new DataBook();
if (elem1 != null) {
fishData.fishName = elem1.getString("slot");
System.out.println("fishData.fishName = " + fishData.fishName);
}
data.add(fishData);
}
}
}
}
// Setup and Handover data to recyclerview
mRVFishPrice = (RecyclerView)findViewById(R.id.rc_slotsList);
mAdapter = new AdapterFish(BookActivity.this, data);
mRVFishPrice.setAdapter(mAdapter);
mRVFishPrice.setLayoutManager(new LinearLayoutManager(BookActivity.this));
} catch (JSONException e) {
Toast.makeText(BookActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
这是我的Adapter类,我正在设置结果
public class AdapterFish extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<DataBook> data= Collections.emptyList();
DataBook current;
int currentPos=0;
// create constructor to innitilize context and data sent from MainActivity
public AdapterFish(Context context, List<DataBook> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.book_container_fish, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Get current position of item in recyclerview to bind data and assign values from list
MyHolder myHolder= (MyHolder) holder;
current = data.get(position);
myHolder.textFishName.setText(current.fishName);
System.out.println("holder_current.fishName"+current.fishName);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView textFishName;
ImageView ivFish;
TextView textSize;
TextView textType;
TextView textPrice;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
textFishName= (TextView) itemView.findViewById(R.id.textFishName);
}
}
}
答案 0 :(得分:0)
public AdapterFish(Context context, List<DataBook> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
system.out.println(""+data.size());
//check how much data that you have got
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Get current position of item in recyclerview to bind data and
assign values from list
MyHolder myHolder= (MyHolder) holder;
current = data.get(position-1);
myHolder.textFishName.setText(current.fishName);
System.out.println("holder_current.fishName"+current.fishName);
}