动态创建的复选框侦听器无法正常工作

时间:2016-04-07 15:58:11

标签: android checkbox onclicklistener tablerow

我有tableLayout,动态创建tableRows,tableRow包含四个textView和一个checkBox。我尝试在tablelayout中的每个checkBox上设置onClickListener,但它只适用于复选框(最后创建)...

dynamicTableRow和OnClickListener ...

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!(v instanceof CheckBox)) {
                // Clicked on something that wasn't a CheckBox - make sure this listener is only given to a CheckBox.
                return;
            }

            CheckBox checkBox = (CheckBox) v;
            if (checkBox.isChecked()) {
                for (int i = 1; i < tableLayout.getChildCount(); i++) {
                    rr = (TableRow) tableLayout.getChildAt(i);
                    idText = (TextView) rr.getChildAt(5);
                    Log.i(TAG, idText.toString());
                }

                //insert row in an array_list and add this array_list as an object in another array_list;
                // every array_list represents an array_list;

                getID = idText.getText().toString();
                //convert getID t int to remove from list
                int itemToAdd = Integer.parseInt(getID);
                if (itemList.get(itemToAdd).getStatus() != "Checked") {
                    itemList.get(itemToAdd).setStatus("Checked");
                    Log.i("Status Checked ::: ", itemList.get(itemToAdd).getStatus());
                } else {
                    // Log.i("INFO:::::::", String.valueOf(itemList.get(itemToAdd).getStatus()));
                    // Log.i("Id Value :::", String.valueOf(getID));
                }
            } else {
                for (int i = 1; i < tableLayout.getChildCount(); i++) {
                    rr = (TableRow) tableLayout.getChildAt(i);
                    idText = (TextView) rr.getChildAt(5);
                }
                getID = idText.getText().toString();
                int itemToRemove = Integer.parseInt(getID);
                if (itemList.get(itemToRemove).getStatus() != "Unchecked") {
                    itemList.get(itemToRemove).setStatus("Unchecked");
                    Log.i("Status unChecked ::: ", itemList.get(itemToRemove).getStatus());
                }

            }
        }
    };

    private void creatingDynamicallyRow() {
        int leftMargin = 130;
        int topMargin = 8;
        int rightMargin = 10;
        int bottomMargin = 2;

        tableLayout = (TableLayout) findViewById(R.id.tableLayout);
        final String getSerialNumber = serialNumber.getText().toString();
        final String getItems = items.getText().toString();
        final String getBrand = brand.getText().toString();
        final String getPrice = price.getText().toString();

        row = new TableRow(StoreFirstActivity.this);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 2);
        layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        row.setLayoutParams(layoutParams);
        checkBox[i] = new CheckBox(StoreFirstActivity.this);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        checkBox[i].setLayoutParams(params);
        checkBox[i].setScaleX((float) 0.80);
        checkBox[i].setScaleY((float) 0.80);
        checkBox[i].setId(checkboxId);
        checkBox[i].setChecked(true);
        row.addView(checkBox[i]);

        rowTexView = new TextView(StoreFirstActivity.this);
        LayoutParams lParams = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        lParams.setMargins(10, 8, 10, 2);
        rowTexView.setLayoutParams(lParams);
        rowTexView.setText(getSerialNumber);

        rowTexViewTwo = new TextView(StoreFirstActivity.this);
        rowTexViewTwo.setLayoutParams(layoutParams);
        rowTexViewTwo.setText(getItems);

        rowTexViewThree = new TextView(StoreFirstActivity.this);
        rowTexViewThree.setLayoutParams(layoutParams);
        rowTexViewThree.setText(getBrand);

        rowTexViewFour = new TextView(StoreFirstActivity.this);
        rowTexViewFour.setLayoutParams(layoutParams);
        rowTexViewFour.setText(getPrice);

        rowTexViewFive = new TextView(StoreFirstActivity.this);
        rowTexViewFour.setLayoutParams(layoutParams);
        rowTexViewFive.setText(String.valueOf(id));

        row.setOnClickListener(listener); // set listener
        row.addView(rowTexView);
        row.addView(rowTexViewTwo);
        row.addView(rowTexViewThree);
        row.addView(rowTexViewFour);
        row.addView(rowTexViewFive);

        tableLayout.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        storeItems = new StoreItems(getSerialNumber, getItems, getBrand, getPrice, getID, "Checked");
        itemList.add(storeItems);
        Log.i(TAG, itemList.toString());

        serialNumber.setText("");
        items.setText("");
        brand.setText("");
        price.setText("");
        row.setClickable(true);
        Log.i("Table Layout is Null :::", String.valueOf(tableLayout.getChildCount()));
        id++;
        i++;
    }

2 个答案:

答案 0 :(得分:1)

试试这个:

CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int i = (int) buttonView.getTag();
        if (isChecked) {
            TableRow tr = (TableRow) tableLayout.getChildAt(i);
            // Do some thing
        } else {
            //etc
        }
    }
};


private void creatingDynamicallyrow()

{
    int leftMargin = 130;
    int topMargin = 8;
    int rightMargin = 10;
    int bottomMargin = 2;

    tableLayout = (TableLayout) findViewById(R.id.tableLayout);

    //...

    int i = 0;

    //...

    CheckBox checkBox = new CheckBox(StoreFirstActivity.this);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    checkBox.setLayoutParams(params);
    checkBox.setScaleX((float) 0.80);
    checkBox.setScaleY((float) 0.80);
    checkBox.setId(checkboxId);
    checkBox.setChecked(true);

    checkBox.setOnCheckedChangeListener(listener);
    checkBox.setTag(i); 

    row.addView(checkBox);

    //...

    id++;
}

答案 1 :(得分:0)

你的问题是我总是0。

int i = 0;

价值永远不会改变。这意味着每次创建新行(以及新的CheckBox)时,您都不会多次设置它。

这就是它适用于最后CheckBox的原因。因为那是覆盖数组上第0个索引的那个。

相反,您应该将i变量完全置于行创建方法之外。像这样:

CheckBox[] checkBox = new CheckBox[100]; // Globally Scope
int i = 0;

然后在您的行创建方法结束时:

private void creatingDynamicallyrow() {
    // ...
    i++;
}

虽然,我不明白你为什么写它如何。我赞成完全重构系统。您可以将侦听器接口实现移到成员范围中,如下所示:

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("onClick was detected");
        if(!(v instanceof CheckBox)) {
            // Clicked on something that wasn't a CheckBox - make sure this listener is only given to a CheckBox.
            return;
        }
        Log.i("onClick was from a CheckBox");

        CheckBox checkBox = (CheckBox) v;
        if (checkBox.isChecked()) {
            for (int i = 1; i < tableLayout.getChildCount(); i++) {
                rr = (TableRow) tableLayout.getChildAt(i);
                idText = (TextView) rr.getChildAt(5);
            }

            //insert row in an array_list and add this array_list as an object in another array_list;
            // every array_list represents an array_list;

            getID = idText.getText().toString();
            //convert getID t int to remove from list
            int itemToAdd = Integer.parseInt(getID);
            if (itemList.get(itemToAdd).getStatus() != "Checked") {
                itemList.get(itemToAdd).setStatus("Checked");
                Log.i("Status Checked ::: ", itemList.get(itemToAdd).getStatus());
            } else {
                // Log.i("INFO:::::::", String.valueOf(itemList.get(itemToAdd).getStatus()));
                // Log.i("Id Value :::", String.valueOf(getID));
            }
        } else {
            for (int i = 1; i < tableLayout.getChildCount(); i++) {
                rr = (TableRow) tableLayout.getChildAt(i);
                idText = (TextView) rr.getChildAt(5);
            }
            getID = idText.getText().toString();
            int itemToRemove = Integer.parseInt(getID);
            if (itemList.get(itemToRemove).getStatus() != "Unchecked") {
                itemList.get(itemToRemove).setStatus("Unchecked");
                Log.i("Status unChecked ::: ", itemList.get(itemToRemove).getStatus());
            }

        }
    }
};

private void creatingDynamicallyrow() {
    int leftMargin = 130;
    int topMargin = 8;
    int rightMargin = 10;
    int bottomMargin = 2;

    tableLayout = (TableLayout) findViewById(R.id.tableLayout);
    final String getSerialNumber = serialNumber.getText().toString();
    final String getItems = items.getText().toString();
    final String getBrand = brand.getText().toString();
    final String getPrice = price.getText().toString();

    int i = 0;
    row = new TableRow(StoreFirstActivity.this);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 2);
    layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
    row.setLayoutParams(layoutParams);
    CheckBox checkBox = new CheckBox(StoreFirstActivity.this);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    checkBox.setLayoutParams(params);
    checkBox.setScaleX((float) 0.80);
    checkBox.setScaleY((float) 0.80);
    checkBox.setId(checkboxId);
    checkBox.setChecked(true);
    checkBox.setOnClickListener(listener);
    row.addView(checkBox);

    rowTexView = new TextView(StoreFirstActivity.this);
    LayoutParams lParams = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
    lParams.setMargins(10, 8, 10, 2);
    rowTexView.setLayoutParams(lParams);
    rowTexView.setText(getSerialNumber);

    rowTexViewTwo = new TextView(StoreFirstActivity.this);
    rowTexViewTwo.setLayoutParams(layoutParams);
    rowTexViewTwo.setText(getItems);

    rowTexViewThree = new TextView(StoreFirstActivity.this);
    rowTexViewThree.setLayoutParams(layoutParams);
    rowTexViewThree.setText(getBrand);

    rowTexViewFour = new TextView(StoreFirstActivity.this);
    rowTexViewFour.setLayoutParams(layoutParams);
    rowTexViewFour.setText(getPrice);

    rowTexViewFive = new TextView(StoreFirstActivity.this);
    rowTexViewFour.setLayoutParams(layoutParams);
    rowTexViewFive.setText(String.valueOf(id));

    row.addView(rowTexView);
    row.addView(rowTexViewTwo);
    row.addView(rowTexViewThree);
    row.addView(rowTexViewFour);
    row.addView(rowTexViewFive);

    tableLayout.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    storeItems = new StoreItems(getSerialNumber, getItems, getBrand, getPrice, getID, "Checked");
    itemList.add(storeItems);
    Log.i(TAG, itemList.toString());

    serialNumber.setText("");
    items.setText("");
    brand.setText("");
    price.setText("");
    row.setClickable(true);
    Log.i("Table Layout is Null :::", String.valueOf(tableLayout.getChildCount()));
    id++;

    if(tableLayout.getChildCount() <= 2) {
        Log.i("Table Layout is Null :::", String.valueOf(tableLayout.getChildCount()));
    }
}