我无法在android listview中选择一个项目。我已经设定了:
android:choiceMode="singleChoice"
android:listSelector="@drawable/gratis_selector"
这在其他布局上工作得很好,我在片段中只有listview。我累了打印出点击的位置,但似乎无法识别点击。
这是我的布局:
<LinearLayout 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:orientation="vertical"
tools:context="com.cosicervin.administration.fragments.ChangePriceFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:text="PLZ/Ort" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:text="Limousine Preis" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:text="Kombi Preis" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:text="Bus Preis" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Neu"
android:id="@+id/new_place_button"/>
</LinearLayout>
<LinearLayout
android:id="@+id/price_swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/places_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:listSelector="@drawable/gratis_selector"
/>
</LinearLayout>
我已经制作了自己的适配器,但这不是我认为的问题,这里也是片段代码:
public class ChangePriceFragment extends Fragment implements GeneralFragment{
ListView placesListView;
String serverRequestToken;
String serverUrl;
View view;
ArrayList<Place> places;
PlaceListAdapter adapter;
RequestQueue requestQueue;
Place selectedPlace;
SwipeRefreshLayout swipe;
Button newPlacebButton;
public ChangePriceFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_change_price, container, false);
serverUrl = ((MainActivity)getActivity()).server_url;
serverRequestToken = ((MainActivity)getActivity()).server_request_token;
requestQueue = MyRequestQueue.getInstance(getContext()).getRequestQueue();
placesListView = (ListView) view.findViewById(R.id.places_listView);
placesListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.e("Position", Integer.toString(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
placesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("Position", Integer.toString(position));
}
});
newPlacebButton = (Button) view.findViewById(R.id.new_place_button);
newPlacebButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NewPlaceFragment newPlaceFragment = new NewPlaceFragment();
newPlaceFragment.show(getFragmentManager(),"New Place");
}
});
places = new ArrayList<>();
adapter = new PlaceListAdapter(getActivity().getApplicationContext(), R.layout.places_list_layout, null);
placesListView.setAdapter(adapter);
placesListView.setSelection(R.drawable.gratis_selector);
fetchPlacesFromServer();
return view;
}
private void fetchPlacesFromServer(){
final Map<String, String> params = new HashMap<>();
params.put("token", serverRequestToken);
params.put("service","15");
final String URL = serverUrl + "/administration_services.php";
CustomRequest customRequest = new CustomRequest(Request.Method.POST, URL, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
adapter.deleteAll();
adapter.notifyDataSetChanged();
try {
JSONArray array = response.getJSONArray("places");
for(int i = 0; i < array.length(); i++){
JSONObject object = array.getJSONObject(i);
Place place = new Place();
place.setId(object.getInt("id"));
place.setName(object.getString("place_name"));
place.setCarPrice(object.getInt("car_price"));
place.setVanPrice(object.getInt("van_price"));
place.setBusPrice(object.getInt("bus_price"));
places.add(place);
adapter.add(place);
adapter.notifyDataSetChanged();
Log.i("Place", place.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(customRequest);
}
private void changePlacePrices(){
Map<String, String> params = new HashMap<>();
params.put("token", serverRequestToken);
params.put("service", "16");
params.put("place_id",Integer.toString(selectedPlace.getId()));
params.put("place_name", selectedPlace.getName());
params.put("car_price", Integer.toString(selectedPlace.getCarPrice()));
params.put("van_price", Integer.toString(selectedPlace.getVanPrice()));
params.put("bus_price", Integer.toString(selectedPlace.getBusPrice()));
final String URL = serverUrl + "/administration_services.php";
CustomRequest customRequest = new CustomRequest(Request.Method.POST, URL, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
int code = 1;
try {
code = response.getInt("code");
} catch (JSONException e) {
e.printStackTrace();
}
if(code == 2){
Toast.makeText(getContext(), "Gespeichert", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(customRequest);
}
}
答案 0 :(得分:1)
我发现问题是什么,我的意思是它仍然存在。我已经在xml的行中添加了自定义列表视图行:
android:descendantFocusability="blocksDescendants"
现在,当我点击列表中的项目之间的空间是可聚焦的,但当我点击列表视图中的值时它不会工作。如果有人知道解决这个新问题,我会很棒。