简体   繁体   中英

List View doesn't appear

I try to show a Listview to show my data

This is main_fragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_main, container, false);
    final List<import_fragment.Contact> Contacts = new ArrayList<import_fragment.Contact>();
    ListView contactListView;

    final EditText nametxt, emailTxt, phoneTxt, addressTxt;
    nametxt = (EditText) view.findViewById(R.id.txtName);
    emailTxt = (EditText) view.findViewById(R.id.txtEmail);
    phoneTxt = (EditText) view.findViewById(R.id.txtPhone);
    addressTxt = (EditText) view.findViewById(R.id.txtAddress);
    contactListView = (ListView) view.findViewById(R.id.listView);
    dbHandler = new DatabaseHandler(getActivity().getApplicationContext());

    final Button addBtn = (Button) view.findViewById(R.id.btnadd);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Uri imageUri = Uri.parse("android.resource://org.intracode.contactmanager/drawable/no_user_logo.png");
            import_fragment.Contact contact = new import_fragment.Contact(dbHandler.getContactsCount(), String.valueOf(nametxt.getText()), String.valueOf(phoneTxt.getText()), String.valueOf(emailTxt.getText()), String.valueOf(addressTxt.getText()), imageUri);
            if (!contactExists(contact)) {
                dbHandler.createContact(contact);
                Contacts.add(contact);
                if (contactAdapter != null) contactAdapter.notifyDataSetChanged();
                Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " has been added to your Contacts!", Toast.LENGTH_SHORT).show();
            return;
            }
            Toast.makeText(getActivity().getApplicationContext(), String.valueOf(nametxt.getText()) + " already exists. Please use a different name.", Toast.LENGTH_SHORT).show();
        }
    });

    final Button addContact = (Button) view.findViewById(R.id.btnadd);

    nametxt.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) {

            addContact.setEnabled(!nametxt.getText().toString().trim().equals(""));


        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    return view;

}
ArrayAdapter<import_fragment.Contact> contactAdapter;
ListView contactListView;
DatabaseHandler dbHandler;
int longClickedItemIndex;
public void onActivityResult(int reqCode, int resCode, Intent data) {
    Uri imageUri = Uri.parse("android.resource://org.intracode.contactmanager/drawable/no_user_logo.png");
    ImageView contactImageImgView;
    contactImageImgView = (ImageView) getActivity().findViewById(R.id.ivContactImage);

    if (resCode == Activity.RESULT_OK) {
        if (reqCode == 1) {
            imageUri = data.getData();
            contactImageImgView.setImageURI(data.getData());
        }
    }
}

public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, view, menuInfo);

    {
        ImageView contactImageImgView;

        contactImageImgView = (ImageView) view.findViewById(R.id.ivContactImage);

        contactImageImgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
            }

        });


        if (dbHandler.getContactsCount() != 0)
            Contacts.addAll(dbHandler.getAllContacts());

        populateList();
    }

    menu.setHeaderIcon(R.drawable.pencil_icon);
    menu.setHeaderTitle("Contact Options");
    menu.add(Menu.NONE, EDIT, menu.NONE, "Edit Contact");
    menu.add(Menu.NONE, DELETE, menu.NONE, "Delete Contact");
}
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case EDIT:
            // TODO: Implement editing a contact
            break;
        case DELETE:
            dbHandler.deleteContact(Contacts.get(longClickedItemIndex));
            Contacts.remove(longClickedItemIndex);
            contactAdapter.notifyDataSetChanged();
            break;
    }

    return super.onContextItemSelected(item);
}


private boolean contactExists(import_fragment.Contact contact) {

    String name = contact.getName();
    int contactCount = Contacts.size();

    for (int i = 0; i < contactCount; i++) {
        if (name.compareToIgnoreCase(Contacts.get(i).getName()) == 0)
            return true;
    }
    return false;
}

private void populateList() {
    contactAdapter = new ContactListAdapter(getActivity());
    contactListView.setAdapter(contactAdapter);
}

final List<import_fragment.Contact> Contacts = new ArrayList<import_fragment.Contact>();

private class ContactListAdapter extends ArrayAdapter<import_fragment.Contact> {
    public ContactListAdapter(Context cntx) {
        super (cntx, R.layout.fragment_import, Contacts);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null)
            view = getActivity().getLayoutInflater().inflate(R.layout.fragment_import, parent, false);

        import_fragment.Contact currentContact = Contacts.get(position);

        TextView name = (TextView) view.findViewById(R.id.contactName);
        name.setText(currentContact.getName());
        TextView phone = (TextView) view.findViewById(R.id.phoneNumber);
        phone.setText(currentContact.getPhone());
        TextView email = (TextView) view.findViewById(R.id.emailAddress);
        email.setText(currentContact.getEmail());
        TextView address = (TextView) view.findViewById(R.id.cAddress);
        address.setText(currentContact.getAddress());


        return view;
    }
}

This is import_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:id="@+id/ivContactImage" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="91dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Contact Name"
        android:id="@+id/contactName"
        android:layout_gravity="left|center_vertical"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Phone"
        android:id="@+id/phoneNumber"
        android:layout_gravity="left|center_vertical"
        android:layout_marginTop="5dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email"
        android:id="@+id/emailAddress"
        android:layout_gravity="left|center_vertical"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Address"
        android:id="@+id/cAddress"
        android:layout_gravity="left|center_vertical"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="My Contacts"
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"/>

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listView"
        android:layout_gravity="center"/>
</LinearLayout>

My listview in import_fragment.xml , when i open import_fragment.xml in app its appear only Contact Name and Phone and Email and Address which is added in import_fragment.xml and not the new contact information which in database added by user.

Change both of your LinearLayout's height to

android:layout_height="fill_parent"

Your listView is actually "there", it's just that the container (layout) of the listView isn't big enough to fit it.

EDIT:

You are defining two different list of Contacts there! Both activity and adapter class are referring to two different Contacts. Remove the one inside the onCreateView function.

final List<import_fragment.Contact> Contacts = new ArrayList<import_fragment.Contact>();

You have to setAdapter in onCreateView . Currently you are populating the list in onCreateContextMenu . Moreover, after setting the adapter, you have to call notifyDataSetChanged() as well

Try this way, i have tried this in my machine and it show me listview properly , i have done with weightSum so it will be applicable for all devices , just change your xml file with below one

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="4">

    <ImageView
        android:id="@+id/ivContactImage"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/contactName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:text="Contact Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/phoneNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_marginTop="5dp"
            android:text="Phone" />

        <TextView
            android:id="@+id/emailAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:text="Email" />

        <TextView
            android:id="@+id/cAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:text="Address" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:text="My Contacts"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>
</LinearLayout>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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