SetOnItemClickListener在android的自定义ListView中没有响应
我是android新手,我目前正在制作一个带有自定义列表项的listview,我想对listview的项目单击事件执行一个操作,我搜索了这么多类似的线程,但运气不佳,有人能帮我解决这个问题吗。我的代码如下所示SetOnItemClickListener在android的自定义ListView中没有响应,android,android-layout,listview,Android,Android Layout,Listview,我是android新手,我目前正在制作一个带有自定义列表项的listview,我想对listview的项目单击事件执行一个操作,我搜索了这么多类似的线程,但运气不佳,有人能帮我解决这个问题吗。我的代码如下所示 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http:
<?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>
适配器
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;
}
}
公共类CityAdapter扩展了BaseAdapter{
私有上下文;
私人城市;
所选整数_位置=-1;
公共城市适配器(上下文c,ArrayList城市){
mContext=c;
this.city=城市;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回城市。大小();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
观点五;
如果(convertView==null){//如果它没有被回收,初始化一些属性
LayoutInflater充气器=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u充气器\u服务);
v=充气机。充气(R.layout.raw_city,父项,false);
}否则{
v=(视图)转换视图;
}
TextView电视城=(TextView)v.findViewById(R.id.tv城市);
tv_city.setText(city.get(position.getCity());
返回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
}
} );
lv_city.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Pref.setValue(选择CITY.this、Const.Pref_CITY_ID、cityList.get(position.getCity_ID());
finish();//正在完成的活动
}
} );
您在listview项中的类错误单击
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
}
});
lv_city.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
Pref.setValue(选择CITY.this、Const.Pref\u CITY\u ID、,
getcitylist.get(position.getCity_id());
finish();//正在完成的活动
}
});
@quick learner's是正确的,但我为您的参考提出了一些想法
- 如果您使用自定义适配器类,在我的建议中,在自定义适配器类中使用setOnClickListener,这是处理此类情况的更好方法李>
mViewHolder.MyUI.setOnClickListener