我想获取我的 ListView 中存在的所有 EditText 元素的所有值。这是我的代码:
final ListView editorList = (ListView) findViewById(R.id.editorList);
final EditorAdapter adapter = new EditorAdapter(context, data);
editorList.setAdapter(adapter);
Button commitButton = (Button) findViewById(R.id.commit_button);
commitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//System.out.println("Size of List : " + editorList.getChildCount());
for(int i =0;i< data.size() ;i++){
System.out.println("Size of List : " + data.size());
EditText value = adapter.getItem(i);
String propertyValue = value.getText().toString();
System.out.println("PropertyValue : " + propertyValue);
}
} catch (Exception e){
e.printStackTrace();
}
}
});
这是我的适配器类:
package in.omerjerk.preferenceseditor;
public class EditorAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String,String>> data;
EditText[] mHolders;
public EditorAdapter(Context context, ArrayList<HashMap<String,String>> data){
this.context = context;
this.data = data;
System.out.println("No . of items in nodes"+data.size());
mHolders = new EditText[data.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public EditText getItem(int pos) {
// TODO Auto-generated method stub
return mHolders[pos];
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
return pos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null){
System.out.println("CONVERT VIEW IS NULL");
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row_edit_string,null,false);
holder.editPropertyValue = (EditText) convertView.findViewById(R.id.propertyValue);
holder.propertyName = (TextView) convertView.findViewById(R.id.propertyName);
holder.propertyName.setText(data.get(position).get("propertyName"));
holder.editPropertyValue.setText(data.get(position).get("propertyName"));
convertView.setTag(holder);
}else{
System.out.println("CONVERT VIEW NOT NULL");
holder = (ViewHolder) convertView.getTag();
holder.propertyName.setText(data.get(position).get("propertyName"));
holder.editPropertyValue.setText(data.get(position).get("propertyName"));
convertView.setTag(holder);
mHolders[position] = new EditText(context);
mHolders[position] = holder.editPropertyValue;
}
return convertView;
}
}
我的输出出现奇怪的错误。mHolders 数组最多只包含 6-7 个元素,这些元素在整个数组中重复出现。我能够获取 EditText 的值,但错误不正确。