English 中文(简体)
setOnClickListener for ToggleButton/CheckBox in not working in ListView?
原标题:setOnClickListener for ToggleButton/CheckBox in not working in ListView?

我希望在名单上打一个支票箱或洞,子,并放下浮标语,但只为零位工作。

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

    }

}

这里是我的XML,用于通用_list_row_withtoggle

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

I used ToggleButton 以及CheckBox both, but there is not difference, it works only for zero position, otherwise not calling onClickListner.

I am unable to underst以及the reason, why its happening.

让我知道,什么还不清楚。

最佳回答

首先在此作改动。

 row = inflater.inflate(R.layout.generic_list_row_withtoggle,parent,false);                 
checkBox = (CheckBox)row.findViewById(R.id.checkBox); 
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签