在ExpandableListView中

时间:2017-10-05 09:38:35

标签: android onclick expandablelistview

我在列表的每个孩子中使用numberPicker实现ExpandableListView,例如this image

现在,当我点击按钮时,我必须从按钮旁边的editText中获取文本。 问题是当有多个子项展开时,如果我单击子项的那个不是最后一个被展开的按钮,该按钮会收集最后一个子项的文本。 因此,我想获得groupPosition,其中有我点击的按钮。

这是我的代码:

public void buttonOK_OnClick(View view){
    buttonOK = (Button)this.findViewById(R.id.buttonOK);
    EditText editText = DepositAdapter.getnumberPickerDisplayDeposit();
    String myText = editText.getText().toString();
    Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
}

这是适配器类:

public class DepositAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Transaction> transactions = ProductsListActivity.getDb_transaction().read(context, 2);
    private static View view;

    public DepositAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return transactions.size();
    }

    @Override
    public int getChildrenCount(int i) {
        // se l'elemento è cliccato allora return 0 altrimenti return 1
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return null;
    }

    @Override
    public Object getChild(int i, int i1) {
        return null;
    }

    @Override
    public long getGroupId(int i) {
        return 0;
    }

    @Override
    public long getChildId(int i, int i1) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_row, null);
        }

        Product product = LoginActivity.getDb_products().checkProduct(transactions.get(groupPosition).getBarcode());

        ((CheckedTextView) convertView).setText(product.getName() + "\nEAN: " + transactions.get(groupPosition).getBarcode());
        ((CheckedTextView) convertView).setChecked(isExpanded);
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent){
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_row, null);
        }
        view = convertView;
        final EditText e = getnumberPickerDisplayDeposit();
        e.setText(String.valueOf(transactions.get(groupPosition).getQuantity()));
        e.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // TODO auto-generated method stub!
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                // TODO auto-generated method stub!
            }

            @Override
            public void afterTextChanged(Editable edt) {
                String quantity = String.valueOf(transactions.get(groupPosition).getQuantity() + 1);
                // viene controllato se l'utente inserisce 0, valore non possibile, allora il valore viene sostituito con 1
                if (edt.toString().equals(quantity))
                    e.setText(String.valueOf(transactions.get(groupPosition).getQuantity()));
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

    public static EditText getnumberPickerDisplayDeposit(){
        EditText textViewNumberPickerDisplayDeposit = (EditText) view.findViewById(R.id.numberPickerDisplayDeposit);
        return textViewNumberPickerDisplayDeposit;
    }
}

0 个答案:

没有答案