如何在ListView中添加具有单独行的新EditText

时间:2017-02-09 11:19:23

标签: android listview android-edittext

我有24个EditTexts,我希望每行都有一个EditText。

注意:此代码工作正常,但我想在一行中添加一个EditText。 我想每行添加一个EditText 意味着我想在24个单独的行中设置24个EditTexts,而不是在一行中设置24个EditText。

这是 lyt_listview_activity.xml

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

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

</LinearLayout>

这是 - lyt_listview_list.xml

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:textColor="@android:color/black"
    android:layout_height="wrap_content"
    android:text="15dp" />

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

    <requestFocus />
</EditText>

</LinearLayout>

这是 - ListviewActivity.java

package com.example.testlistview;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class ListviewActivity extends Activity {

private String[] arrText =
        new String[]{"Text1","Text2","Text3","Text4"
        ,"Text5","Text6","Text7","Text8","Text9","Text10"
        ,"Text11","Text12","Text13","Text14","Text15"
        ,"Text16","Text17","Text18","Text19","Text20"
        ,"Text21","Text22","Text23","Text24"};
private String[] arrTemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     setContentView(R.layout.lyt_listview_activity) ;

    arrTemp = new String[arrText.length];

    MyListAdapter myListAdapter = new  MyListAdapter();
    ListView listView = (ListView)  findViewById(R.id.listViewMain);
    listView.setAdapter(myListAdapter);
}

private class MyListAdapter extends  BaseAdapter{

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(arrText != null &&  arrText.length != 0){
            return arrText.length;    
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arrText[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View  convertView, ViewGroup parent) {

        //ViewHolder holder = null;
        final ViewHolder holder;
        if (convertView == null) {

            holder = new ViewHolder();
            LayoutInflater inflater =  ListviewActivity.this.getLayoutInflater();
            convertView =  inflater.inflate(R.layout.lyt_listview_list, null);
            holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.editText1 = (EditText) convertView.findViewById(R.id.editText1);    

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        holder.ref = position;

        holder.textView1.setText(arrText[position]);
         holder.editText1.setText(arrTemp[position]);
         holder.editText1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                arrTemp[holder.ref] = arg0.toString();
            }
        });

        return convertView;
    }

    private class ViewHolder {
        TextView textView1;
        EditText editText1;
        int ref;
    }
}
}

enter image description here

1 个答案:

答案 0 :(得分:0)

lyt_listview_list.xml中,您只有一个EditText。

那些是你的行,因此,每行只有一个EditText(和一个TextView)

您看到的行数由getCount()确定,并且返回arrText.length或24。

至于“添加另一行”。您无法向数组添加任何内容。您需要在适配器中使用Arraylist。在这种情况下,您可能需要ArrayAdapter而不是BaseAdapter