簡體   English   中英

單擊Android ListActivity中的項目不起作用

[英]Click on item in android ListActivity not working

我有帶有自定義ArrayAdapter的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>

我的問題是,我無法單擊任何列表項。 到目前為止,我嘗試將ImageView的focusableclickable設置為false (這對我發現可能有所幫助),但是不幸的是,問題仍然存在。 請幫助,這整天困擾着我。 謝謝。

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

為了清楚起見,默認的ListActivity默認背景顏色是白色。 我要黑色。 當我從上方應用樣式時,將禁用單擊時突出顯示列表活動項。 如何再次將其啟用為默認突出顯示顏色?

在您的oncreate嘗試一下

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

或者,您可以編寫此函數來處理列表項的單擊

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

我有此工作代碼,並且我認為您的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;
        }

嘗試這個

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM