2014-03-31 81 views
0

我創建了ListView並在其上設置了Adapter。然後添加簡單的OnItemClickListener(帶一個Toast),但沒有任何東西出現。設置onItemClick在ListView中

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

當我試圖調試它,我看到節目

和佈局

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

和主要活動我以前不叫onItemClick。

所以我的ListView是無法點擊的,我不能忽略什麼是錯的。

+0

post activity_main佈局。在listview定義中可能有些東西。 – njzk2

+0

發佈您的activity_main佈局。 –

+0

嘗試在「RelativeLayout」中添加'android:descendantFocusability =「blocksDescendants」' – Blackbelt

回答

0

請在下面添加屬性到您的複選框在Layout.XML文件

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

希望它會幫助你。