1

如何从编辑文本中获取值。

大家好,我正面临着一个 android 任务的问题,其中编辑文本中有很多可用的编辑文本。

在此处输入图像描述

我需要从 1 到 4 的所有编辑文本中获取值..? 下面是我的Adapter的get view方法。

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.social_media_row, null);

            holder = new ViewHolder();

            holder.social_id = (EditText) convertView.findViewById(R.id.socialMediaId);

            convertView.setTag(holder);

            holder.social_id.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
                @Override
                public void afterTextChanged(Editable s) {
                  Log.i("AfterTextChange",holder.social_id.getText().toString());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SocialMediaModel contact = contactsList.get(position);

        holder.social_id.setText(contact.getSocialMediaId());

        holder.social_id.setTag(contact);
        return convertView;
    }

更新:我想在用户单击按钮时获取值

savebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

  /here what i can do    


            }
        });
4

4 回答 4

1
        //at top Globally declare it
        private String[] valueList = new String[];

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
                    final ViewHolder holder;
                    if (convertView == null) {

                        convertView = LayoutInflater.from(getContext()).inflate(R.layout.social_media_row, null);

                        holder = new ViewHolder();

                        holder.social_id = (EditText) convertView.findViewById(R.id.socialMediaId);

                        convertView.setTag(holder);

                        holder.social_id.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            }
                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {

                         valueList[pos] = s.toString();

                            }
                            @Override
                            public void afterTextChanged(Editable s) {
                              Log.i("AfterTextChange",holder.social_id.getText().toString());
                            }
                        });
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }

                    SocialMediaModel contact = contactsList.get(position);

                    holder.social_id.setText(contact.getSocialMediaId());

                    holder.social_id.setTag(contact);
                    return convertView;
                }

// method to  get Values
public String[] getValueList(){
        return valueList;
    }

现在在按钮单击事件上放置此代码

String[] myvl = yourAdapteName.getValueList();
于 2016-09-24T08:08:05.010 回答
0

我希望这段代码对你有帮助

使用适配器单元位置

遵循此代码

在 getView 中(在适配器类中)

edittext.getText().toString();
于 2016-09-24T09:11:25.087 回答
0

您可以使用列表位置作为参数为每个 EditText 实现这样的方法:

public String getValueFromFirstEditText(int position){
      //here you need to recreate the id for the first editText
     String result = textValues.get("theFirstEditTextAtPos:"+position);
     if(result ==null)
          result = "default value";
 return result;

}

于 2016-09-24T07:43:27.283 回答
0

在您的声明中声明这一点

    protected AdapterView.OnItemClickListener messageClickedHandler;

使用适配器视图的列表视图的单击事件

   messageClickedHandler = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view, int position, long id) {
            String strName=rowItems.get(position).getStrName();
            Toast.makeText(getApplicationContext(),"Name : "+strName , Toast.LENGTH_LONG).show();
            }
        };
        list.setOnItemClickListener(messageClickedHandler);
    }

rowItems 是一个数组列表 Object ,来自用户定义的类的 getStrName()。

于 2016-09-24T13:21:59.723 回答