【问题标题】:RecyclerView Android JsonRecyclerView Android Json
【发布时间】:2017-05-29 13:50:21
【问题描述】:

如何使用 RetrofitRecylerView 中设置数据。我尝试过,但无法成功执行。

这是我的代码:

JSON 数据是:

 {
"model": [
{
  "id": "1",
  "brand_name": "Audi",
  "brand_logo": "1495456122.",

},
{
  "id": "3",
  "brand_name": "BMW",
  "brand_logo": "1495451144.",

}
]
}    

BrandSelectActivity.java

   public class BrandSelectActivity extends AppCompatActivity implements View.OnClickListener {
ProgressDialog pDialog;

Button btn_addVehicle;
BrandListRecyclerAdapter adapter;


RecyclerView recyclerView_brand;
public ImageView carBrandImg;
public TextView carBrandName;

private static String TAG = BrandSelectActivity.class.getSimpleName();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_brand_select);
    carBrandImg = (ImageView)findViewById(R.id.carBrandImg);
    carBrandName = (TextView)findViewById(R.id.carBrandName);
    recyclerView_brand = (RecyclerView) findViewById(R.id.recycler_view);
    pDialog = new ProgressDialog(getApplicationContext());
    pDialog.setCancelable(true);
    pDialog.setMessage("Please Wait...");
    pDialog.show();

    ArrayList<BrandModel> brandModelArrayList = new ArrayList<BrandModel>();   

    BrandListRecyclerAdapter brandAdapter = new BrandListRecyclerAdapter(BrandSelectActivity.this, brandModelArrayList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView_brand.setLayoutManager(mLayoutManager);
    recyclerView_brand.setAdapter(brandAdapter);


    brandAdapter.setItemClickListener(new BrandListRecyclerAdapter.MyClickListerer() {
        @Override
        public void onItemClick(int position, View view) {
            Intent sosIntent = new Intent(BrandSelectActivity.this, ModelSelectActivity.class);
            startActivity(sosIntent);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    });

    callBrandSelect();

}


    private void callBrandSelect() {

    ApiInterface apiService = 
   ApiClient.getClient().create(ApiInterface.class);

    Call<BrandSelectResponse> call = apiService.saveBrand("1");
    call.enqueue(new Callback<BrandSelectResponse>() {
        @Override
        public void onResponse(Call<BrandSelectResponse> call, retrofit2.Response<BrandSelectResponse> response) {
            BrandSelectResponse br=  response.body();
            List<BrandSelectResponse.ModelBean> modelList = br.getModel();

            modelList.size();
            modelList.get(0).getBrand_logo();
            modelList.get(0).getBrand_name();


               pb.dismiss();
        }

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

            Toast.makeText(BrandSelectActivity.this,"Opps ..!!Failed to 
     connect to our server.. Try Again later..",Toast.LENGTH_SHORT).show();

            Log.e(TAG, t.toString());
        }
    });
    }

@Override
public void onClick(View v) {
}
 }

BrandListRecyclerAdapter.java

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

    Context mcontext;
    List<BrandSelectResponse> brandModelArrayList;
    public  LayoutInflater inflater;
    public static MyClickListerer myClickListerer;


public BrandListRecyclerAdapter(Context context, ArrayList<BrandSelectResponse> brandModelArrayList) {
    this.mcontext = context;
   this.brandModelArrayList=brandModelArrayList;
 }



@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.row_brand, parent, false);

    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    BrandSelectResponse current = brandModelArrayList.get(position);
    holder.setData((BrandSelectResponse) brandModelArrayList,position);
    holder.carBrandName.setText(current.getBrand_name());
     holder.carBrandImg.setImageResource(current.getBrand_logo());
}

public interface MyClickListerer {
    void onItemClick(int position, View view);
}

public void setItemClickListener(MyClickListerer myClickListerer) {
    this.myClickListerer = myClickListerer;
}

@Override
public int getItemCount() {
    return brandModelArrayList.size();
}
  public class MyViewHolder extends RecyclerView.ViewHolder implements 
   View.OnClickListener {
    public ImageView carBrandImg;
    public TextView carBrandName;
    BrandSelectResponse brandModelArrayList;
    LinearLayout current;
    int position;

    public MyViewHolder(View itemView) {

        super(itemView);
        carBrandImg = (ImageView) itemView.findViewById(R.id.carBrandImg);
        carBrandName = (TextView) itemView.findViewById(R.id.carBrandName);



    }


    public void setData(BrandSelectResponse current, int position) {

        carBrandName.setText(brandModelArrayList.getBrand_name());
        carBrandImg.setImageResource(brandModelArrayList.getBrand_logo());

        this.position = position;
        this.brandModelArrayList = current;
    }


    @Override
    public void onClick(View v) {
        myClickListerer.onItemClick(getAdapterPosition(), v);
    }
}
  } 

品牌选择响应.java

public class BrandSelectResponse implements Serializable{


private List<ModelBean> model;

public BrandSelectResponse(List<ModelBean> model) {
    this.model = model;
}

public List<ModelBean> getModel() {
    return model;
}

public void setModel(List<ModelBean> model) {
    this.model = model;
}

public static class ModelBean {


    private String id;
    private String brand_name;
    private String brand_logo;


    public String getId() {
        return id;
    }

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

    public String getBrand_name() {
        return brand_name;
    }

    public void setBrand_name(String brand_name) {
        this.brand_name = brand_name;
    }

    public String getBrand_logo() {
        return brand_logo;
    }

    public void setBrand_logo(String brand_logo) {
        this.brand_logo = brand_logo;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

  }
  }

【问题讨论】:

  • 问题是……?
  • 你能发布你的 logcat 以发现 HARDEEP 错误吗?
  • 我应该给你一个关于改造的工作例子吗?
  • 您的 Json 数据似乎格式不正确

标签: android json android-recyclerview retrofit


【解决方案1】:

你给适配器 EMPTY 列表。 您永远不会添加数据到 brandModelArrayList

下次试试看logcat,什么异常,哪一行有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多