6

我正在 Android 中开发 Expandablelistview。

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

这段代码运行良好,我可以在每个 Group 项目中看到不同的 ChildView。

在 ChildView 中,我有一个 EditText,当我单击完成按钮时,我想获取用户输入的所有 EditText 和 Spinner 值。

当我当时单击完成按钮时,我想获取用户在组项目Customer Location等中填写的详细信息。

例如:有问题的图像。我想获取由用户vehicleTEST组项填写的输入详细信息。

请,我需要帮助,我想获取用户在每个组项子项的 EditText 中填写的值。

这是我的代码:

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>

图片 : 在此处输入图像描述

在此处输入图像描述

谢谢!

4

1 回答 1

1

您将无法浏览列表并直接从视图中获取值,这可能是您计划做的事情。

相反,只需在用户输入值时跟踪它们。您可以在适配器或活动中保留字段名称 -> 值的哈希映射。每次用户更改字段中的值时,使用哈希图中的新值更新该字段。当他们按下完成时,您可以遍历地图以获取用户更改的所有字段的列表以及他们更改为的值。

要在用户更改字段时获取字段值,请使用TextWatcher

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

您可以将其添加到适配器中的 getChildView 中。您可以从父项和子项的位置派生 myFieldName。

于 2013-07-17T15:20:49.160 回答