嵌套片段中的ListView显示空列表项,(仅文本视图不是图像)

时间:2014-06-09 11:50:48

标签: android android-fragments android-listview

谷歌搜索后我从来没有找到任何有用的答案,所以我在这里发布我的问题。我的问题如下: 我有一个嵌套片段(MainActivity调用SettingsFragment,该片段调用另一个名为ContactFragment的片段,其中使用了listview。我在适配器中设置数据但是调用adapter.notifyDataSetChanged();它会在ListView中添加一个项目,但只显示不是TextViews的图片。我逐行调试了我的代码,每个变量都有它的值,但是列表项没有显示它。以下是我的代码。

ContactsFragment extends Fragment {

    public static List<ContactItem> contactList;
    private ContactsItemAdapter adapter;
    private ListView lvContacts;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

       View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
       return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        lvContacts = (ListView) getView().findViewById(R.id.lvContacts);

        contactList = new ArrayList<ContactItem>();
        contactList.add(new ContactItem(R.drawable.ic_action_overflow, "OverFlow", "Testing", SOS.CONTACT_TYPE_SMS));

        adapter = new ContactsItemAdapter(getActivity(), R.layout.contact_item, contactList);
        lvContacts.setAdapter(adapter); 
    }

以下是点击事件,用于将项目添加到列表

    private void btnAddOnClick(int type){
        contactList.add(new ContactItem(R.drawable.ic_action_call, "ABC", "123", "SMS"));
        adapter.notifyDataSetChanged();     
    }

}

以下是 fragment_contacts.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:gravity="center_horizontal"
    android:orientation="vertical" >



    <ListView
        android:id="@+id/lvContacts"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >

    </ListView>


</LinearLayout>

通过调用btnAddOnClick在listview中添加项目但是像这样空白

enter image description here

在这里你可以看到它显示了呼叫图标但没有显示文本视图值。请告诉我我做错了什么。

这是适配器

public class ContactsItemAdapter extends ArrayAdapter<ContactItem> {

    private LayoutInflater mInflater;

    public ContactsItemAdapter(Context context, int resource,
            List<ContactItem> contactList) {
        super(context, resource, contactList);
        mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    }

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

        ViewHolder holder = null;

        TextView tvName;
        TextView tvValue;
        ImageView ivIcon;

        ContactItem rowData = ContactsFragment.contactList.get(position);

        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.contact_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();

        tvName = holder.getName();
        tvName.setText(rowData.getName());

        tvValue = holder.getValue();
        tvValue.setText(rowData.getValue());

        ivIcon = holder.getIcon();
        ivIcon.setImageResource(rowData.getIcon());

        return convertView;
    }



    public class ViewHolder {
        private View mRow;

        private TextView tvName = null;
        private TextView tvValue = null;
        private ImageView ivIcon = null;

        public ViewHolder(View row) {
            mRow = row;
        }

        public TextView getName() {
            if (null == tvName) {
                tvName = (TextView) mRow.findViewById(R.id.tvName);
            }
            return tvName;
        }

        public TextView getValue() {
            if (null == tvValue) {
                tvValue = (TextView) mRow.findViewById(R.id.tvValue);
            }
            return tvValue;
        }

        public ImageView getIcon() {
            if (null == ivIcon) {
                ivIcon = (ImageView) mRow.findViewById(R.id.icon);
            }
            return ivIcon;
        }
    }
}

ContactItem类

public class ContactItem {

    private int icon;
    private String name;
    private String value;

    private String contactType;

    public ContactItem() {
        icon = R.drawable.ic_action_call;
        name = "";
        value = "";
        contactType = "";
    }

    public ContactItem(int icon, String nameString, String valueString,
            String type) {
        this.icon = icon;
        this.name = nameString;
        this.value = valueString;
        this.contactType = type;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

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

    public String getContactType() {
        return contactType;
    }

    public void setContactType(String contactType) {
        this.contactType = contactType;
    }

}

contact_item.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="55dp"
    android:background="@color/holo_gray_light"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
         />

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/darker_gray" />

    <ImageView
        android:id="@+id/ivCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_action_cancel" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/divider"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/ivCancel"
        android:layout_toRightOf="@+id/icon"
        android:orientation="vertical" 
        android:weightSum="1">

        <TextView
            android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.49"
            android:gravity="center_vertical"
            android:paddingRight="40dp"
            android:ellipsize="end" 
            android:text="Nauman Zubair"/>

        <TextView
            android:id="@+id/tvValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.49"
            android:paddingRight="40dp" 
            android:ellipsize="end"
            android:gravity="center_vertical"
            android:text="+92 123 1234567"/>

    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您的代码应该有效,我认为问题是您的contact_item.xml布局。不知何故,你的设计隐藏了文字。使用更简单的布局设计来查看它是否显示文本,尝试删除LinearLayout:

<?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="55dp"
    android:background="@color/holo_gray_light"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon" />

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/darker_gray" />

    <ImageView
        android:id="@+id/ivCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_action_cancel" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/ivCancel"
        android:layout_toRightOf="@+id/icon"
        android:gravity="center_vertical"
        android:paddingRight="40dp"
        android:ellipsize="end" 
        android:text="Nauman Zubair"/>

    <TextView
        android:id="@+id/tvValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/ivCancel"
        android:layout_toRightOf="@+id/icon"
        android:paddingRight="40dp" 
        android:ellipsize="end"
        android:gravity="center_vertical"
        android:text="+92 123 1234567"/>

</RelativeLayout>