Frage

Ich möchte ein Kontrollkästchen oder eine Umschalttaste in listView und setze onclicklisterner, aber es funktioniert nur für die Nullposition. Hier ist mein Code

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

und

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

etOnItemClickListener (listClickListner);

Mein Adapter:

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

    }

}

Hier ist mein XML für generic_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>

Ich habe sowohl ToggleButton als auch CheckBox verwendet, aber es gibt keinen Unterschied, es funktioniert nur für die Nullposition, andernfalls wird onClickListner nicht aufgerufen.

Ich kann den Grund nicht verstehen, warum es passiert.

Lassen Sie mich wissen, wenn noch etwas nicht klar ist

War es hilfreich?

Lösung

Nehmen Sie hier zunächst Änderungen vor,

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top