在ListView中设置onItemClick

时间:2014-03-31 13:49:48

标签: android android-layout listview android-listview

我创建了ListView并在其上设置了Adapter。然后添加简单的OnItemClickListener(带一个Toast),但没有任何东西。

public class MainActivity extends Activity {

ListView lvIrregularVerbs;
ArrayList<Map<String, Object>> irregularVerbsData;
SimpleAdapter irregularVerbsAdapter;
String[] inf = { "read", "write", "begin" };
boolean[] fav = { false, true, true };
Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    irregularVerbsData = new ArrayList<Map<String, Object>>(inf.length);
    Map<String, Object> tmp;
    for (int i = 0; i < inf.length; i++) {
        tmp = new HashMap<String, Object>();
        tmp.put("Inf", inf[i]);
        tmp.put("Fav", fav[i]);
        irregularVerbsData.add(tmp);
    }

    String[] from = { "Inf", "Fav" };
    int[] to = { R.id.tvInfinitive, R.id.cbFavorite,};

    irregularVerbsAdapter = new SimpleAdapter(this, irregularVerbsData, R.layout.verbs, from, to);
    lvIrregularVerbs = (ListView) findViewById(R.id.lvSimple);
    lvIrregularVerbs.setAdapter(irregularVerbsAdapter);
    lvIrregularVerbs.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String selected = ((TextView) view.findViewById(R.id.tvInfinitive))
                    .getText().toString();

            Toast toast = Toast.makeText(getApplicationContext(), selected,
                    Toast.LENGTH_SHORT);
            toast.show();

        }
    });

}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="6dp"
    android:layout_marginRight="6dp"
    android:layout_marginTop="4dp"
    android:background="@drawable/googlenow" >

    <TextView
        android:id="@+id/tvInfinitive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Infinitive"
        android:textColor="@android:color/primary_text_light"
        android:textSize="30sp" />

    <CheckBox
        android:id="@+id/cbFavorite"
        style="?android:attr/starStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="5dp"
        android:enabled="false" />
</RelativeLayout>

和主要活动

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.irregularverbs.MainActivity$PlaceholderFragment"
android:background="#e5e5e5" >

<ListView
    android:id="@+id/lvSimple"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@null" 
    android:dividerHeight="0dp" >
</ListView>

当我尝试调试它时,我看到该程序没有调用onItemClick。

所以我的ListView无法点击,我无法理解错误。

1 个答案:

答案 0 :(得分:0)

请在Layout.XML文件

的复选框中添加以下属性
android:focusable="false"
android:focusableInTouchMode="false"

希望它会对你有所帮助。