Android ListView未显示所有项目

时间:2014-07-06 13:59:34

标签: android listview

我的自定义ListView没有显示所有列表项。检查我的活动和xml,请告诉我我的问题是什么。我有30项。但Listview只显示25项。

以下是我的customlistview活动:

        public class DrawableYerliListAdapter extends BaseAdapter {
        private List<Integer> list;
        private final String[] stationName;

        public DrawableYerliListAdapter(Context context) {
            list = new ArrayList<Integer>();
            stationName = context.getResources().getStringArray(R.array.yerli_tv);
        }

        @Override
        public int getCount() {
            if (list == null)
                return 0;
            return list.size();
        }

        @Override
        public Object getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                convertView = inflater.inflate(R.layout.yerlikanal_list_item,
                        parent, false);
            }

            Integer data = list.get(position);

            TextView nameView = (TextView) convertView.findViewById(R.id.title);
            nameView.setText(stationName[position]);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
            imageView.setImageResource(data);
            return convertView;
        }

        public int getImageResource(int position) {
            return list.get(position);
        }

        public String getStationName(int position) {
            return stationName[position];
        }

        public void setData(List<Integer> data) {
            list = data;
        }

这是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

    <!--
    The frame layout is here since we will be showing either
    the empty view or the list view.
    -->

    <FrameLayout
        android:id="@+id/list_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <!--
             Here is the list. Since we are using a ListActivity, we
             have to call it "@android:id/list" so ListActivity will
             find it
        -->

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"
            android:visibility="visible" />

        <!-- Here is the view to show if the list is emtpy -->

        <TextView
            android:id="@+id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="No items."
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:visibility="gone" />
    </FrameLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ProgressBar
            android:id="@+id/load"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="gone" />
    </RelativeLayout>
    <!--
    <include layout="@layout/play_bar"/>-->
</RelativeLayout>

我的适配器使用类:

package com.canli.tvyayinlari;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

public class YerliList extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fm = getSupportFragmentManager();

        // Create the list fragment and add it as our sole content.
        if (fm.findFragmentById(android.R.id.content) == null) {
            AppListFragment list = new AppListFragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        }
    }

    /**
     * A custom Loader that loads all of the Station
     */
    public static class StationListLoader extends
            AsyncTaskLoader<List<Integer>> {
        private List<Integer> stationsImage;
        Context context;

        public StationListLoader(Context context) {
            super(context);
            this.context = context;
        }

        /**
         * This will load all the stations image Integer
         */
        @Override
        public List<Integer> loadInBackground() {
            List<Integer> stationImage = new ArrayList<Integer>();
            Resources rs = context.getResources();
            for (int i = 9; i <= rs.getStringArray(R.array.yerli_tv).length; i++) {
                int resID = rs.getIdentifier("yerli_" + i, "drawable",
                        context.getPackageName());
                if (resID <= 0)
                    resID = rs.getIdentifier("station_default", "drawable",
                            context.getPackageName());
                stationImage.add(resID);
            }

            return stationImage;
        }

        @Override
        public void deliverResult(List<Integer> images) {
            if (isReset()) {
            }
            stationsImage = images;
            if (isStarted()) {
                super.deliverResult(images);
            }
        }

        @Override
        protected void onStartLoading() {
            if (stationsImage != null) {
                deliverResult(stationsImage);
            }
            forceLoad(); // *********** important
        }

        /**
         * Handles a request to stop the Loader.
         */
        @Override
        protected void onStopLoading() {
            cancelLoad();
        }

        /**
         * Handles a request to cancel a load.
         */
        @Override
        public void onCanceled(List<Integer> apps) {
            super.onCanceled(apps);
        }

        /**
         * Handles a request to completely reset the Loader.
         */
        @Override
        protected void onReset() {
            super.onReset();
            onStopLoading();
        }

        /**
         * Helper function to take care of releasing resources associated with
         * an actively loaded data set.
         */
        protected void onReleaseResources(List<Integer> apps) {
        }
    }

    public static class AppListFragment extends SherlockFragment implements
            LoaderManager.LoaderCallbacks<List<Integer>>, OnItemClickListener {
        // This is the Adapter being used to display the list's data.
        private DrawableYerliListAdapter mAdapter;
        private ListView listView;
        private ProgressBar progressBar;
        private TextView textView;
        private FrameLayout layout;

        // private PlayBar playBar;

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            getActivity().setTitle(getResources().getString(R.string.app_name));
            setHasOptionsMenu(true);
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        }

        @Override
        public Loader<List<Integer>> onCreateLoader(int id, Bundle args) {
            return new StationListLoader(getActivity());
        }

        @Override
        public void onLoadFinished(Loader<List<Integer>> loader,
                List<Integer> data) {
            progressBar.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            if (data.size() == 0) {
                textView.setText("No Stations Found");
                textView.setVisibility(View.VISIBLE);
            }
            mAdapter.setData(data);
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onLoaderReset(Loader<List<Integer>> loader) {
            mAdapter.setData(null);
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            try {
                View v = inflater.inflate(R.layout.yerlikanal_list, container,
                        false);
                listView = (ListView) v.findViewById(R.id.list);
                progressBar = (ProgressBar) v.findViewById(R.id.load);
                textView = (TextView) v.findViewById(R.id.empty);
                layout = (FrameLayout) v.findViewById(R.id.list_show);

                mAdapter = new DrawableYerliListAdapter(getActivity());
                listView.setAdapter(mAdapter);
                listView.setOnItemClickListener(this);
                progressBar.setVisibility(View.VISIBLE);
                layout.setVisibility(View.GONE);
                getLoaderManager().initLoader(0, null, this);
                return v;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long l) {
            Intent intent = new Intent(getActivity(), TVDetayYerli.class);
            intent.putExtra("station_id", position);
            intent.putExtra("station_image",
                    mAdapter.getImageResource(position));
            intent.putExtra("station_name", mAdapter.getStationName(position));
            startActivity(intent);
        }

    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

假设您在从适配器类创建对象后调用setData(),请不要在构造函数中包含它。如果只有25个项目,你应该看看你传递给setData()函数的参数。