简体   繁体   English

如何在android中动态添加listview中的项目?

[英]How to add the items in listview Dynamically in android?

I am adding the items in my listview, it's working fine but the listview is not expanding.我正在我的列表视图中添加项目,它工作正常但列表视图没有扩展。

This is my code:这是我的代码:

private ArrayList<NameDetails> nameDetails = null;

inside onCreate() code is里面的onCreate()代码是

    nameDetails = new ArrayList<NameDetails>();
    case R.id.tvAddMore:

                final Dialog dialog = new Dialog(this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.location_name_details_dialog);

                final EditText etName = (EditText) dialog.findViewById(R.id.etName);
                final EditText etPhone = (EditText) dialog.findViewById(R.id.etPhone);
                Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
                Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);


                btnOk.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) 
                    {
                        String name= etName.getText().toString();
                        String phone= etPhone.getText().toString();


                        nameDetails = new ArrayList<NameDetails>();

                        NameDetails nameDetailss = new NameDetails();
                        nameDetailss.setName(name);
                        nameDetailss.setPhone(phone);
                        nameDetails.add(nameDetailss);

                        NameDetailsAdapter nameDetailsAdapter = new NameDetailsAdapter(nameDetails);
                        lvNameDetails.setAdapter(nameDetailsAdapter);
                        nameDetailsAdapter.notifyDataSetChanged();
                        dialog.cancel();

                    }
                });

                btnCancel.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) 
                    {
                        dialog.cancel();

                    }
                });
                dialog.show();
                break;









    public class NameDetailsAdapter extends BaseAdapter
    {

        ArrayList<NameDetails> nameDetails;
        String Name,Phone;
        int count  = 0;
        Context ctx;
        Activity act;
        public NameDetailsAdapter(ArrayList<NameDetails> nameDetails)
        {
             this.nameDetails = nameDetails;

        }



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

        @Override
        public Object getItem(int position) {
            return position;
        }

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

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

             LayoutInflater vi = (LayoutInflater)MyApp.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView = vi.inflate(R.layout.name_details, null);

            TextView tvName     =    (TextView)convertView.findViewById(R.id.tvName);
            TextView tvPhone    =    (TextView)convertView.findViewById(R.id.tvPhone);
            TextView tvNameOne  =    (TextView)convertView.findViewById(R.id.tvNameOne);
            TextView tvPhoneOne =    (TextView)convertView.findViewById(R.id.tvPhoneOne);


            NameDetails name = nameDetails.get(position);
            tvName.setText(name.getName());
            tvPhone.setText(name.getPhone());

            tvNameOne.setTypeface(AppConstants.BOLD);
            tvPhoneOne.setTypeface(AppConstants.BOLD);



            return convertView;
        }

    }

NameDetails.xml名称详细信息.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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin15"
        android:layout_marginRight="@dimen/margin15"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tvNameOne"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:text="@string/name"
                android:textSize="@dimen/text_size14" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:text=":" />

            <TextView
                android:id="@+id/tvName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin10"
                android:layout_weight="0.65"
                android:background="@drawable/text_field"
                android:gravity="center_vertical"
                android:inputType="textPersonName"
                android:maxEms="40"
                android:paddingLeft="@dimen/padding5"
                android:paddingRight="@dimen/padding5"
                android:singleLine="true"
                android:textColor="@android:color/black"
                android:textSize="@dimen/text_size13" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tvPhoneOne"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:text="@string/phone_no"
                android:textSize="@dimen/text_size14" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:text=":" />

            <TextView
                android:id="@+id/tvPhone"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin10"
                android:layout_weight="0.65"
                android:background="@drawable/text_field"
                android:gravity="center_vertical"
                android:inputType="phone"
                android:maxEms="40"
                 android:textColor="@android:color/black"
                android:paddingLeft="@dimen/padding5"
                android:paddingRight="@dimen/padding5"
                android:singleLine="true"
                android:textSize="@dimen/text_size13" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

ListView in android Does not increases dynamically. android中的ListView不会动态增加。 Its dimesion will be same as you have provided in your layout file(But you can scroll it).它的尺寸将与您在布局文件中提供的相同(但您可以滚动它)。 If you want to dynamically increase your ListView make it the child of scrollView and make sure the scrollView shoud go upto bottom of screen, and the hieght of listview should match parent.如果你想动态增加你的 ListView 让它成为 scrollView 的子级,并确保 scrollView 应该上升到屏幕底部,并且 listview 的高度应该与父级匹配。

Note- This is not the standard practice to do注意 - 这不是标准做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM