简体   繁体   English

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

[英]How to create Recyclerview adapter in android?

I am using this LIB . 我正在使用这个LIB It's working fine but I can not loading my data in adapter class. 它工作正常,但无法在适配器类中加载数据。 like. 喜欢。 If i add some static data that will be added but whenever i tries to load from server that will not be added. 如果我添加一些将要添加的静态数据,但是每当我尝试从服务器加载时都不会添加。 finally i am trying with this approach. 最后,我正在尝试这种方法。
But when i calls the Brandinfo and try to add data that will be error saying colon missing etc. please tell me how i can i add data to adapter to complete this recyclerview. 但是,当我打电话给Brandinfo并尝试添加数据时会出现错误,例如冒号丢失等。请告诉我如何将数据添加到适配器以完成此recyclerview。 Thanks. 谢谢。 My Fragment class is 我的片段类是

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();

        }
    });

This is my Brandinfo class: 这是我的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;
}

} }

finally this is my adapter class: 最后这是我的适配器类:

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);
    }
}

} }

So what I would recommend is the following (which I use in my own project): 因此,我建议以下内容(在我自己的项目中使用):

Create the following method in you TOAdapter: 在您的TOAdapter中创建以下方法:

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

Then call the following method from where you initialized the recyclerviewadapter and your list: 然后从初始化recyclerviewadapter和列表的位置调用以下方法:

mAdapter.refreshRecyclerViewBrands(mContentItems);

This basically takes the list you just got from volley and tells the recyclerview to use this new list and refresh the recyclerview. 这基本上是将您刚刚从凌空抽出的列表拿走,并告诉recyclerview使用此新列表并刷新recyclerview。 This worked for me so hopefully will work for you. 这对我有用,因此希望对您有用。

Another thing I noticed is in your onBindViewHolder your are setting the following: 我注意到的另一件事是在您的onBindViewHolder您正在设置以下内容:

BrandInfo brandList = new BrandInfo();

Then you use: 然后您使用:

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

But brandlist.getBrandname is null. 但是brandlist.getBrandname为null。 Never are you actually giving the brandlist object any values, rather use the following : 实际上,您实际上从不为品牌列表对象提供任何值,而应使用以下内容:

final BrandInfo brandList = brandLists.get(position);

Hope this helps. 希望这可以帮助。

you should do like this 你应该这样

  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");

then i add my adapter like this CustomSourcingAdapter adapter=new 然后我像这样添加我的适配器CustomSourcingAdapter adapter = new
CustomSourcingAdapter(Sourcing_modes.this, publishPlacementList); CustomSourcingAdapter(Sourcing_modes.this,publishPlacementList); lists.setAdapter(adapter); list.setAdapter(adapter);

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(position).getPositionName()); viewHolder.placementtitle.setText(publishPlacementList.get(position).getPositionName()); viewHolder.Noofposition.setText(publishPlacementList.get(position).getNoOfPosition()); viewHolder.Noofposition.setText(publishPlacementList.get(position).getNoOfPosition()); viewHolder.Department.setText(publishPlacementList.get(position).getName()); viewHolder.Department.setText(publishPlacementList.get(position).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;

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM