如何在片段中的列表视图中显示数组?
How can I display the array on my listview in a fragment?
我正在开发预订系统
我需要在列表视图中显示一个数组
我尝试了一些代码,但它们不起作用
请帮帮我
提前致谢;
这是 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;
}
这部分是从 google sheet 接收有预订的数据,并使用适配器在我的列表视图上显示预订。
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();
}
这里我有包含文本视图和列表视图的 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 字符串?。所以首先尝试在 LOG 中打印这个响应,看看你是否从请求中得到了一些东西。
如果您在回复中得到了一些东西,但仍然无法正常工作。然后在此处评论屏幕截图或响应字符串。此外,如果您可以透露 URL 您发送请求以获取数据的位置,那么我将很容易检查为什么它在您的情况下不起作用。
另外你应该这样做。这不是您的问题的原因,但您应该这样使用它。 您的代码片段的这一行
lv=view.findViewById(R.id.lview);
getReservations();
覆盖函数 onViewCreated() 并将这两行放在该函数中。但实际问题不在这里
我正在开发预订系统
我需要在列表视图中显示一个数组 我尝试了一些代码,但它们不起作用
请帮帮我
提前致谢;
这是 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;
}
这部分是从 google sheet 接收有预订的数据,并使用适配器在我的列表视图上显示预订。
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();
}
这里我有包含文本视图和列表视图的 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 字符串?。所以首先尝试在 LOG 中打印这个响应,看看你是否从请求中得到了一些东西。
如果您在回复中得到了一些东西,但仍然无法正常工作。然后在此处评论屏幕截图或响应字符串。此外,如果您可以透露 URL 您发送请求以获取数据的位置,那么我将很容易检查为什么它在您的情况下不起作用。
另外你应该这样做。这不是您的问题的原因,但您应该这样使用它。 您的代码片段的这一行
lv=view.findViewById(R.id.lview);
getReservations();
覆盖函数 onViewCreated() 并将这两行放在该函数中。但实际问题不在这里