如何使用自定义ArrayAdapter在MainActivity选项卡中显示自定义列表片段

时间:2014-06-22 01:43:16

标签: android android-listview android-arrayadapter android-listfragment

好的,我有一个主要活动,标准Android模板制作了3个标签(带ViewPager的ActionBar标签)。我有一个List<AppInfo>,其中有一些数据要显示在ListView的第二个选项卡上,每行有2个文本字段。

以下代码的组织如下:

我的ArrayAdapterList<AppInfo>。它还使用app_info_row_layout.xml

我的ListFragment将其listadapter设置为我的ArrayAdapter。它还使用list_fragment.xml

我的FragmentPagerAdapter然后在其ListFragment方法

中创建我的getItem()

这是我的ArrayAdapter类

public class AppInfoList extends ArrayAdapter<AppInfo>{
    private List<AppInfo> list;
    private final Context context;

    public AppInfoList(Context context, int resource) {
        super(context, resource);
        this.context = context;
        list = // a list with objects is generated
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        AppInfo appInfo = list.get(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(convertView == null){
            convertView = (View) inflater.inflate(R.layout.app_info_row_layout, null, false);
        }

        // text field 1
        TextView appName = (TextView) convertView.findViewById(R.id.appName);
        appName.setText(appInfo.packageName);

        // text field 2
        TextView totalTime = (TextView) convertView.findViewById(R.id.totalTime);
        totalTime.setText( " minutes" ); // took out logic to make code shorter

        return convertView;
        }
}

以上使用了一个简单的app_info_row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
       android:id="@+id/appName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/appName"
        android:textSize="30sp" >
    </TextView>

    <TextView
        android:id="@+id/totalTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/totalTime"
        android:textSize="20sp" >
    </TextView>

</LinearLayout> 

这里是自定义ListFragment,它应该使用上面的ArrayAdapter并在ListView中显示它的数据

public class AppInfoListFragment extends ListFragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.list_fragment, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        AppInfoList appInfoAdapter = new AppInfoList(getActivity(), 0);
        setListAdapter(appInfoAdapter);
    }


}

使用以下list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </TextView>

</LinearLayout>

我扩展了FragmentPagerAdapter并创建了以下

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment for 1st and 3rd tab

            // return different views/fragments here
            if(position == 1){
                if(appInfoListFragment == null)
                    appInfoListFragment = new AppInfoListFragment();
                return appInfoListFragment;
            }
            else
                return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
}

当我运行应用程序时,第二个选项卡只是空白而不是显示列表。我究竟做错了什么?

1 个答案:

答案 0 :(得分:0)

我会使用以下设置:

Array Adapter类:

public class AppInfoList extends ArrayAdapter<AppInfo>{
    private List<AppInfo> list;
    private final Context context;

    public AppInfoList(Context context, list<Appinfo>) {
        super(context, R.layout.rowLayout, list);
        this.context = context;
        list = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        AppInfo appInfo = list.get(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(convertView == null){
            convertView = (View) inflater.inflate(R.layout.app_info_row_layout, null, false);
        }

        // text field 1
        TextView appName = (TextView) convertView.findViewById(R.id.appName);
        appName.setText(appInfo.packageName);

        // text field 2
        TextView totalTime = (TextView) convertView.findViewById(R.id.totalTime);
        totalTime.setText( " minutes" ); // took out logic to make code shorter

        return convertView;
        }
}

所有其他课程似乎没问题。供您参考,我使用以下教程help me