How do I create a Recycliewiew adapter in Android?

I am using LIB . It works fine, but I cannot load my data into the adapter class. as. If I add some static data to be added, but whenever I try to download from the server that will not be added. finally i am trying with this approach.
But when I find Brandinfo and try to add data that will be wrong if there is no colon, etc. please tell me how can I add data to the adapter to complete this recyclerview. Thank you. My Fragment Class

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:

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

      

}

0


source to share


2 answers


So, I would recommend the following (which I use in my own project):

Create the following TOAdapter method for yourself:

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

      

Then call the following method from which you initialized the recyclerviewadapter and your list:

mAdapter.refreshRecyclerViewBrands(mContentItems);

      

Basically it is the list you just got from volley and tells recyclerview to use this new list and update recyclerview. This worked for me so, hopefully will work for you.

Another thing I noticed, in yours onBindViewHolder

, you set the following:



BrandInfo brandList = new BrandInfo();

      

Then you use:

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

      

But brandlist.getBrandname is null. Never give the brandlist object any values, rather use the following:

final BrandInfo brandList = brandLists.get(position);

      

Hope it helps.

0


source


you should do 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 Adapter CustomSourcingAdapter = new
      CustomSourcingAdapter (Sourcing_modes.this, publishPlacementList); lists.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.Noofposition.setText (publishPlacementList.get (position) .getNoOfPosition ()); 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;

};

      

0


source







All Articles