尽管我之前创建了一个可点击的列表窗口,但那时我不能做同样的事情。 我正在使用片段活动。
这是我的片段xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark">
<TextView
android:id="@+id/textView"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="@string/name" />
<TextView
android:id="@+id/textView2"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Arriving" />
<TextView
android:id="@+id/textView3"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:text="Departure" />
<TextView
android:id="@+id/textView5"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:text="Pax" />
<TextView
android:id="@+id/textView6"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/tbrow"
android:text="Phone" />
<TextView
android:id="@+id/textView7"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Email" />
<TextView
android:id="@+id/textView8"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/room_type" />
<TextView
android:id="@+id/textView9"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="@string/rate" />
</TableRow>
</TableLayout>
<ListView
android:id="@+id/lview"
style="@android:style/Widget.Holo.Light.ListView"
android:layout_width="wrap_content"
android:layout_height="318dp"
android:layout_marginBottom="16dp"
android:background="@color/design_default_color_primary"
android:choiceMode="singleChoice"
android:clickable="true"
android:divider="@android:drawable/divider_horizontal_dim_dark"
android:focusable="true"
android:longClickable="true"
android:theme="@style/ThemeOverlay.AppCompat.Light"
android:transitionGroup="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
android:descendantFocusability="blocksDescendants"
tools:listitem="@android:layout/simple_list_item_1">
</ListView>
</android.support.constraint.ConstraintLayout>
这是我的上下文xml,或者不知道如何调用它。但是该xml中的数据或项目。 Textviews正在接收数据(来自url的保留信息)。
<?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="67dp"
android:background="@color/design_default_color_primary"
android:clickable="false">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:clickable="false"
android:orientation="vertical">
<TextView
android:id="@+id/namev"
android:layout_width="80dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="textPersonName"
android:lines="2"
android:maxLines="10" />
<TextView
android:id="@+id/checkv"
android:layout_width="80dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="date" />
<TextView
android:id="@+id/depv"
android:layout_width="80dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="date" />
<TextView
android:id="@+id/personv"
android:layout_width="20dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/phonev"
android:layout_width="80dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/emailv"
android:layout_width="150dp"
android:layout_height="56dp"
android:layout_marginStart="5dp"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/roomtypev"
android:layout_width="60dp"
android:layout_height="match_parent" />
<TextView
android:id="@+id/ratev"
android:layout_width="40dp"
android:layout_height="match_parent"
android:ems="10"
android:inputType="textPersonName" />
</TableRow>
</TableLayout>
</FrameLayout>
这是活动代码。我也尝试实现“ AdapterView.OnItemClickListener”。
public class Res extends Fragment {
ListView lv;
SimpleAdapter 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);
lv.setClickable(true);
getReservations();
return view;
}
public 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");
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("pax", pax);
item.put("roomtype", roomtype);
item.put("rate", rate);
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"}, 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});
//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();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.v("TAG", "CLICKED row number: ");
}
});
}
我没有收到任何错误。但是我也无法使lisview可点击。 如果有必要,我可以发布整个活动代码。