如何从编辑文本中获取值。
大家好我即将面临android任务的问题,其中编辑文本中有很多编辑文本。
我需要从1到4获取所有编辑文本的值..? 下面是我的适配器查看方法。
@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
}
});
答案 0 :(得分: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();
答案 1 :(得分: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;
}
答案 2 :(得分:0)
我希望此代码可以帮助您
USE适配器单元格位置
按照此代码
在getView(在适配器类中)
edittext.getText().toString();
答案 3 :(得分: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()。