如何从可扩展列表视图的子视图中的多个编辑文本中获取数据?

时间:2015-06-03 08:54:10

标签: android expandablelistview

我想从可扩展列表视图中获取编辑文本数据,如何在活动中获取编辑文本的位置,因为我在Adapter上找到了编辑文本的ID?

这是我的代码

活动

    public class AddNewAddressActivity extends Activity implements OnClickListener
{

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    //

    private RelativeLayout _mainRl = null;
    private RelativeLayout _rlBackToCart  = null;
    private LinearLayout _llPay = null;
    private TextView _tvCart = null;
    private TextView _tvHeader = null;
    private TextView _tvPay = null;
    private TextView _tvfromListTxt = null;
    private TextView _tvnewoneTxt = null;

    private ImageView _addNewAddressBtn = null;
    private TextView _saveAddressBtn = null;

    //tint
    SystemBarTintManager tintManager;
    private RelativeLayout _rlHeader = null;

    //font 
    Typeface font;
    int _maintainAddress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_new_address_screen);

        _mainRl  =(RelativeLayout) findViewById(R.id.mainRl);
        _rlBackToCart = (RelativeLayout) findViewById(R.id.back_to_cart_rl);
        _llPay = (LinearLayout) findViewById(R.id.pay_ll);
        _tvCart = (TextView) findViewById(R.id.cartTxt);
        _tvHeader = (TextView) findViewById(R.id.add_new_address_headertv);
        _tvPay = (TextView) findViewById(R.id.payTxt);
        _tvfromListTxt  = (TextView) findViewById(R.id.fromListTxt);
        _tvnewoneTxt = (TextView) findViewById(R.id.newoneTxt);
        _rlHeader  = (RelativeLayout) findViewById(R.id.header_rl);
        _addNewAddressBtn = (ImageView) findViewById(R.id.add_new_address);
        _saveAddressBtn = (TextView) findViewById(R.id.saveAddressBtn);


        expListView = (ExpandableListView) findViewById(R.id.exp_list);
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // preparing list data
        //      prepareListData();


        //font
        font = Typeface.createFromAsset(getAssets(),"century_gothic.ttf");
        _tvCart.setTypeface(font);
        _tvHeader.setTypeface(font);
        _tvPay.setTypeface(font);
        _tvfromListTxt.setTypeface(font);
        _tvnewoneTxt.setTypeface(font);

        //on click
        _rlBackToCart.setOnClickListener(this);
        _llPay.setOnClickListener(this);
        _addNewAddressBtn.setOnClickListener(this);
        _mainRl.setOnClickListener(this);
        _saveAddressBtn.setOnClickListener(this);


        // changestatus bar code
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {

            Constant.changeStatusBarColor(AddNewAddressActivity.this);
        }else {


            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)_rlHeader.getLayoutParams();
            params.setMargins(0,28,0,0); 
            _rlHeader.setLayoutParams(params);


            setTranslucentStatus(true);
            tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.grey);


        }
    }

    private void setTranslucentStatus(boolean on) {
        // TODO Auto-generated method stub

        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);

    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left); //for back button
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.back_to_cart_rl:
            finish();
            overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left); //for back button
            break;
        case R.id.pay_ll:
            Intent _int = new Intent(AddNewAddressActivity.this, ConfirmOrderActivity.class);
            startActivity(_int);
            overridePendingTransition(R.anim.enter, R.anim.exit);
            break;
        case R.id.add_new_address:
            if(listDataHeader.size()<5)
            {
                _maintainAddress = _maintainAddress + 1;
                System.out.println("value maintain"+_maintainAddress);
                listDataHeader.add("Address "+_maintainAddress);

                // Adding child data
                List<String> Address1 = new ArrayList<String>();
                Address1.add("");
                Address1.add("");
                Address1.add("");
                Address1.add("");
                Address1.add("");
                Address1.add("");
                Address1.add("");
                Address1.add("");
                Address1.add("");

                int i = _maintainAddress - 1;
                listDataChild.put(listDataHeader.get(i), Address1); // Header, Child data

                listAdapter = new ExpandableListAdapter(AddNewAddressActivity.this, listDataHeader, listDataChild);

                // setting list adapter
                expListView.setAdapter(listAdapter);
                listAdapter.notifyDataSetChanged();
            }
            break;

        case R.id.mainRl:
            Constant.hideKeyBoard(AddNewAddressActivity.this);
            break;

        case R.id.saveAddressBtn:
            for(int i = 0;i<listDataHeader.size();i++)
            {
                for (int j = 0; j < listDataChild.size(); j++) 
                {

                }

            }

            break;

        default:
            break;
        }

    }

点击saveAddress按钮我想获取适配器的编辑文本值

这是我的适配器类

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;
    Typeface font;
    public static EditText txtListChild = null;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;

    }


    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.exp_list_item, null);
        }

        txtListChild = (EditText) convertView
                .findViewById(R.id.expEdt);
        ImageView _expandcountry = (ImageView) convertView.findViewById(R.id.exapandCountry);
        Spinner _spnCountry = (Spinner) convertView.findViewById(R.id.country_spinner);
        ArrayList<String> _countyArrList = new ArrayList<String>();
        _countyArrList.add("India");
        _countyArrList.add("USA");
        _countyArrList.add("UAE");
        _countyArrList.add("South Africa");
        ArrayAdapter<String> _colorAdap = new ArrayAdapter<String>(_context,
                R.layout.spinner_country, _countyArrList);
        _colorAdap.setDropDownViewResource(R.layout.spinner_country); 
        _spnCountry.setAdapter(_colorAdap);
        _spnCountry.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {

                /*if (_transferTo[position].equalsIgnoreCase("Select location")) {
                    _transferToSpinner.setSelection(position);  
                    _location = _transferToSpinner.getSelectedItem().toString();
                } else {
                    _location = _transferToSpinner.getSelectedItem().toString();
                    //System.out.println("Selected location" + _location);
                    _transferToSpinner.setSelection(position);
                }*/

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

        txtListChild.setText(childText);
        if(childPosition==0)
        {
            txtListChild.setHint("Name");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);

        }
        else if(childPosition==1)
        {
            txtListChild.setHint("Last Name");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);
        }
        else if(childPosition==2)
        {
            txtListChild.setHint("Address Line 1");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);
        }
        else if(childPosition==3)
        {
            txtListChild.setHint("Address Line 2");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);
        }
        else if(childPosition==4)
        {
            txtListChild.setHint("Phone");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);
        }

        else if(childPosition==5)
        {
            txtListChild.setHint("City");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);
        }
        else if(childPosition==6)
        {
            txtListChild.setHint("State");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);
        }
        else if(childPosition==7)
        {
            txtListChild.setHint("Zip Code");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            txtListChild.setVisibility(View.VISIBLE);
            _expandcountry.setVisibility(View.GONE);
            _spnCountry.setVisibility(View.GONE);
        }
        else if(childPosition==8)
        {
            txtListChild.setHint("Country");
            txtListChild.setHintTextColor(_context.getResources().getColor(R.color.grey));
            _expandcountry.setVisibility(View.VISIBLE);
            _spnCountry.setVisibility(View.VISIBLE);
            txtListChild.setVisibility(View.GONE);
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) 
    {
        System.out.println("group position"+groupPosition);
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.exp_list_group, null);
        }
        Typeface.createFromAsset(_context.getAssets(),"century_gothic.ttf");

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.addressTxt);
        LinearLayout _panelLayout = (LinearLayout) convertView.findViewById(R.id.exp_maingrp_rl);
        ImageView _rightIcon = (ImageView) convertView.findViewById(R.id.rightBtn);
        ImageView _openCloseBtn = (ImageView) convertView.findViewById(R.id.openCloseBtn);


        int panelBackId = isExpanded ?  R.drawable.panel_active_tab: R.drawable.panel_inactive ;
        _panelLayout.setBackgroundResource(panelBackId);


        int textResourceId = isExpanded ? _context.getResources().getColor(R.color.white) : _context.getResources().getColor(R.color.grey) ;
        lblListHeader.setTextColor(textResourceId);
        lblListHeader.setTypeface(font);
        lblListHeader.setText(headerTitle);

        int imageResourceId = isExpanded ? R.drawable.right_white : R.drawable.right_black  ;
        _rightIcon.setImageResource(imageResourceId);

        int imageOpenCloseId = isExpanded ? R.drawable.arrow_bottom : R.drawable.forward_iv_black  ;
        _openCloseBtn.setImageResource(imageOpenCloseId);


        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

我将编辑文本设置为静态以便在活动中访问,但我无法获取编辑文本的位置,因为我使用多个编辑文本。

请帮助我。

建议赞赏

谢谢

3 个答案:

答案 0 :(得分:1)

  1. 您必须将数据存储到应用程序类中才能访问“保存”按钮。
  2. 您可以在适配器中创建一个公共方法,该方法将返回单击的编辑文本数据。

    private static ArrayList<String> selectedEditTextData;
    
    public ArrayList<String> getClickedEditTextData() {
            return selectedEditTextData;
        }
    
  3. 将点击的编辑文本数据添加到getChildView onClickListner上的ArrayList中。

  4. 在应用程序类中创建一个arraylist来存储所有编辑文本视图的值,为其生成getter和setter。
  5. 在将适配器设置为列表后的活动中。
  6. getClickedEditTextData()获取值到活动中并设置为全局arraylist。
  7. 在应用程序类的保存按钮上使用getArraylist方法。
  8. 基本上,您可以将所有输入的数据分别添加到ArrayList中,并从中添加getData。

答案 1 :(得分:1)

我在RND之后找到了这个问题的解决方案,我希望它对其他开发者有用..

这是我在适配器中的代码

全球价值

public static HashMap<String,String>  arrayList = new HashMap<String,String>();

之后

txtListChild.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    System.out.println("Value : " + childPosition  +txtListChild.getText().toString());
                    name = childPosition+""+txtListChild.getText().toString();
                    if(groupPosition==0)
                    {
                        arrayList.put(String.valueOf(groupPosition),name);
                    }
                    else if(groupPosition==1)
                    {
                        arrayList.put(String.valueOf(groupPosition),name);
                    }
                    else if(groupPosition==2)
                    {
                        arrayList.put(String.valueOf(groupPosition),name);
                    }
                    else if(groupPosition==3)
                    {
                        arrayList.put(String.valueOf(groupPosition),name);
                    }
                    else if(groupPosition==4)
                    {
                        arrayList.put(String.valueOf(groupPosition),name);
                    }

                }
            });

点击保存按钮,我们执行此代码

case R.id.saveAddressBtn:


            for(Map.Entry m:ExpandableListAdapter.arrayList.entrySet()){  
                System.out.println("Dats in arralist===="+m.getKey()+" "+m.getValue());  
            }  

            //end changes

            break;

答案 2 :(得分:0)

您必须使用此方法找出点击的子编辑文本位置

    // Listview on child click listener
                expListView.setOnChildClickListener(new OnChildClickListener() {

                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                        // TODO Auto-generated method stub
                        /*
                         * Toast.makeText( getActivity(), listDataHeader.get(groupPosition) + " : " + listDataChild.get( listDataHeader.get(groupPosition)) .get(childPosition),
                         * Toast.LENGTH_SHORT).show();
                         */
String editTextData = ((EditText)v).getText().toString().trim();
                        return false;
                    }
                });

在此方法中,您可以找到单击的子位置,并单击所关注的视图作为编辑文本并单击组位置。 如果它会帮助你,那么接受它!!