无法通过arrayAdapter填充listView

时间:2016-01-30 18:38:09

标签: android listview android-arrayadapter

我是android和java的新手,并且在大学作业之外的一些教程之后。目前,我正在关注电话簿教程,但无法让联系人填充listView。

非常感谢任何帮助 - 无论是这个问题还是学习Android开发人员。

谢谢!

 public class Creator extends AppCompatActivity{

       protected EditText _name;
       protected EditText _phoneNumber;
       protected EditText _email;
       protected EditText _address;

    List<Contact> Contacts = new ArrayList<Contact>();
    ListView contactListView;





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

        _name = (EditText) findViewById(R.id.inputName);
        _phoneNumber = (EditText) findViewById(R.id.inputPhone);
        _email = (EditText) findViewById(R.id.inputEmail);
        _address = (EditText) findViewById(R.id.inputAddress);

        TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
        contactListView = (ListView) findViewById(R.id.listView);

        tabHost.setup();

        TabHost.TabSpec tabSpec = tabHost.newTabSpec("creator");
        tabSpec.setContent(R.id.tabCreator);
        tabSpec.setIndicator("Creator");
        tabHost.addTab(tabSpec);

        tabSpec = tabHost.newTabSpec("contacts");
        tabSpec.setContent(R.id.tabContacts);
        tabSpec.setIndicator("Contacts");
        tabHost.addTab(tabSpec);


        final Button btnSave = (Button)findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addContact(_name.getText().toString(),        _phoneNumber.getText().toString(), _email.getText().toString(),    _address.getText().toString());
                populateList();
                Toast.makeText(getApplicationContext(), _name.getText().toString() +" has been added to your contacts",Toast.LENGTH_SHORT).show();
            }
        });

        _name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
           public void onTextChanged(CharSequence s, int start, int before, int    count) {
               btnSave.setEnabled(!_name.getText().toString().trim().isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void populateList(){
        ArrayAdapter<Contact> adapter = new ContactListAdapter();
        contactListView.setAdapter(adapter);
    }

    private void addContact(String name, String number, String email, String address){
        Contacts.add(new Contact(name, number, email, address));
    }



    private class ContactListAdapter extends ArrayAdapter<Contact>{
        public ContactListAdapter(){
            super(Creator.this, R.layout.contacts, Contacts);
        }
        @Override
        public View getView(int position, View view, ViewGroup parent){
            if (view == null)
            view = getLayoutInflater().inflate(R.layout.contacts, parent, false);

            Contact currentContact = Contacts.get(position);

            TextView name = (TextView) view.findViewById(R.id.textName);
            name.setText(currentContact.getName());
            TextView number = (TextView) view.findViewById(R.id.textNumber);
            number.setText(currentContact.getNumber());
            TextView email = (TextView) view.findViewById(R.id.textEmail);
            email.setText(currentContact.getEmail());
            TextView address = (TextView) view.findViewById(R.id.textAddress);
            address.setText(currentContact.getAddress());

            return view;
        }
    }



}



<LinearLayout 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="com.example.davel.phonebook.Creator"
    android:orientation="vertical">

    <TabHost
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabHost">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="match_parent">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="ContactList"
                        android:id="@+id/textContactsHead"
                        android:layout_gravity="center_horizontal" />

                    <ListView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/listView"
                        android:layout_gravity="center_horizontal" />

                </LinearLayout>

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="Create Contact"
                        android:id="@+id/textView2"
                        android:layout_below="@+id/inputPhone"
                        android:layout_centerHorizontal="true" />

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/inputName"
                        android:width="300dp"
                        android:hint="Name"
                        android:layout_below="@+id/btnSave"
                        android:layout_centerHorizontal="true"
                        android:layout_gravity="center_vertical" />

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/inputPhone"
                        android:width="300dp"
                        android:hint="Phone number"
                        android:phoneNumber="true"
                        android:inputType="phone"
                         />

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/inputEmail"
                        android:width="300dp"
                        android:inputType="textEmailAddress"

                        android:hint="Email" />

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/inputAddress"
                        android:width="300dp"
                        android:inputType="textMultiLine"
                        android:hint="Address"
                         />

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Save"
                        android:id="@+id/btnSave"
                        android:width="150dp"
                        android:layout_gravity="center_horizontal"
                        android:clickable="false"
                        android:contextClickable="false"
                        android:enabled="false"
                        android:onClick="btnClick"
                         />
                </LinearLayout>

            </FrameLayout>

        </LinearLayout>
    </TabHost>

</LinearLayout>

0 个答案:

没有答案