首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法将数据从抽射加载到片段(选项卡布局)

无法将数据从抽射加载到片段(选项卡布局)
EN

Stack Overflow用户
提问于 2019-06-08 20:02:48
回答 2查看 294关注 0票数 0

我正在尝试使用volley将数据从mysql数据库加载到Tab布局中的片段。问题是该函数正在从DB获取数据,但没有将其加载到片段中。

当我试图在列表中添加硬编码的数据时,它起作用了,数据被加载到片段中。但这不适用于截击。请帮帮忙

以下是我的片段代码:

代码语言:javascript
运行
复制
public class DriverFragment extends Fragment {
    View v;
    private RecyclerView mRecyclerView;
    Context ctx;
    private static final String URL_PRODUCTS = "http://192.168.10.11/AutoBus/show_driver.php";
    List<DriverModel> mData;
    public DriverFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.driver_frgment, container, false);

        mRecyclerView = (RecyclerView) v.findViewById(R.id.mon_data);
        DriverAdapter recyclerViewAdapter = new DriverAdapter(getContext(),mData);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(recyclerViewAdapter);
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mData = new ArrayList<>();

        /*mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
        mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
        mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
        mData.add(new DriverModel("Asad","092 4587455","Asss","ssss","dddddd"));
*/

        loadDetails();
    }

    private void loadDetails() {


        /*
         * Creating a String Request
         * The request type is GET defined by first parameter
         * The URL is defined in the second parameter
         * Then we have a Response Listener and a Error Listener
         * In response listener we will get the JSON response as a String
         * */
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject data = array.getJSONObject(i);

                                //adding the product to product list
                                mData.add(new DriverModel(
                                        data.getString("bus_number"),
                                        data.getString("driver_uname"),
                                        data.getString("driver_password"),
                                        data.getString("driver_id"),
                                        data.getString("driver_phone")
                                ));
                                Toast.makeText(getActivity(), ""+data, Toast.LENGTH_LONG).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getActivity(), "Error" + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(), "Error" + error.toString(), Toast.LENGTH_SHORT).show();


                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(Objects.requireNonNull(getActivity())).add(stringRequest);
    }

}
EN

回答 2

Stack Overflow用户

发布于 2019-06-08 22:25:58

在列表上设置数据后,在volley请求的onResponse(String response)方法内调用适配器类的notifyDataSetChanged()方法

代码语言:javascript
运行
复制
  // make global object DriverAdapter class
    DriverAdapter recyclerViewAdapter;


     for (int i = 0; i < array.length(); i++) {
        //getting product object from json array
        JSONObject data = array.getJSONObject(i);
        //adding the product to product list
        mData.add(new DriverModel(data.getString("bus_number"),
        data.getString("driver_uname"),
        data.getString("driver_password"),
        data.getString("driver_id"),
        data.getString("driver_phone")));
     Toast.makeText(getActivity(), ""+data, Toast.LENGTH_LONG).show();
    }

  //after set data on list,call here notifiyDataSetChanged() method
  recyclerViewAdapter.notifyDataSetChanged();
票数 0
EN

Stack Overflow用户

发布于 2019-12-20 22:40:16

代码语言:javascript
运行
复制
package com.sbtcc.zimcon;



import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.sbtcc.zimcon.ui.main.Product;
import com.sbtcc.zimcon.ui.main.VendorcustomListAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class VendorsFragment extends Fragment {

    private View VendorsProView;
    private String URL_GET_USERPRODUCT = "http://192.168.1.6/ZimCon/IndividualSell/GetAllProducts.php";

    private View PrivateUserProductsView;
    private ArrayList<Product> arrayList;
    private ListView lv;
    private String URL_GET_USERPRODUCTS = "http://192.168.1.6/ZimCon/IndividualSell/GetMyProducts.php";
    private SessionManager sessionManager;
    private String UseId = "2";
    Context context;
    public VendorsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        VendorsProView =  inflater.inflate(R.layout.fragment_vendors, container, false);

        arrayList = new ArrayList<>();
        lv = VendorsProView.findViewById(R.id.Listview);

        return VendorsProView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GetProductFromServer();
    }
    private void GetProductFromServer() {

        final String UserId = UseId;


        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_GET_USERPRODUCT,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String responseMyProductsPHP) {
                        try {

                            JSONObject jsonObject = new JSONObject(responseMyProductsPHP);
                            String Success = jsonObject.getString("success");
                            JSONArray jsonArray = jsonObject.getJSONArray("products");

                            if (Success.equals("1")){

                                for (int i = 0; i < jsonArray.length(); i++){

                                    JSONObject object = jsonArray.getJSONObject(i);

                                    String id = object.getString("Product_Id");
                                    String image = object.getString("Product_img_link");
                                    String name = object.getString("Name");
                                    String prize = object.getString("Prize");
                                    String qty = object.getString("Qty");
                                    String sold = object.getString("Sold");

                                    String strName = name;
                                    String strPrice = prize;
                                    String strQty = qty;
                                    String vendorId = "9";

                                    arrayList.add(new Product(id, strName, image, strPrice, strQty, vendorId));
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getActivity(),"Error Json: "+e.toString(),Toast.LENGTH_SHORT).show();
                        }

                        VendorcustomListAdapter userCustomProductListAdapter = new VendorcustomListAdapter(
                                getContext(), R.layout.vendor_custom_product_listview, arrayList
                        );
                        lv.setAdapter(userCustomProductListAdapter);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getActivity(),"Error resp : "+error.toString(),Toast.LENGTH_SHORT).show();
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("id", UserId);
                return params;
            }
        };
        RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
        queue.add(stringRequest);

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56506182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档