如何从编辑文本中获取值。
大家好,我遇到了 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
}
});
最佳答案
//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();
关于android - 如何从 Listview 编辑文本字段中获取文本值......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673824/