在android的listview末尾动态添加Textview

时间:2014-09-20 10:33:14

标签: android android-listview

我正在做一个Android应用程序,我需要在一个列表视图中组合两个视图。(即)。我有一个自定义视图的列表视图&一个名为" ADD"的textview。如果我单击该文本视图,则应打开一个对话框,该对话框将从用户获得一些输入。那些应该填写在自定义列表视图中,如

item1 - 包含3个值的自定义视图 ADD

如果再次单击添加文本视图,它应该再次从用户&获取值。应该填充为 ITEM1 ITEM2 ADD

它只是意味着,应该在android中的每个更新列表视图的末尾添加一个textview。我可以这样做吗?先谢谢。

1 个答案:

答案 0 :(得分:0)

        //AddItemAdapter

        class AddItemAdapter extends BaseAdapter {

                ArrayList<String> listItem = new ArrayList<String>();
                LayoutInflater inflater;

                public AddItemAdapter() {
                    inflater = LayoutInflater.from(MainActivity.this);
                    listItem.clear();

                    listItem.add("blank");
                }

                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return listItem.size();
                }

                @Override
                public Object getItem(int position) {
                    // TODO Auto-generated method stub
                    return listItem.get(position);
                }

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

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

                    View listItemView = inflater.inflate(R.layout.list_item, null);
                    View listItemAdd = inflater.inflate(R.layout.list_item_add, null);

                    TextView txtItem = (TextView) listItemView.findViewById(R.id.txt1);
                    final EditText edtAdd = (EditText) listItemAdd.findViewById(R.id.edtText);
                    Button btnAdd = (Button) listItemAdd.findViewById(R.id.btnAdd);
                    edtAdd.requestLayout();
                    btnAdd.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if(!edtAdd.getText().toString().trim().equals(""))
                            {       
                                if(position==0)
                                {
                                    listItem.add(position, edtAdd.getText().toString());
                                }
                                else
                                {
                                listItem.add(listItem.size()-1, edtAdd.getText().toString());
                                }
                                notifyDataSetChanged();
                            }
                            else
                            {
                                Toast.makeText(MainActivity.this, "Blank Text Not Add", 300).show();
                            }


                        }
                    });

                    if (listItem.get(position).equals("blank")) {

                        return listItemAdd;
                    } else {
                        txtItem.setText(listItem.get(position));
                        return listItemView;
                    }

                }

            }

            //list_item.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" >

                <TextView 
                android:id="@+id/txt1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>


        //list_item_add.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" >

            <EditText 
                android:id="@+id/edtText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/btnAdd"
                />
            <Button 
                android:id="@+id/btnAdd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="ADD"/>
            </RelativeLayout>


            and another you have to set you listview property for edittext Focus

            android:descendantFocusability="beforeDescendants"

            and also add property to your activity in manifast.xml

                 <activity
            android:screenOrientation="sensorPortrait"
            android:windowSoftInputMode="adjustPan" >