android - Fragment 中的自定义 ListView 不起作用

标签 android listview fragment

我是 android 的新手,我试图在 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="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>

最佳答案

@Override
public int getCount() {

     return data.size();
}

@Override
public Custom getItem(int position) {
     return data.get(position);
}

关于android - Fragment 中的自定义 ListView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18200504/

相关文章:

android - FragmentActivity 操作栏选项菜单

Android 仪器测试 - "Unable to find instrumentation info for: ComponentInfo"

Android clear listview链接到SQLite

适用于平板电脑和手机的 Android Fragment

android - 我们可以在 Android 中将 ListView 添加到 RemoteView 吗?

listview - 我可以使用最初未声明的itemCount定义ListView Builder吗

android - 不使用newInstance/setArguments直接在Activity中创建DialogFragment是否正确?

android - 获取 TapTargetView 的 MenuItem View 引用

android - 在android中发现可用的蓝牙设备

java - 用于 Java、Cpp 和 C 的 cscope