Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ListView中设置MClick_Android_Android Layout_Listview_Android Listview - Fatal编程技术网

Android 在ListView中设置MClick

Android 在ListView中设置MClick,android,android-layout,listview,android-listview,Android,Android Layout,Listview,Android Listview,我创建了ListView并在其上设置了适配器。然后添加简单的ListView监听器(带有一个Toast),但不添加heppend public class MainActivity extends Activity { ListView lvIrregularVerbs; ArrayList<Map<String, Object>> irregularVerbsData; SimpleAdapter irregularVerbsAdapter; String[] inf

我创建了ListView并在其上设置了适配器。然后添加简单的ListView监听器(带有一个Toast),但不添加heppend

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();

        }
    });

}
公共类MainActivity扩展活动{
列表视图;
ArrayList Verbsdata;
SimpleAdapter不规则Verbsdapter;
字符串[]inf={“读”、“写”、“开始”};
布尔[]fav={false,true,true};
意图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
不规则Verbsdata=新的数组列表(inf.length);
MAPTMP;
对于(int i=0;i
和布局

<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>

当我试着调试它时,我看到程序没有调用McClick


因此,我的ListView不可点击,我无法理解到底出了什么问题。

请将以下属性添加到Layout.XML文件的复选框中

android:focusable="false"
android:focusableInTouchMode="false"

希望它能对你有所帮助。

发布活动\u主布局。listview定义中可能有某些内容。发布活动\u主布局。尝试将
android:DegenantFocusability=“blocksDescendants”
添加到
相对活动
@blackbelt现在开始工作了!!!但是,实际上,我不知道为什么:)这是一个bug。看一看