如何从listview中的所有edittext字段中获取值

时间:2014-08-10 16:01:57

标签: android listview android-edittext adaptor

我希望得到所有文本字段的值,然后按下。

有时它会给出正确的值,有时会给出log cat消息:

08-10 11:41:48.540: D/list Item No0=(1127): 1
08-10 11:41:48.540: D/list Item No1=(1127): 2
08-10 11:41:48.540: D/list Item No2=(1127): 1

我只是失去了如何从所有edittext字段获取值,请帮助

EditTextActivity.java

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import pl.looksok.listviewdemo.EditTextAdaptor;

public class EditTextActivity extends Activity {

    ArrayList<String> Fields;
    ArrayList<String> Values; 

    ListView atomPaysListView;

    private EditTextAdaptor adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        Fields = new ArrayList<String>();
        Fields.add("Name");
        Fields.add("Phone");
        Fields.add("Mobile");
        Fields.add("Email");
        Values = new ArrayList<String>();
        Values.add("");
        Values.add("");
        Values.add("");
        Values.add("");
        Button Get = (Button) findViewById(R.id.UD_Done);
        setupListViewAdapter();
        Get.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ArrayList<String> nValues  = adapter.getList();

                for(int i=0 ; i<4; i++){


                    Log.d("list Item No"+i+"=",nValues.get(i));


                }

            }
        });


    }


    private void setupListViewAdapter() {
        adapter = new EditTextAdaptor(EditTextActivity.this, R.layout.atom_pay_list_item,Fields, Values);
        atomPaysListView = (ListView)findViewById(R.id.list);
        atomPaysListView.setAdapter(adapter);


    }


}

EditTextAdaptor.java

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class EditTextAdaptor extends ArrayAdapter<String> {

    protected static final String LOG_TAG = EditTextAdaptor.class.getSimpleName();

    //private List<AtomPayment> items;
    private int layoutResourceId;
    private Context context;
    private ArrayList<String> Fields;
    private ArrayList<String> Values;
    private TextView TextField;
    private EditText TextValue;
    private List mList;


    public EditTextAdaptor(Context context, int layoutResourceId, ArrayList<String> Fields, ArrayList<String> Values) {
        super(context, layoutResourceId, Fields);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.Fields = Fields;
        this.Values = Values;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        //AtomPaymentHolder holder = null;
        final int pos=position;
        if(convertView != null){
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
//      
        TextField = (TextView)row.findViewById(R.id.TextField);
        TextValue = (EditText)row.findViewById(R.id.TextValue);
        //mList = (List)findViewById(R.id.list);


        TextField.setText(Fields.get(position));
        TextValue.setText(Values.get(position));
        if(position==0){TextValue.requestFocus();}

        TextValue.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //holder.atomPayment.setName(s.toString());
                Values.add(pos,s.toString());

            }

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

            @Override
            public void afterTextChanged(Editable s) { }
        });

        }

    if(convertView == null){
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
    }

        return row;
    }


  public ArrayList<String> getList(){

    return Values;

}




}

activity_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#EAEAEA" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="60dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/title32040"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textcv_UserDetails"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dip"
            android:gravity="center"
            android:text="User Details"
            android:textSize="18dip"
            android:textStyle="bold" />

        <Button
            android:id="@+id/UD_Back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textcv_UserDetails"
            android:layout_alignBottom="@+id/textcv_UserDetails"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="14dp"
            android:background="@drawable/back_1" />

        <Button
            android:id="@+id/UD_Done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textcv_UserDetails"
            android:layout_alignBottom="@+id/textcv_UserDetails"
            android:layout_alignParentRight="true"
            android:layout_marginRight="14dp"
            android:background="@drawable/done" />



    </RelativeLayout>

            <ListView
                android:id="@+id/list"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/list_backround"
                android:layout_marginTop="60dp"
                >

        </ListView>






    </RelativeLayout>

atom_pay_listitem.xnl

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/TextField"
        android:layout_width="0sp"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:background="@drawable/list_backround"
        android:gravity="center_vertical"
        android:hint="@string/EnterPays_atomPay_nameHint"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" >

       </EditText>

    <EditText
        android:id="@+id/TextValue"
        android:layout_width="0sp"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:background="@drawable/list_backround"
        android:gravity="center"
        android:text="@string/EnterPays_TextView_ZeroValue"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" >

       </EditText>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

我认为使用ListView显示许多EditText是一个坏主意,例如:

如果您制作非常长的列表并首先关注EditText,然后开始滚动,View将在其他行项目中重复使用,并且将失去焦点。

如果您只有少数字段,只需将它们添加到任何ViewGroup中的 activity_listview.xml ,您就会遇到更少问题。

PS。您在TextWatcher时添加convertView != null,这样您最终会得到一些字段而不是&#34;观看&#34;。

答案 1 :(得分:0)

请看下面的工作代码:

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.edittexttolist.MainActivity"
tools:ignore="MergeRootFrame" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Change List" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

    public class MainActivity extends ActionBarActivity {

EditText edit1,edit2,edit3;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edit1 = (EditText) findViewById(R.id.editText1);
    edit2 = (EditText) findViewById(R.id.editText2);
    edit3 = (EditText) findViewById(R.id.editText3);

    list = (ListView) findViewById(R.id.listView1);

    //default values for listview
    String[] defaultlist = {"Pune", "Gurgaon", "Bangaluru"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
            android.R.layout.simple_list_item_1, defaultlist);

    // Setting the adapter on mDrawerList
    list.setAdapter(adapter);

    buttonpress();
}

public void buttonpress()
{
    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String newlist[] = new String[3];
            newlist[0] = edit1.getText().toString();
            newlist[1] = edit2.getText().toString();
            newlist[2] = edit3.getText().toString();

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), 
                    android.R.layout.simple_list_item_1, newlist);

            // Setting the adapter on mDrawerList
            list.setAdapter(adapter);
        }
    });
}