本文介绍了安卓:对按钮单击ExpandableListView getChild()的EditText值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Expandablelistview的Andr​​oid系统。

I'm developing Expandablelistview in Android.

在GroupView只有一个标题,每一个组都有不同的ChildView。

In the GroupView has only a title and every group has different ChildView.

这code工作正常,我能看到不同的ChildView的每一个组项

This code is working fine and i'm able to see different ChildView's in every Group item.

在ChildView我有一个EditText,当我点击完成按钮,我想所有的EditText和微调框的值,它输入是由用户。

In ChildView i have an EditText and when i click on done button i want to get all EditText and Spinner values which are inputs by user.

当我点击完成按钮在那个时候,我想这是在集团项目客户 位置等。

When i click on done button at that time, i want to get details which is filled by user in Group items Customer Location etc.

有关一个例子:在质询所示的图像。我想要得到的输入信息是在充满由用户测试组项目。

For a example : the image shown in question. I want to get the input details which is filled by user in vehicle and TEST group item.

请,我需要帮助,我想这是每一个组项目的孩​​子的EditText上填补了用户的值。

Please, I need help, i want to get values which is filled by user in every group item child's EditText.

下面是我的code:

MainActivity.java

public class MainActivity extends Activity {

    Context mContext;
    private ExpandableListView mExpandableListView;
    CustomAdapter mCustomAdapter;

    ArrayList<String> newCalibration = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mContext = MainActivity.this;

        newCalibration.add("CUSTOMER");
        newCalibration.add("LOCATION");
        newCalibration.add("VEHICLE");
        newCalibration.add("TEST");
        newCalibration.add("ABC");

        /* genarate data for list view */
        // genarateData();
        /* instantiate adapter with our item list */
        mCustomAdapter = new CustomAdapter(mContext, newCalibration);
        /* we get list view */
        mExpandableListView = (ExpandableListView) findViewById(R.id.explist_tools);
        /* set adapter to list view */
        mExpandableListView.setAdapter(mCustomAdapter);

        mExpandableListView
                .setOnGroupExpandListener(new OnGroupExpandListener() {

                    int previousGroup = -1;

                    @Override
                    public void onGroupExpand(int groupPosition) {
                        // TODO Auto-generated method stub
                        if (groupPosition != previousGroup) {
                            mExpandableListView.collapseGroup(previousGroup);
                            previousGroup = groupPosition;
                        }
                    }
                });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

CustomAdapter.java

public class CustomAdapter extends BaseExpandableListAdapter {

    private LayoutInflater layoutInflater;

    /* list of parent */
    private ArrayList<String> mParents;

    public CustomAdapter(Context context, ArrayList<String> parents) {
        super();
        this.mParents = parents;
        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object getChild(int parentPosition, int childPosition) {
        return null;
    }

    @Override
    public long getChildId(int parentPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int parentPosition, int childPosition, boolean b,
            View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = layoutInflater
                    .inflate(R.layout.list_item_child, null);

        }
        EditText mEdTxt_1 = (EditText) convertView.findViewById(R.id.edtxt_1);
        EditText mEdTxt_2 = (EditText) convertView.findViewById(R.id.edtxt_2);
        EditText mEdTxt_3 = (EditText) convertView.findViewById(R.id.edtxt_3);
        EditText mEdTxt_4 = (EditText) convertView.findViewById(R.id.edtxt_4);
        EditText mEdTxt_5 = (EditText) convertView.findViewById(R.id.edtxt_5);
        RelativeLayout relativeLayout = (RelativeLayout) convertView
                .findViewById(R.id.relativeLayout);
        mEdTxt_1.setVisibility(View.VISIBLE);
        mEdTxt_2.setVisibility(View.VISIBLE);
        mEdTxt_3.setVisibility(View.VISIBLE);
        mEdTxt_4.setVisibility(View.VISIBLE);
        mEdTxt_5.setVisibility(View.VISIBLE);
        relativeLayout.setVisibility(View.VISIBLE);

        if (mParents.get(parentPosition).equals("CUSTOMER")) {
            mEdTxt_4.setVisibility(View.GONE);
            mEdTxt_5.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.GONE);
        } else if (mParents.get(parentPosition).equals("LOCATION")) {
            mEdTxt_4.setVisibility(View.GONE);
            mEdTxt_5.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.GONE);
        } else if (mParents.get(parentPosition).equals("VEHICLE")) {
            relativeLayout.setVisibility(View.GONE);
        } else if (mParents.get(parentPosition).equals("TAXIMETER/BCT")) {
            mEdTxt_3.setVisibility(View.GONE);
            mEdTxt_4.setVisibility(View.GONE);
            mEdTxt_5.setVisibility(View.GONE);
        } else if (mParents.get(parentPosition).equals("TIRE SPECS")) {
            mEdTxt_4.setVisibility(View.GONE);
            mEdTxt_5.setVisibility(View.GONE);
            relativeLayout.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int parentPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int parentPosition) {
        // TODO Auto-generated method stub
        return mParents.get(parentPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return mParents.size();
    }

    @Override
    public long getGroupId(int parentPosition) {
        // TODO Auto-generated method stub
        return parentPosition;
    }

    @Override
    public View getGroupView(int parentPosition, boolean isExpanded,
            View convertView, ViewGroup viewGroup) {
        TextView groupName = null;
        String parent = (String) getGroup(parentPosition);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item_parent,
                    null);
        }
        groupName = (TextView) convertView.findViewById(R.id.txt_parentname);
        groupName.setText(parent);
        return convertView;
    }

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

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

}

list_item_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_child"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edtxt_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:inputType="text" />

    <EditText
        android:id="@+id/edtxt_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:inputType="text" />

    <EditText
        android:id="@+id/edtxt_3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:inputType="text" />

    <EditText
        android:id="@+id/edtxt_4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:inputType="text" />

    <EditText
        android:id="@+id/edtxt_5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:inputType="text" />

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Radio 1" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Radio 2" />
        </RadioGroup>

        <TextView
            android:id="@+id/txt_childname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/radio_group"
            android:text="TaxiMeter ?"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/radio_group"
            android:text="Switch" />
    </RelativeLayout>

</LinearLayout>

图片:

Images :

谢谢!

推荐答案

您不会可以去通过列表,并获得价值直接的意见,这可能是你正在计划做。

You aren't going to be able to go through the list and get the values from the views directly, which is probably what you were planning on doing.

相反,只要跟踪值作为用户输入他们。你可以保留的字段名的哈希地图 - >价值适配器或活动。每当用户在一个字段的值发生变化,更新场HashMap中的新价值。当他们preSS做,你可以遍历地图,让所有的用户更改了字段列表,他们改变了他们。

Instead, just keep track of the values as the user enters them. You can keep a hash map of field name -> value in your adapter or activity. Every time the user changes a value in a field, update that field with the new value in the hashmap. When they press done, you can iterate over the map to get a list of all the fields the user changed and the values they changed them to.

要获得字段的值作为用户改变他们,用 TextWatcher :

To get the values of the fields as the user changes them, use a TextWatcher:

myEditText.addTextChangedListener(new TextWatcher() {
    ...
    @Override
    onTextChanged(CharSequence newText, int start, int before, int count) {
        myMapOfFieldsToValues.set(myFieldName, newText);
    }
}

您可以添加到getChildView在您的适配器。您可以从父母和孩子的立场得到myFieldName。

You can add that to getChildView in your adapter. You can derive myFieldName from the position of the parent and child.

这篇关于安卓:对按钮单击ExpandableListView getChild()的EditText值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:20