2017-06-13 10 views
0

アンドロイドリストビューでアイテムを選択できません。私はすでに設定しています:Androidのリストビューでアイテムを選択できません

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

これは、私が断片内のリストビューしか持っていない他のレイアウトでもうまくいきました。私はクリックの位置を印刷することに疲れましたが、クリックが認識されないようです。ここで

は私のレイアウトです:

<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

他のコンポーネントがclickイベントを消費している必要があります。他のレイアウトがそのイベントを取得しているかどうかを確認してください。 – Vucko

+0

アダプタービューのクリックとリストビューアイテムのクリックは一緒には使えません...アダプターORアクティビティ/フラグメント –

+0

ListView xmlの片方の端をクリックして使用する必要があります.... android:focusable = "true" –

答えて

1

私は問題が何であるかを見つけました。それは文句を言わない私は、リスト内の項目間のスペースをクリックすると、フォーカス可能である

android:descendantFocusability="blocksDescendants" 

となりましたが、私は、リストビューで値をクリックしたとき、私は、XMLでカスタムリストビューの列に行を追加しました作業。私は誰かがこの新しい問題を解決するために熱心に知っていたら素晴らしいだろう。

関連する問題