从位于android中的列表视图中的EditText获取值

时间:2011-08-11 12:28:26

标签: android

我不知道如何在列表视图中获取编辑文本的值。我使用simpleadapter填充列表视图中的数据。

是否有人会提供样本代码,用于从列表中对edittext值进行格式化

4 个答案:

答案 0 :(得分:4)

for (int i = 0; i < ListSortOrder.getAdapter().getCount(); i++) {

        HashMap result = (HashMap)ListSortOrder.getItemAtPosition(i);       
        **View vListSortOrder;
        vListSortOrder=ListSortOrder.getChildAt(i);     

        TextView Sort_DeliveryOrder=(TextView)vListSortOrder.findViewById(R.id.et_Sort_Order);**

        initialSortOrder.put("DeliveryOrder",Sort_DeliveryOrder.getText().toString());
        dbAdapter = new DatabaseAdapter(this);
        dbAdapter.open();
        dbAdapter.BeginTransaction();
        dbAdapter.UpdateRecord("tblDelivery", initialSortOrder, "PKDelivery" + "=" + result.get("Sort_PKDelivery").toString(), null);
}

答案 1 :(得分:3)

也许你已经有了答案,但我会把我的解决方案放在这里,因为我看到了很多没有好答案的问题。 我有一个自定义ArrayAdapter,其中包含更新源列表“telefones”的fowling代码。

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.telefone_form, parent, false);

    tipoEditText = (EditText) view.findViewById(R.id.telefone_form_tipo);
    telefoneEditText = (EditText) view.findViewById(R.id.telefone_form_telefone);
    Telefone telefone = telefones.get(position);
    tipoEditText.setText(telefone.getTipo());
    telefoneEditText.setText(telefone.getTelefone());

    final int i = position;

    tipoEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            telefones.get(i).setTipo(s.toString());
        }
    });

    telefoneEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            telefones.get(i).setTelefone(s.toString());
        }
    });

在设置适配器的Activity中,我使用代码获取所有更改:

for (int i = 0; i < telefoneListView.getAdapter().getCount(); i++) {
    View viewTelefone = telefoneListView.getChildAt(i);
    if (viewTelefone.findViewById(R.id.telefone_form_tipo) != null) {
        EditText tipoEditText = (EditText) viewTelefone.findViewById(R.id.telefone_form_tipo);
        EditText telefoneEditText = (EditText) viewTelefone.findViewById(R.id.telefone_form_telefone);
        Log.d(TAG, tipoEditText.getText().toString() + ":" + telefoneEditText.getText().toString());
    }
}

问候,Marcello

答案 2 :(得分:2)

我面临同样的问题,但解决了它。所以我在分享我的所作所为:

  • 逻辑:当用户填写listview的EditText中的值时,必须存储带有索引的值以供将来使用。因此,在其TouchListener中创建ArrayList的实例并用空值填充它。现在,当用户更改其值时,新值将在ArrayList的最后位置(存储在TouchListener中的索引)中更新。

    • 问题:当用户在EditText.So中更改值时,每次调用TouchListener时,它也会填充空值,当使用此ArrayList时,它必须删除空值。

    *解决方案

    **In getView set tag (position) in EditText so that we can identify it on its events.
    
            //On TouchListner
    
    
      myEditText.setOnTouchListener(new View.OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
    
        HashMap<String, String> hashMap=new HashMap<String,String>();                                  
        hashMap.put("", "");
        MyArrayList.add(hashMap);
        if(MyArrayList.size()>0)
        {
         index=MyArrayList.size()-1;
        }
        }
            }
            return false;
        }
    });
    
    myEditText.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
                            System.out.println(myEditText.getText().toString());
                String quanString=String.valueOf(myEditText.getTag());
                HashMap<String, String> hashMap=new HashMap<String, String>();
                hashMap.put(quanString,value);
                MyArrayList.set(index,hashMap);
    
        }
    });
    return listRowItemView;
        }
    
    //Let Suppose values had to fetch on button click--
    
     findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("MyArrayList....value before removing====>"+MyArrayList);
                    // TODO Auto-generated method stub
                    for(int i=0;i<MyArrayList.size();i++)
                    {
                        HashMap<String, String> hashMap=new HashMap<String, String>();
                        hashMap= MyArrayList.get(i);
                        for (Entry<String, String> entry : hashMap.entrySet()) {
                              String key = entry.getKey();
                              String value = entry.getValue();
                              if(key.length()<=0 & value.length() <=0)
                              {
                                  MyArrayList.remove(i);
                                  System.out.println("At index"+i+" key value was empty so it is removes");
                                  System.out.println("MyArrayList..value after removeing empty values====>"+MyArrayList);
                              }
    
                            }
                    }
    
                }
            });
    

答案 3 :(得分:1)

package com.example.user.listview;

import android.content.ContentValues;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    private ListView lv1;
    private EditText Et1;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv1 = (ListView) findViewById(R.id.lv);
        btn = (Button) findViewById(R.id.btn);
        Et1 = (EditText) findViewById(R.id.ETName);
        final ArrayList<String> List = new ArrayList<>();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item = Et1.getText().toString();
                List.add(item);

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,List);

                lv1.setAdapter(adapter);
            }
        });
    }
}