如何在android中创建Recyclerview适配器?

时间:2015-05-26 11:12:46

标签: android android-recyclerview recycler-adapter

我正在使用此LIB。它工作正常但我无法在适配器类中加载我的数据。喜欢。 如果我添加一些将添加的静态数据,但每当我尝试从无法添加的服务器加载时。最后我正在尝试这种方法 但是,当我调用Brandinfo并尝试添加错误表示冒号丢失等数据时,请告诉我如何将数据添加到适配器以完成此回收查看。 谢谢。 我的片段类是

List<BrandInfo> mContentItems = new ArrayList<BrandInfo>();
pbDialog = new ProgressDialog(getActivity());
    // Showing progress dialog before making http request
    pbDialog.setMessage("Loading...");
    pbDialog.show();
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d("BrandViewActivity", response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            brandInfo = new BrandInfo();
                            mContentItems.add(new BrandInfo().setBrandname(obj.getString("title")));
                            String title = obj.getString("title");
                            String location = obj.getString("location");
                            String image = obj.getString("image_path");
                            String category = obj.getString("category");

                            Log.d("fffffffffff", mContentItems.toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Error aa gai bhaya", "Error: " + error.getMessage());
            hidePDialog();

        }
    });

这是我的Brandinfo课程:

public class BrandInfo {
private String brandname;
private String brandinfo;
private String address;
private String detail;

public String getBrandname() {
    return brandname;
}
public void setBrandname(String brandname) {
    this.brandname = brandname;
}
public String getBrandInfo() {
    return brandinfo;
}
public void setBrandinfo(String brandinfo) {
    this.brandname = brandinfo;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getDetail() {
    return detail;
}
public void setSex(String detail) {
    this.detail = detail;
}

}

最后这是我的适配器类:

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

private List<BrandInfo> brandLists = new ArrayList<BrandInfo>();
private ImageLoader imageLoader;

public TOAdapter(List<BrandInfo> brandLists) {
    this.brandLists = brandLists;
}

// Create new views (invoked by the layout manager)
@Override
public TOAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View itemLayoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_card_big, null);

    // create ViewHolder

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    // - get data from your itemsData at this position
    // - replace the contents of the view with that itemsData

    BrandInfo brandList = new BrandInfo();



   imageLoader = AppController.getInstance().getImageLoader();
    viewHolder.txtViewTitle.setText(brandList.getBrandname());
    //viewHolder.thumbNail.setImageResource(itemsData[position].getImageUrl());
    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    viewHolder.thumbNail.setImageUrl(brandList.getBrandname(), imageLoader);


}

@Override
public int getItemCount() {
    String s = String.valueOf(brandLists.size());
    Toast.makeText(AppController.getInstance(),s,Toast.LENGTH_LONG).show();
    return brandLists.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public TextView txtViewTitle;
    final NetworkImageView thumbNail;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.brandtitle);

        thumbNail = (NetworkImageView) itemLayoutView
                .findViewById(R.id.thumbnail);
    }
}

}

2 个答案:

答案 0 :(得分:0)

所以我建议的是以下(我在自己的项目中使用):

在TOAdapter中创建以下方法:

public void refreshRecyclerViewBrands(List<BrandInfo> brandLists) {
    this.brandLists = brandLists;
    notifyDataSetChanged();
}

然后从初始化recyclerviewadapter和列表的位置调用以下方法:

mAdapter.refreshRecyclerViewBrands(mContentItems);

这基本上是你从排球中获得的列表,并告诉recyclerview使用这个新列表并刷新recyclerview。这对我有用,所以希望对你有用。

我注意到的另一件事是onBindViewHolder你正在设置以下内容:

BrandInfo brandList = new BrandInfo();

然后你使用:

viewHolder.txtViewTitle.setText(brandList.getBrandname());

但brandlist.getBrandname为null。 您实际上从未向品牌列表对象提供任何值,而是使用以下内容:

final BrandInfo brandList = brandLists.get(position);

希望这有帮助。

答案 1 :(得分:0)

你应该这样做

  JSONObject obj = response.getJSONObject(i);
  brandInfo = new BrandInfo();
  mContentItems.add(new                      
  BrandInfo(obj.getString("title"),
   obj.getString("title");
   obj.getString("location");
   obj.getString("image_path");
   obj.getString("category");

然后我像这样添加我的适配器         CustomSourcingAdapter适配器=新的
        CustomSourcingAdapter(Sourcing_modes.this,publishPlacementList);         lists.setAdapter(适配器);

this is my custom Adapter class
    public class CustomSourcingAdapter extends ArrayAdapter<SourcingModel> {

private final Activity context;

private final List<SourcingModel> publishPlacementList;

public CustomSourcingAdapter(Activity context,List<SourcingModel>       
publishPlacementList) {
    // TODO Auto-generated constructor stub

    super(context, R.layout.sorsinglist,publishPlacementList);
    // TODO Auto-generated constructor stub
    this.context=context;
    this.publishPlacementList=publishPlacementList;

}

static class ViewHolder {
    protected TextView placementtitle;
    protected TextView Noofposition;
    protected ImageButton next;
    protected TextView Department;

}

public View getView(int position,View converview,ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (converview ==null ){
        LayoutInflater inflater=context.getLayoutInflater();
        converview=inflater.inflate(R.layout.sorsinglist, null);
        viewHolder = new ViewHolder();




        converview.setTag(viewHolder);
        converview.setTag(R.id.idplacetitle,viewHolder.placementtitle);
        converview.setTag(R.id.idnoofpos,viewHolder.Noofposition);
        converview.setTag(R.id.imreqserch,viewHolder.next);
        converview.setTag(R.id.iddepartment,viewHolder.Department);
    }else{
        viewHolder= (ViewHolder)converview.getTag();
    }

viewHolder.placementtitle.setText(publishPlacementList.get(位置).getPositionName());         viewHolder.Noofposition.setText(publishPlacementList.get(位置).getNoOfPosition());         viewHolder.Department.setText(publishPlacementList.get(位置).getName());

    viewHolder.next.setTag(position);

       viewHolder.next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int pos = (Integer) v.getTag();
            long  id = publishPlacementList.get(pos).getId();

            Intent it = new Intent(context, SourcingSecond.class);
            it.putExtra("id", id);
            context.startActivity(it);

        }
    });


    return converview;

};