2017-06-24 10 views
-1

私は現在、カスタムリストアイテムを使用してリストビューを作成しています。リストビューのアイテムクリックイベントでアクションを実行したいと思っています。 、誰もがSetOnItemClickListenerがカスタムリストビューで応答しません

<?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="#fff"> 

    <TextView 
     android:id="@+id/tv_hdr" 
     android:layout_width="fill_parent" 
     android:layout_height="80dp" 
     android:layout_alignParentTop="true" 
     android:background="@color/orange" 
     android:gravity="center" 
     android:text="SELECT YOUR CITY" 
     android:textColor="@color/white" 
     android:textSize="22dp" /> 

    <EditText 
     android:id="@+id/et_search" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tv_hdr" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" 
     android:background="@drawable/et_selector" 
     android:drawableLeft="@drawable/search" 
     android:hint="City Or Postal Code" 
     android:padding="10dp" 
     android:textSize="16dp" /> 

    <TextView 
     android:id="@+id/tv_loc" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/et_search" 
     android:background="@color/grey_light" 
     android:drawableLeft="@drawable/ic_loc" 
     android:gravity="center_vertical" 
     android:padding="10dp" 
     android:paddingLeft="5dp" 
     android:text=" My Location" 
     android:textColor="@color/black" 
     android:textSize="16dp" 
     android:textStyle="bold" /> 


    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/scr_location" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/tv_loc" 
     android:fillViewport="true"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/tv_populcar_city" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 

       android:background="@color/white" 
       android:gravity="center_vertical" 
       android:padding="10dp" 
       android:paddingLeft="5dp" 
       android:text=" Popular Cities" 
       android:textColor="@color/orange" 
       android:textSize="16dp" 
       android:textStyle="bold" /> 

      <ListView 
       android:id="@+id/lv_city" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tv_populcar_city" /> 

      <TextView 
       android:id="@+id/tv_states" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/lv_city" 
       android:background="@color/white" 
       android:gravity="center_vertical" 
       android:padding="10dp" 
       android:paddingLeft="5dp" 
       android:text="States" 
       android:textColor="@color/orange" 
       android:textSize="16dp" 
       android:textStyle="bold" /> 


      <ListView 
       android:id="@+id/lv_state" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/tv_states" /> 
     </RelativeLayout> 
    </ScrollView> 


</RelativeLayout> 

アダプタ、私はthis.myコードは以下の通りです修正し

public class CityAdapter extends BaseAdapter { 
    private Context mContext; 
    private final ArrayList<City> city; 
    Integer selected_position = -1; 


    public CityAdapter(Context c, ArrayList<City> city) { 
     mContext = c; 
     this.city = city; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return city.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View v; 
     if (convertView == null) { // if it's not recycled, initialize some attributes 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.raw_city, parent, false); 
     } else { 
      v = (View) convertView; 
     } 
     TextView tv_city = (TextView) v.findViewById(R.id.tv_city); 
     tv_city.setText(city.get(position).getCity()); 

     return v; 
    } 
} 

活動を助けることができる

lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Pref.setValue(SelectCity.this, Const.PREF_CITY_ID, cityList.get(position).getCity_id()); 
     finish();//finishing activity 
    } 
}); 

答えて

0

あなたがクリック

lv_city.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, 
        long id) { 
       Pref.setValue(SelectCity.this, Const.PREF_CITY_ID, 
       cityList.get(position).getCity_id()); 
       finish();//finishing activity 
      } 
     }); 
0

@quick学習者のは正しかった、しかし、あなたは、カスタムアダプタを使用する場合、私は...あなたの参照のために

  • をいくつかのアイデアを入れてリストビュー項目に間違ったクラスです私の提案では、カスタムアダプタクラスでsetOnClickListenerを使用してください。これは、このタイプのケースを実行するためのより良い方法です!例えば

mViewHolder.MyUI.setOnClickListener 
関連する問題