我有一个列表视图适配器。有一个列表,它是一个 EditText。我有一个按钮。现在单击按钮时,我想获取edittext的值。注意:适配器中有两个列表。EditText 的位置是 1,另一个列表的位置是 0。
在 onCreate() 中:
private ListView listView;
private List<BaseListElement> listElements;
listElements = new ArrayList<BaseListElement>();
listElements.add(new FriendList(0));
listElements.add(new TextArea(1));
if (savedInstanceState != null) {
// Restore the state for each list element
for (BaseListElement listElement : listElements) {
listElement.restoreState(savedInstanceState);
}
}
// Set the list view adapter
listView.setAdapter(new ActionListAdapter(getActivity(),
R.id.list, listElements));
shareButton = (Button) view.findViewById(R.id.sharebtn);
Svalue = (EditText) view.findViewById(R.id.Tstatus);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String value = Svalue.getText().toString();
if(value != null && value.length()>0){
Log.e("msg", value);
}
适配器:
private class ActionListAdapter extends ArrayAdapter<BaseListElement> {
private final String Tag = "ActionAdapter";
int k = 0;
private List<BaseListElement> listElements;
public ActionListAdapter(Context context, int resourceId, List<BaseListElement> listElements) {
super(context, resourceId, listElements);
this.listElements = listElements;
for (int i = 0; i < listElements.size(); i++) {
listElements.get(i).setAdapter(this);
}
k = 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
if(position == 0){
LayoutInflater inflater =
(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.shareview, null);
}else{
LayoutInflater inflater =
(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.textarea, null);
}
k = k++;
String ki = Integer.toString(position);
Log.e(Tag, ki);
}
else{
String ki = Integer.toString(position);
Log.e(Tag, "else : "+ki);
}
BaseListElement listElement = listElements.get(position);
view.setOnClickListener(listElement.getOnClickListener());
ImageView icon = (ImageView) view.findViewById(R.id.icon);
TextView text1 = (TextView) view.findViewById(R.id.withWhom);
TextView text2 = (TextView) view.findViewById(R.id.selectFriend);
EditText status = (EditText) view.findViewById(R.id.Tstatus);
if (icon != null && position == 0) {
icon.setImageDrawable(listElement.getIcon());
}
if (text1 != null && position == 0) {
text1.setText(listElement.getWithWhom());
}
if (text2 != null && position == 0) {
text2.setText(listElement.getSelectFriend());
}
if(status != null)
Log.e("adapter class", status.getText().toString());
return view;
}
}