簡體   English   中英

點擊Android中自定義列表視圖中的復選框

[英]Click event from checkbox in custom listview in Android

我的列表視圖行有自定義布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listSelector"
    android:orientation="horizontal">

    <LinearLayout
            android:id="@+id/checkboxSelection1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip">

        <CheckBox android:id="@+id/checkbox1" />

        </LinearLayout>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/checkbox1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

我還有一個適配器來顯示相應的數據; 它做的。 從UI的角度來看,它看起來像我想要它。

但是,當我單擊一個復選框時 - 沒有任何反應。 我想存儲我在后端選擇的項目列表(理想情況下在活動類中)。

在我的onCreate活動類中,我有這個代碼:

listView.setAdapter(adapter);

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

// Click event for single list row
listView.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        int i = 1;
    }
});

我得到了int 1 = 1; 在那里,我可以添加一個斷點,看看它是否被擊中。 它沒有。 我確定我做錯了,就像它連接到列表視圖行而不是復選框或其他東西 - 但我不知道如何將事件掛鈎到復選框。

如果有人能指出我正確的方向,我會很感激。

謝謝

編輯:只是為了澄清

我在適配器中有這個:

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{

    public void onCheckedChanged(CompoundButton arg0, boolean arg1) 
    {
        // TODO Auto-generated method stub
        int i = 1;
    }
});

那個斷點確實受到了打擊。 因此,當我選擇或取消選中復選框時,我只是想知道如何在活動中引發事件,而不僅僅是適配器。

不要這樣做! 我幾乎瘋了,試圖在ListView中獲取小部件來響應點擊。 不要將Button,ImageButton或CheckBox小部件放在ListView中。 TextViews和ImageViews是最佳選擇。 試圖對該CheckBox上的單擊做出反應,找到它所在的ListView項目,然后向Activity發送一些內容可能對您的健康非常有害。 我試過了。

TextView + ImageView可以在顯示復選標記的圖標和不顯示復選標記的圖標之間變化 - 模擬CheckBox;

ImageView本身可以模擬一個Button。

需要將ImageView設置為focus = false。

首先,創建一個新類,其中包含要為ListView中的每個項顯示的字段。 我創建了一個顯示的文本和一個指示是否已檢查的布爾值。 將此類用於ArrayList和ArrayAdapter。

然后為ListView添加setOnItemClickListener(),然后使用position查找項視圖,然后獲取新類的項並切換其布爾值。

在MyArrayAdapter.getView方法中,getItem(position)返回該項的新類的實例。 使用布爾值確定要用於ImageView的圖標。

當你需要知道ListView中有什么和沒有“檢查”時,你只需要瀏覽ArrayList並檢查每個項目的布爾值。

我想到了。

在適配器中我添加了這個:_activity是從調用活動傳遞到構造函數的活動。 根據getView中的位置和構造適配器時傳入的數據,myObj被聲明為代碼中的最后一個。

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean checked) 
            {
                // Cast it so we can access the public functions
                MyActivity myActivity = (MyActivity) _activity;

                if (checked) // true if the checkbox is checked, false if unchecked
                {
                    myActivity.checkboxSelected(myObj);
                }
            }
        });

在活動中我添加了這個:

public void checkboxSelected(MyObj myObj)
{
    // Do stuff with myObj here
}

希望這有助於某人。

暫無
暫無

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

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