Ervin Cosic
Ervin Cosic

Reputation: 114

Cannot select item in listview in Android

I cannot select an item in the android listview. I've already set:

android:choiceMode="singleChoice"
android:listSelector="@drawable/gratis_selector"

this worked fine on other layouts where i had only the listview in the fragment. I tired to print out the position of the click but it seems like the click is not recognized.

Here is my layout:

<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>

I've made my own adapter but this is not the problem i think, here is the fragment code as well :

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);
}

}

Upvotes: 1

Views: 665

Answers (1)

Ervin Cosic
Ervin Cosic

Reputation: 114

I found what the problem was i mean it still is in a way. I've added to the custom listview row in the xml the line :

 android:descendantFocusability="blocksDescendants"

and now when I click on the space between the items in the list is focusable but when i click on a value in the listview it wont work. I would be awesome if someone knew hot to solve this new problem.

Upvotes: 1

Related Questions