Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在listview的片段中显示数组?_Java_Android_Android Listview - Fatal编程技术网

Java 如何在listview的片段中显示数组?

Java 如何在listview的片段中显示数组?,java,android,android-listview,Java,Android,Android Listview,我在做预订系统 我需要在listview上显示一个数组 我尝试了一些代码,但是它们不起作用 请帮帮我 提前感谢 这是OnCreateView部分 public class Res extends Fragment { ListView lv; ArrayAdapter adapter; ProgressDialog loading; @Override public View onCreateView(LayoutInflater inflater,

我在做预订系统

我需要在listview上显示一个数组 我尝试了一些代码,但是它们不起作用

请帮帮我

提前感谢

这是OnCreateView部分

public class Res extends Fragment {
    ListView lv;
    ArrayAdapter adapter;
    ProgressDialog loading;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_res, container, false);
        lv=view.findViewById(R.id.lview);

        getReservations();
        return view;
    }
这一部分是从GoogleSheet接收数据,GoogleSheet有保留,并使用adapter在我的listview上显示保留

private void getReservations() {

        loading =  ProgressDialog.show(getActivity(),"Rezervasyonlar yukleniyor","Lutfen Bekleyiniz",false,true);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, "HERE I HAVE THE URL ..WORKS ON GOOGLE APP SCRIPT BUT PROBLEM IS ABOUT MY CODE ",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        parseItems(response);
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        );
        int socketTimeOut = 50000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);

        stringRequest.setRetryPolicy(policy);

        RequestQueue queue = Volley.newRequestQueue(getActivity());
        queue.add(stringRequest);
    }

    private void parseItems(String jsonResponse) {
        ArrayList<HashMap<String, String>> list = new ArrayList<>();

        try {
            JSONObject jobj = new JSONObject(jsonResponse);
            JSONArray jarray = jobj.getJSONArray("items");


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

                JSONObject jo = jarray.getJSONObject(i);

                final String name = jo.getString("name");
                final String phone = jo.getString("phone");
                final String email = jo.getString("email");
                final String arriving = jo.getString("arriving");
                final String departure = jo.getString("departure");
                final String pax = jo.getString("pax");
                final String roomtype = jo.getString("roomtype");
                final String rate = jo.getString("rate");
                final String address = jo.getString("addres");
                final String company = jo.getString("company");
                final String agency = jo.getString("agency");
                final String ratecode= jo.getString("ratecode");

                HashMap<String, String> item  = new HashMap<>();
                item.put("name",name);
                item.put("email",email);
                item.put("phone",phone);
                item.put("arriving",arriving);
                item.put("departure",departure);
                item.put("company",company);
                item.put("pax",pax);
                item.put("roomtype",roomtype);
                item.put("rate",rate);
                item.put("address",address);
                item.put("agency",agency);
                item.put("ratecode",ratecode);

                list.add(item);


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


        //adapter = new SimpleAdapter(getActivity(),list,R.layout.getres,
                //new String[]{"name","phone","email","arriving","departure","pax","roomtype","rate","address","company","agency"},new int[]{R.id.namev,R.id.phonev,R.id.emailv,R.id.checkv,R.id.depv,R.id.personv,R.id.roomtypev,R.id.ratev,R.id.adresv,R.id.companyv,R.id.agencyv});

        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list);
        adapter = new ArrayAdapter(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, list);


        lv.setAdapter(adapter);
        lv.setCacheColorHint(getResources().getColor(android.R.color.white   ));
        loading.dismiss();

    }
private void getReservations(){
loading=ProgressDialog.show(getActivity(),“Rezervasynlar yukleniyor”,“Lutfen Bekleyinazing”,false,true);
StringRequest StringRequest=new StringRequest(Request.Method.GET,“我这里有URL..在GOOGLE应用程序脚本上工作,但问题是关于我的代码”,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
项目(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}
);
int socketTimeOut=50000;
RetryPolicy policy=newDefaultRetryPolicy(socketTimeOut,0,DefaultRetryPolicy.DEFAULT\u BACKOFF\u MULT);
stringRequest.setRetryPolicy(策略);
RequestQueue=Volley.newRequestQueue(getActivity());
添加(stringRequest);
}
私有void parseItems(字符串jsonResponse){
ArrayList=新建ArrayList();
试一试{
JSONObject jobj=新的JSONObject(jsonResponse);
JSONArray jarray=jobj.getJSONArray(“项目”);
for(int i=0;i
在这里,我有包含TextView和listview的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light"
    tools:context=".Res">


    <ListView
        android:id="@+id/lview"
        style="@android:style/Widget.Holo.ListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@android:color/background_light" />
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/design_default_color_primary">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light">

            <TextView
                android:id="@+id/namev"
                android:layout_width="157dp"
                android:layout_height="match_parent"
                android:ems="10"
                android:hint="Name"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/emailv"
                android:layout_width="121dp"
                android:layout_height="match_parent"
                android:layout_marginStart="5dp"
                android:ems="10"
                android:hint="E-mail"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/phonev"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Phone"
                android:inputType="number" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_orange_light">

            <TextView
                android:id="@+id/companyv"
                android:layout_width="111dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Company"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/adresv"
                android:layout_width="132dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:ems="10"
                android:hint="Address"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/agencyv"
                android:layout_width="91dp"
                android:layout_height="match_parent"
                android:ems="10"
                android:hint="Agency"
                android:inputType="textPersonName" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light">


            <TextView
                android:id="@+id/nationv"
                android:layout_width="105dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Natinality"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/idnov"
                android:layout_width="143dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="ID number"
                android:inputType="textPersonName" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_orange_light">

            <TextView
                android:id="@+id/checkv"
                android:layout_width="111dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Arriving"
                android:inputType="date" />

            <TextView
                android:id="@+id/depv"
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Departure"
                android:inputType="date" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light">

            <TextView
                android:id="@+id/roomprv"
                android:layout_width="137dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Room Preference"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/roomtypev"
                android:layout_width="155dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Room Tye"
                android:inputType="textPersonName" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_orange_light">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="113dp"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:gravity="center_vertical"
                android:text="Pérson" />

            <TextView
                android:id="@+id/personv"
                android:layout_width="134dp"
                android:layout_height="wrap_content"
                android:autofillHints=""
                android:ems="10"
                android:gravity="center|center_vertical"
                android:inputType="number"
                android:text="0" />


        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light">

            <TextView
                android:id="@+id/ratecodev"
                android:layout_width="114dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Rate Code"
                android:inputType="textPersonName" />

            <TextView
                android:id="@+id/ratev"
                android:layout_width="151dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Rate"
                android:inputType="textPersonName" />



        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_orange_light"
            android:gravity="center">

        </TableRow>
    </TableLayout>

</FrameLayout>

我不确定到底是什么问题在你这边。但我建议做这些事情来确认问题所在

确保您在网络通话中收到了一些信息 你的截击请求中有这个函数

            public void onResponse(String response) {
                parseItems(response);
            }
是否确保响应包含包含您的项目的有效JSON字符串?。因此,首先尝试在日志中打印此响应,看看您是否从请求中获得了一些信息

如果你在回应中得到了一些东西,但仍然不起作用。然后在这里评论屏幕截图或响应字符串。此外,如果你可以透露你的URL,你是在哪里发送请求获取数据,那么我将很容易检查为什么它在你的情况下不工作

您应该这样做。这不是问题的原因,但您应该这样使用。这一行代码片段

 lv=view.findViewById(R.id.lview); 
 getReservations();

重写函数onViewCreated(),并将这两行放在此函数内。但是真正的问题不在这里

我不确定到底是什么问题在你这边。但我建议做这些事情来确认问题所在

确保您在网络通话中收到了一些信息 你的截击请求中有这个函数

            public void onResponse(String response) {
                parseItems(response);
            }
是否确保响应包含包含您的项目的有效JSON字符串?。因此,首先尝试在日志中打印此响应,看看您是否从请求中获得了一些信息

如果你在回应中得到了一些东西,但仍然不起作用。然后评论截图o