2

我想要 listView 中的复选框或切换按钮,并设置 onclicklisterner 但它仅适用于零位置,这是我的代码

String[] NewTransferItems = { "Spendability", "Accounts", "Budgets",
    "I'm keeping" };

dashboardEditListView = (ListView) findViewById(R.id.dashboardEditListView);
listAdapter = new DashboardEditListAdapater();
dashboardEditListView.setAdapter(listAdapter);
dashboardEditListView.s

etOnItemClickListener(listClickListner);

我的适配器:

class DashboardEditListAdapater extends ArrayAdapter<String> implements OnClickListener{
    DashboardEditListAdapater() {
        super(DashboardEdit.this, R.layout.generic_list_row,
                NewTransferItems);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        System.out.println("Called Method " + getItem(position)
                + "  Position " + position);

        View row = null;
        EditText editText = null;
        CheckBox checkBox = null;

        if (position == 0 || position == 1 || position == 2) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.generic_list_row_withtoggle,
                    parent, false);
            checkBox = (CheckBox) findViewById(R.id.checkBox);

            // init
        } else if (position == 3) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.generic_list_row_with_edittext,
                    parent, false);

            editText = (EditText) findViewById(R.id.rowRightEditText);

            if(editText != null) {
                editText.setVisibility(View.VISIBLE);
            }
        }

        LinearLayout rowWrapper = (LinearLayout) row
                .findViewById(R.id.rowWrapper);
        LinearLayout rowHeadWrapper = (LinearLayout) row
                .findViewById(R.id.rowHeadWrapper);
        rowHeadWrapper.setVisibility(ViewGroup.GONE);

        String item = getItem(position);

        if (position == 0) {
            if(checkBox != null){
                checkBox.setTag(position);
                checkBox.setOnClickListener(this);
            }

        } else if (position == 1) {
            if(checkBox != null){
                checkBox.setTag(position);
                checkBox.setOnClickListener(this);
            }

        } else if (position == 2) {
            if(checkBox != null){
                checkBox.setTag(position);
                checkBox.setOnClickListener(this);
            }
        }

        TextView rowTitle = (TextView) row.findViewById(R.id.rowTitle);
        rowTitle.setText(item);
        return row;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        System.out.println("Clicked");

    }

}

这是我的 generic_list_row_withtoggle 的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.mytalk.androidapp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/rowWrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="8dip"
        android:background="#FFF"
        android:orientation="vertical"
        android:padding="0dip" >

        <LinearLayout
            android:id="@+id/rowHeadWrapper"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/cell_header_bg"
            android:orientation="vertical"
            android:paddingLeft="8dip" >

            <TextView
                android:id="@+id/rowHead"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:text="Profile"
                android:textColor="#000"
                android:textSize="18dip"
                android:textStyle="bold"
                android:visibility="gone" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:paddingLeft="8dip" >

            <!--
            <ImageView
                android:id="@+id/rowImage"
                android:layout_width="60dp"
                android:layout_height="36dp"
                android:layout_centerVertical="true"
                android:src="@drawable/bahrain_flag"
                android:visibility="gone" />
            -->

            <TextView
                android:id="@+id/rowTitle"
                android:layout_width="200dip"
                android:layout_height="45dip"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dip"
                android:layout_toRightOf="@id/rowImage"
                android:gravity="center_vertical"
                android:text="Current"
                android:textColor="#000"
                android:textSize="15dip"
                android:textStyle="bold" />

                   <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="10dip"
                 />
            <!-- 
            <ToggleButton
                android:id="@+id/togglebutton2"
                android:layout_width="60dip"
                android:layout_height="40dip"
                android:layout_alignParentRight="false"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="10dip"
                android:textOff="OFF"
                android:textOn="ON"
                android:visibility="visible" />
                 -->
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

我同时使用了 ToggleButton 和 CheckBox,但没有区别,它只适用于零位置,否则不调用 onClickListner。

我无法理解原因,为什么会发生。

让我知道是否有任何不清楚的地方

4

1 回答 1

1

首先在这里进行更改,

 row = inflater.inflate(R.layout.generic_list_row_withtoggle,parent,false);                 
checkBox = (CheckBox)row.findViewById(R.id.checkBox); 
于 2012-01-12T07:38:12.523 回答