我是android的新手,我目前正在制作一个自定义列表项的列表视图,我想对Listview的项目点击事件做一个动作,我搜索了很多类似的线程没有运气,任何人都可以帮助我来解决这个问题。我的代码如下,
<?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;
}
}
活性
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 :(得分: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
}
});
答案 1 :(得分:0)
@quick learner 是正确的,但我对你的参考作了一些想法..
例如,
mViewHolder.MyUI.setOnClickListener