我正在使用预订系统
我需要在列表视图上显示一个数组 我尝试了一些代码,但是它们不起作用
请帮助我
预先感谢;
这是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工作表接收带有保留的数据,并使用适配器在我的列表视图中显示保留。
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();
}
在这里,我有具有textviews和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>
答案 0 :(得分:2)
我不确定您所面对的问题到底是什么。但我建议您做这些事情以确认问题出在哪里。
排球请求中包含此功能。
public void onResponse(String response) {
parseItems(response);
}
您是否确定响应中包含包含您的商品的有效JSON字符串?。因此,首先尝试在LOG中打印此响应,看看您是否从请求中得到了一些东西。
如果您在响应中得到一些帮助,但仍然无法正常工作。然后在此处评论屏幕截图或响应字符串。另外,如果您可以在发送请求以获取数据的位置显示URL,那么我很容易检查一下为什么在您的情况下不起作用。
此外,您还应该这样做。这不是造成问题的原因,但是您应该以这种方式使用它。您的代码段
lv=view.findViewById(R.id.lview);
getReservations();
重写函数onViewCreated()并将这两行放在该函数中。但是实际的问题不在这里