gpt4 book ai didi

android - 使用 volley json 数据的 fragment 中的 Recyclerview?

转载 作者:行者123 更新时间:2023-11-29 17:17:32 25 4
gpt4 key购买 nike

请任何人解释 RecyclerView 在 fragment 中使用 volley 获取 json 数据。我已经在下面提到了链接编码 Google recyclerview in fragment

这是我在 android 中的第一个项目,所以我无法理解那些编码。请任何人帮助我。

我的 fragmnet 编码:

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

我的适配器代码:

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

首先创建一个布局项

例如,让我们创建一个只有 TextView 的 View

XML

每个项目.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" />

现在让我们创建一个 View 持有者我将发布 View 持有者和 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);

}
}
}

现在是模型类

public class Model {

private String name;

public String getName() {
return name;
}

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

现在后端已经完成,让我们在Fragment上实现它

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

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

onCreatView()中获取recyclerView的id

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

然后通过创建对象将数据传递给适配器

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

你现在已经完成了,唯一未决的是,如果你正在访问数据表单服务器调用 notifyDataSetChanged()adaptor = new Adaptor(getContext(), getList()); 其中 getList() 返回模型数据并且不调用 adaptor.clearAdaptor()

希望这有帮助..

编辑你可以通过两种方式来影响每个布局项目.. 一种在上面,第二种在 onCreateViewHolder

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

关于android - 使用 volley json 数据的 fragment 中的 Recyclerview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38157011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com