2 momo1199 Momo1199 于 2013.08.13 17:22 提问

在fragment中的listView问题

在fragment中显示一个自定义listView,但是运行后什么也没显示。

    public class Tab1 extends Fragment
        implements ListView.OnItemClickListener{

    private ArrayList<Custom> fetch = new ArrayList<Custom>();
    private ContactsAdapter adapter;
    private ListView lv;

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

        View v = inflater.inflate(R.layout.tab_layout, container, false);
        Custom one = new Custom("Hoang Ha", "01672286349");
        Custom two = new Custom("Ha Link", "03203590176");

        fetch.add(one);
        fetch.add(two);
        lv =(ListView)v.findViewById(R.id.list);
        adapter = new ContactsAdapter(getActivity(), fetch);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        return v;
    }

public class Custom{ 
    private String contactName;
    private String contactPhone;

    public Custom(String st1, String st2){
        contactName = st1;
        contactPhone = st2;
    }

    public String getName(){
        return contactName;
    }

    public String getPhone(){
        return contactPhone;
    }

    public void setName(String st1){
        contactName = st1;
    }
    public void setPhone(String st2){
        contactPhone = st2;
    }
}
    @Override
    public void onItemClick(AdapterView<?> ad, View v, int position,
                            long id) {
        // TODO Auto-generated method stub
    }
    private class ContactsAdapter extends BaseAdapter{
        private FragmentActivity activity;
        private LayoutInflater inflater;
        private ArrayList<Custom> data;

        public ContactsAdapter (FragmentActivity a, ArrayList<Custom> d){
            activity = a;
            data = d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            data.size();
            return 0;
        }
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View v = convertView;
                     v = inflater.inflate(R.layout.list_row, null);
                TextView contact_name = (TextView)v.findViewById(R.id.contact_name);
                TextView phone_number = (TextView)v.findViewById(R.id.phone_number);    
                //final Custom custom = entries.get(position);
                final Custom custom = data.get(position);
                contact_name.setText(custom.getName());
                phone_number.setText(custom.getPhone());
                return v;
        }
    }
}

这是列表xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/light_dark"
    android:padding="5dip" >

    <TextView
        android:id="@+id/contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textColor="@color/white"
        android:typeface="sans"
        android:textSize="@dimen/font_medium"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textColor="@color/white"
        android:typeface="sans"
        android:textSize="@dimen/font_small"
        />

</LinearLayout>

4个回答

y78431967
y78431967   2015.09.24 15:14
已采纳
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        data.size();
        return 0;
    }


            适配器 return 0 能有东西才怪。。。
ysj40540
ysj40540   2013.10.18 15:00

我也是!!!!求方法 。 我的这个fragment里上面是其他view(button等等 ),下面是listview,也是个Listview.setadapter(自定义的adapter)没数据!!!

marzul
marzul   2015.02.03 19:14

给Listview绑定监听时间按了么?

u014132820
u014132820   2016.05.06 23:18

怎么解决啊,我也碰到这个问题了,求解答啊

Csdn user default icon
上传中...
上传图片
插入图片