如何在android中使用baseadapter从listview获取edittext和text值

时间:2014-12-02 10:10:06

标签: android listview baseadapter

我正在使用基础适配器,但是当单击按钮时无法获取值,只传递了值,但编辑的值不是gettng。为什么这样,你能找到我的代码中哪里出错了。

public class MainActivity extends Activity {

    ListView listview;

    Button getall;
     HashMap<String,String> row;
    HashMap<Integer, String> mValues;
    private EditorAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview=(ListView) findViewById(R.id.ageListView);
        getall=(Button) findViewById(R.id.btn_ger);

        ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();

       for (int i = 0; i < 3; i++) {
            row = new HashMap<String, String>();
            row.put("name","name  "+i);
            row.put("value",""+i);
            data.add(row);
       }
        adapter= new EditorAdapter(getBaseContext(), data);
        listview.setAdapter(adapter);

        //notifyDataSetChanged();
        getall.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void onClick(View v) {
                String allValues="";
                ArrayList<String> valueList = new ArrayList<String>();
                for (int i=0;i<adapter.getCount();i++){
                    allValues +=((HashMap<String,String>)adapter.getItem(i)).get("value")+ ",";
                    valueList.add(((HashMap<String,String>)adapter.getItem(i)).get("value"));
                }
                // use this valueList as per ur requirement
                allValues = allValues.substring(0,allValues.length()-1);
                Toast.makeText(MainActivity.this,allValues,Toast.LENGTH_LONG).show();

            }
        });



    }
    class EditorAdapter extends BaseAdapter {

        Context context;
        ArrayList<HashMap<String,String>> data;

        public EditorAdapter(Context context, ArrayList<HashMap<String,String>> data){

            this.context = context;
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int pos) {
            return data.get(pos);
        }

        @Override
        public long getItemId(int pos) {
            return pos;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.custom_view,null,false);
                holder.editPropertyValue = (EditText) convertView.findViewById(R.id.edit_age);
                holder.propertyName = (TextView) convertView.findViewById(R.id.txt_name);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.propertyName.setText(data.get(position).get("name"));
            holder.editPropertyValue.setText(data.get(position).get("value"));

            convertView.setTag(holder);
            return convertView;
        }

        class ViewHolder {
            EditText editPropertyValue;
            TextView propertyName;
        }

}

这里,如果编辑后点击按钮,则只显示前一个值0,1,2。如果我输入其他价值,为什么不呢。

1 个答案:

答案 0 :(得分:0)

试试这个:

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.custom_view,null,false);
                holder.editPropertyValue = (EditText) convertView.findViewById(R.id.edit_age);
                holder.propertyName = (TextView) convertView.findViewById(R.id.txt_name);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.propertyName.setText(data.get(position).get("name"));
            holder.editPropertyValue.setText(data.get(position).get("value"));

            holder.editPropertyValue.addTextChangedListener(new TextWatcher(){// add text watcher to update your data after editing
                 public void afterTextChanged(Editable s) {
                     HashMap<String,String> row = data.get(position); // get the item from your ArrayList
                     row.put("value", holder.editPropertyValue.getText());// update your HashMap
                     data.set(position, row); // update your data ( your ArrayList )
                 }
                 public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                 public void onTextChanged(CharSequence s, int start, int before, int count){}
            }); 
            convertView.setTag(holder);
            return convertView;
        }