2013-04-25 65 views
1

我ListActivity定製ArrayAdapter,並按照列表項的版面項目:點擊android的ListActivity不工作

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" 
android:paddingTop="5dip" 
android:paddingBottom="5dip" > 

<ImageView 
    android:id="@+id/image" 
    android:layout_width="48dip" 
    android:layout_height="48dip" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/white" 
     android:textSize="24.5sp" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/white" 
     android:textSize="12sp"/> 
</LinearLayout> 

我的問題是,我無法點擊任何列表項。我到目前爲止試過的是 設置focusable,clickablefalse(我發現可能有所幫助)到我的ImageView 但不幸的是,問題依然存在。請幫助,這一直在困擾着我整天。謝謝。

編輯:好的,所以William的答案幫助我解決了這個問題,現在唯一的問題是點擊項目時不會突出顯示它,當我將背景顏色更改爲黑色時(通過在清單中爲此列表活動設置新主題)。 這是我從res/values/styles/文件夾獲取的活動主題<style name="ContentsListTheme"> <item name="android:background">@color/black</item> </style>的自定義樣式。

爲了清楚起見,默認的ListActivity背景顏色再次爲白色。我想要黑色。當我應用上面的樣式時,禁用單擊時列出活動項突出顯示。我怎樣才能再次啓用它默認的突出顯示顏色?

回答

1

在你oncreate試試這個

ListView lv = getListView(); 
lv.setTextFilterEnabled(true); 
lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
}); 

OR你可以寫這個函數將處理在列表項

protected void onListItemClick(ListView l, View v, int position, long id) {} 
+0

謝謝。我添加onListItemClick與小'確定'吐司消息和點擊正在工作(我得到'確定'吐司消息)。但是,列表項目不會改變顏色,並在點擊時閃爍(因爲他們應該)。什麼可能是錯的? – InProgress 2013-04-25 15:20:31

+1

@ 1atera1us你的問題是關於點擊列表項目,如果你想要關於顏色,請添加更多的代碼,並告訴我你想要什麼顏色和哪個UI – 2013-04-25 15:31:58

+0

我編輯了我的問題。 – InProgress 2013-04-25 16:01:35

0

我有這樣的工作代碼的點擊,我想在你的Java代碼是個問題。

public class NewsListActivity extends Activity implements OnItemClickListener 

...

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.list); 
     listView = (ListView) findViewById(R.id.listView); 
     listView.setAdapter(new NewsDataArrayAdapter(NewsListActivity.this, headlines)); 
     listView.setOnItemClickListener(this); 

...

public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     if (v == null) { 
      return; 
     } 
+0

我正在擴展ListActivity,我使用onListItemClick。 – InProgress 2013-04-25 15:23:23

+0

@ 1atera1us你會嘗試這種工作解決方案嗎? – 2013-04-25 15:37:01

+0

我讓我的舊解決方案工作。但是,謝謝你,我一定會保存你提供的這段代碼。 – InProgress 2013-04-25 16:00:41

0

試試這個

ListView lv=(ListView)findViewById(yourlistview); 
Lv.setonItemClickListener(this); 

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    paret.getItemAtPositon(position); 
    //this returns you an object cast it into the same object you passed to your adapter cast to string if values passed were string 
}