在listview项目上动态添加edittext

时间:2016-02-04 11:29:19

标签: android listview dynamic android-edittext

我想在按钮点击该特定列表项时在列表项上添加edittext。我需要在edittexts中获取文本。我已经发布了我设法提出的代码。但是当我滚动列表视图时,edittexts也会被添加到其他列表项中。请帮忙......

MainActivity

ArrayList<EdittextValues> etValues = new ArrayList<>();

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

    ArrayList<ListItemModel> arrayList = new ArrayList<>();
    arrayList.add(new ListItemModel("Title 1"));
    arrayList.add(new ListItemModel("Title 2"));
    arrayList.add(new ListItemModel("Title 3"));
    arrayList.add(new ListItemModel("Title 4"));
    arrayList.add(new ListItemModel("Title 5"));
    arrayList.add(new ListItemModel("Title 6"));
    arrayList.add(new ListItemModel("Title 7"));
    arrayList.add(new ListItemModel("Title 8"));
    arrayList.add(new ListItemModel("Title 9"));
    arrayList.add(new ListItemModel("Title 10"));

    ListView lv = (ListView)findViewById(R.id.listview);
    lv.setItemsCanFocus(true);
    final CustomListAdapter adapter = new CustomListAdapter(MainActivity.this, arrayList, etValues);
    lv.setAdapter(adapter);

    Button btn = (Button)findViewById(R.id.show);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String all="";
            for (int i=0; i<etValues.size(); i++){
                all += etValues.get(i).getValue()+"\n";
            }
            Toast.makeText(MainActivity.this, all, Toast.LENGTH_LONG).show();
        }
    });

}

CustomListAdapter:

public class CustomListAdapter extends BaseAdapter {

ArrayList<ListItemModel> items;

ViewHolder holder;

Context mContext;

ArrayList<EdittextValues> etValues;

int i = 0;

public CustomListAdapter(Context context, ArrayList<ListItemModel> items, ArrayList<EdittextValues> etValues) {
    this.mContext = context;
    this.items = items;
    this.etValues = etValues;
}

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

@Override
public Object getItem(int i) {
    return items.get(i);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.list_row, null);

        holder.tvTitle = (TextView) convertView.findViewById(R.id.title);
        holder.btnAdd = (Button) convertView.findViewById(R.id.addEdittext);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tvTitle.setText(items.get(position).getTitle());

    final View finalConvertView = convertView;

    holder.btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            etValues.add(new EdittextValues(position, i, ""));

            LinearLayout mLayout = (LinearLayout) finalConvertView.findViewById(R.id.llChild);
            LinearLayout.LayoutParams mparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);

            final EditText myEditText = new EditText(mContext);
            mparams.setMargins(46, 3, 46, 3);
            myEditText.setLayoutParams(mparams);
            mLayout.setOrientation(LinearLayout.VERTICAL);
            myEditText.setPadding(10, 10, 10, 10);
            myEditText.setSingleLine(true);
            myEditText.setId(i);
            myEditText.setHeight(72);
            myEditText.setTextSize(15);
            myEditText.setFocusableInTouchMode(true);
            myEditText.setFocusable(true);
            myEditText.setHint("Type");
            mLayout.addView(myEditText);

            myEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                    etValues.get(myEditText.getId()).setValue(charSequence+"");
                    Toast.makeText(mContext, etValues.get(myEditText.getId()).getValue(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            i++;

        }
    });

    return convertView;
}

private class ViewHolder {
    public TextView tvTitle;
    public Button btnAdd;
}

}

ListItemModel:

public class ListItemModel {
String title;

public ListItemModel(String title) {
    this.title = title;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

EdittextValues:

public class EdittextValues {
int ids, etIds;
String value;

public EdittextValues(int ids, int etIds, String value) {
    this.ids = ids;
    this.etIds = etIds;
    this.value = value;
}

public int getIds() {
    return ids;
}

public void setIds(int ids) {
    this.ids = ids;
}

public int getEtIds() {
    return etIds;
}

public void setEtIds(int etIds) {
    this.etIds = etIds;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}
}

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/show"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show Values"/>

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/show"
    android:descendantFocusability="beforeDescendants"/>

</RelativeLayout>

list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title"
    android:textSize="20dp" />

<Button
    android:id="@+id/addEdittext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:layout_alignTop="@+id/title"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<LinearLayout
    android:id="@+id/llChild"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_below="@+id/addEdittext">

</LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以使用ExpandableListView解决此问题。您可以通过引用this link来解决此问题。