Recyclerview in fragmnet using volley json data?

亡梦爱人 提交于 2019-12-24 06:44:02

问题


please any one explain RecyclerView in fragment using volley to get json data. Already i refered below link coding Google recyclerview in fragment

this is my first project in android, so i cannot understand that coding. Please any one help me.

My fragmnet Coding:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View drawer = inflater.inflate(R.layout.fragment_progress, container, false);
    orderLists = new ArrayList<>();

    getProgressData();

    recyclerView = (RecyclerView) drawer.findViewById(R.id.progress);
    adapter = new ProgressOrderListAdapter(orderLists, this);
    adapter.clearAdaptor();
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return recyclerView;
}

private void getProgressData(){
    String mobilecustomertoken = SharedPreferencesManager.readPreferenceString("MobileCustomerToken", "D/N");
    JSONObject progressData = new JSONObject();
    try{
        progressData.put("mobilecustomertoken", mobilecustomertoken);
        JsonObjectRequest progressObject = new JsonObjectRequest(1, Common.OrderDetails + "progress", progressData, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject progressResponse) {
                Log.d("Responseprogress", progressResponse.toString());
                try {
                    int status = progressResponse.getInt("status");
                    if(status == 1) {
                        progressOrderProgress(progressResponse);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                Log.d("Response", "PROGRESS ERROR");
            }
        });
        progressObject.setShouldCache(false);
        ServiceBellApp.getInstance().addToRequestQueue(progressObject);
    }
    catch (JSONException localJSONException){
        localJSONException.printStackTrace();
        return;
    }
}

private void progressOrderProgress(JSONObject progressResponse) throws JSONException {
    JSONArray result = progressResponse.getJSONArray("orderdata");
    OrderList orderListModule = new OrderList();
    for(int i=0; i<result.length(); i++){
        JSONObject orderData = result.getJSONObject(i);
        orderListModule.setPackage_name(orderData.getString("package_name"));
        orderListModule.setOrderdate(orderData.getString("orderdate"));
        orderListModule.setServicedate(orderData.getString("servicedate"));
        orderListModule.setServicetime(orderData.getString("servicetime"));
        orderListModule.setOrderid(orderData.getString("orderid"));
        orderListModule.setOrdstatus(orderData.getString("ordstatus"));
        orderListModule.setOrderamount(orderData.getInt("orderamount"));
    }
    orderLists.add(orderListModule);
}

My adapter code:

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {
List<OrderList> List;
private FragmentPending mContext;

public OrderListAdapter(List<OrderList> List, FragmentPending context) {
    this.mContext = context;
    this.List = List;

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_list_view, null);
    ViewHolder holder = new ViewHolder(view);
    // this is where the each item is inflated.

    return holder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    OrderList orderListsPos = List.get(position);
    // this is where the data for each item is assigned
    holder.textViewPackageName.setText(orderListsPos.getPackage_name());
    holder.textOrderdate.setText(orderListsPos.getOrderdate());
    holder.textServicedate.setText(orderListsPos.getServicedate());
    holder.textServicetime.setText(orderListsPos.getServicetime());
    holder.textOrderid.setText(orderListsPos.getOrderid());
    holder.textOrderamount.setText("Rs." + orderListsPos.getOrderamount());
    holder.textStatus.setText(orderListsPos.getOrdstatus());

}

@Override
public int getItemCount() {
    return List.size();
}

public void clearAdaptor() {
    List.clear();
}


public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView textViewPackageName;
    public TextView textServicedate;
    public TextView textServicetime;
    public TextView textOrderdate;
    public TextView textOrderid;
    public TextView textOrderamount;
    public TextView textStatus;

    public ViewHolder(View itemView) {
        super(itemView);
        textViewPackageName = (TextView) itemView.findViewById(R.id.productName);
        textOrderdate = (TextView) itemView.findViewById(R.id.orderdate);
        textOrderid = (TextView) itemView.findViewById(R.id.orderno);
        textOrderamount = (TextView) itemView.findViewById(R.id.orderprice);
        textStatus = (TextView) itemView.findViewById(R.id.orderstatus);

    }
}}

回答1:


First you need four things

1 ) layout that holds the each recycler view layout item

2 ) A view holder for creating each layout

3 ) A Model Class to holds the data

4 ) Recycler Adaptor which deals with the data for the Each Layout item

FIrst create a layout item

for eample lets create a single view with only TextView in it

XML

each_item.xml

   <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:gravity="start|center_vertical"
        android:textColor="@color/white"
        android:textSize="18sp" />

and lets now create a view holder i will post both code for the view holder and RecyclerAdaptor

public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {
  List<Model> List = Collections.emptyList();
  private Context mContext;
  private LayoutInflater inflater;

  public Adaptor(Context context, List<Model> List) {
    inflater = LayoutInflater.from(context);
    this.mContext = context;
    this.List = List;

 }


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.each_item, parent, false);
    final ViewHolder holder = new ViewHolder(view);
    // this is where the each item is inflated.

    return holder;
}

@Override
public void onBindViewHolder(WinnersViewHolder holder, int position) {
    Model mModel = List.get(position); 
    // this is where the data for each item is assigned
    holder.nameView.setText("" + mModel.getName());

}

@Override
public int getItemCount() {
    return List.size();
}

public void clearAdaptor() {
    List.clear();
}


public class ViewHolder extends RecyclerView.ViewHolder {
    protected TextView nameView;

    public ViewHolder(View itemView) {
        super(itemView);
        this.nameView = (TextView) itemView.findViewById(R.id.name);

    }
 }
}

Now the model class

public class Model {

private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name= name;
 }
}

Now the back end is done, Lets implement it on the Fragment

List<Model> List = new ArrayList<>();
private RecyclerView mRecyclerView;
private Adaptor adaptor;

public Fragment() {
    // constructor of fragment
    // Required empty public constructor
}

in the onCreatView() get the id of recyclerView

 View fragmentView = inflater.inflate(R.layout.fragment_layout, container, false);
 mRecyclerView = (RecyclerView) fragmentView .findViewById(R.id.recycler_view);

and then pass the data to the Adaptor by creating its object

    adaptor = new Adaptor(getContext(), List);
    adaptor.clearAdaptor();
    mRecyclerView.setAdapter(adaptor);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

You are done now, only thing pending is if you are accessing data form server call notifyDataSetChanged() or adaptor = new Adaptor(getContext(), getList()); where the getList() returns the Model data and do not call adaptor.clearAdaptor() .

hope this helps ..

EDIT you can two ways infalte the each layout item .. one is above and second is in onCreateViewHolder

 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item, null);



回答2:


Check this link It is really helpful for beginners for understanding concepts from start to mastering the RecyclerView.

https://github.com/codepath/android_guides/wiki/Using-the-RecyclerView

Hope this helps you to understand recycler view concepts.



来源:https://stackoverflow.com/questions/38157011/recyclerview-in-fragmnet-using-volley-json-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!