3

我是 android 的新手,我试图在 Fragment 中显示自定义列表视图,但是当我运行它时什么都不显示。有人可以帮我解决这个问题。这是我的代码:

    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="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:id="@+id/tab_layout"
>

    <ListView
        android:id="@+id/list"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:dividerHeight="1dp"
        android:divider="@color/white"
        />
</LinearLayout>

这是我的列表项 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

1 回答 1

0
@Override
public int getCount() {

     return data.size();
}

@Override
public Custom getItem(int position) {
     return data.get(position);
}
于 2013-08-13T04:14:44.867 回答