Android:片段中的适配器不起作用

时间:2016-12-23 06:32:57

标签: android

我目前正在编写一个包含Fragment的代码,其中包含一个扩展BaseAdapter的适配器。我正在尝试向适配器添加一些项目并使它们出现在屏幕上。这是代码。

public class SettingsFragment extends Fragment {

    private ListView lvSettings;
    private SettingsAdapter settingsAdapter = new SettingsAdapter();

    private int[] icons = {
            R.drawable.ic_developer
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, null);
        lvSettings = (ListView) view.findViewById(R.id.lvSettings);
        lvSettings.setAdapter(settingsAdapter);
        initializeAdapter();
        return view;
    }

    private void initializeAdapter() {
        int size = icons.length;
        SettingsItem[] settingsItems = new SettingsItem[size];
        for(int i=0; i<size; i++) {
            settingsItems[i] = new SettingsItem(getActivity());
            settingsItems[i].setIcon(icons[i]);
            settingsItems[i].setTitle(getResources().getStringArray(R.array.settings_items)[i]);
            settingsAdapter.addItem(settingsItems[i]);
        }

        settingsAdapter.notifyDataSetChanged();
    }

    private class SettingsAdapter extends BaseAdapter {

        private ArrayList<SettingsItem> itemsList = new ArrayList<>();

        @Override
        public int getCount() {
            return itemsList.size();
        }

        @Override
        public Object getItem(int position) {
            return itemsList.get(position);
        }

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

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {

            return view;
        }

        public void addItem(SettingsItem item) {
            itemsList.add(item);
        }
    }

    private class SettingsItem {
        private ImageView ivIcon;
        private TextView tvTitle;
        private LayoutInflater inflater;

        SettingsItem(Context context) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.listitem_settings, null);
            ivIcon = (ImageView) view.findViewById(R.id.ivSettingsItem);
            tvTitle = (TextView) view.findViewById(R.id.tvSettingsItem);
        }

        public void setIcon(int icon) {
            ivIcon.setImageResource(icon);
        }

        public void setTitle(String title) {
            tvTitle.setText(title);
        }
    }
}

我运行了此代码,但屏幕上没有任何内容。我应该如何修改此代码以使列表项出现在屏幕上?

4 个答案:

答案 0 :(得分:3)

因为你在getView();

中什么都不做

@Override public View getView(int position, View view, ViewGroup viewGroup) { //do something here View view = LayoutInflater.from(context).... TextView tv =view.findView.... Item item = list.get(position); tv.set.... return view; }

答案 1 :(得分:3)

使用此代码:

    public class SettingsFragment extends Fragment {

    private ListView lvSettings;
    private SettingsAdapter settingsAdapter;
    private ArrayList<SettingsItem> alsettingsItems;
    private int[] icons = {
            R.drawable.ic_developer
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sms, null);
        lvSettings = (ListView) view.findViewById(R.id.lvSettings);
        initializeAdapter();
        settingsAdapter=new SettingsAdapter(alsettingsItems,getActivity());
        lvSettings.setAdapter(settingsAdapter);
        return view;
    }

    private void initializeAdapter() {
        int size = icons.length;
        alsettingsItems = new ArrayList<>();
        final SettingsItem settings_items= new SettingsItem();
        for (int i = 0; i < size; i++) {
            settings_items.setIvIcon(icons[i]);
            settings_items.setTvTitle(getResources().getStringArray(R.array.settings_items)[i]);
            alsettingsItems.add(settings_items);
        }

        settingsAdapter.notifyDataSetChanged();
    }

    public class SettingsAdapter extends BaseAdapter {

        private ArrayList<SettingsItem> itemsList = new ArrayList<>();
        private Context context;
        private ViewHolder viewHolder;

        public SettingsAdapter(ArrayList<SettingsItem> itemsList, Context context) {
            this.context = context;
            this.itemsList = itemsList;
        }

        @Override
        public int getCount() {
            return itemsList.size();
        }

        @Override
        public Object getItem(int i) {
            return itemsList.get(i);
        }

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

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public View getView(int i, View convertView, final ViewGroup viewGroup) {
            View view = convertView;
            if (view == null) {
                LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = li.inflate(R.layout.listitem_settings, null);
            } else {
                view = convertView;
            }

            viewHolder = new ViewHolder(view);

            final SettingsItem data = itemsList.get(i);
            viewHolder.ivIcon.setImageResource(data.getIvIcon());
            viewHolder.tvTitle.setText(data.getTvTitle());
            view.setTag(data);
            return view;
        }

        private class ViewHolder {
            private ImageView ivIcon;
            private TextView tvTitle;

            ViewHolder(final View view) {
                ivIcon = (ImageView) view.findViewById(R.id.ivSettingsItem);
                tvTitle = (TextView) view.findViewById(R.id.tvSettingsItem);
            }
        }


    }
    public  class SettingsItem{
        private Integer ivIcon;
        private String tvTitle;


        public String getTvTitle() {
            return tvTitle;
        }

        public void setTvTitle(String tvTitle) {
            this.tvTitle = tvTitle;
        }

        public Integer getIvIcon() {
            return ivIcon;
        }

        public void setIvIcon(Integer ivIcon) {
            this.ivIcon = ivIcon;
        }
    }
}

它会按你的意愿工作。

答案 2 :(得分:0)

lvSettings.setAdapter(settingsAdapter);
        initializeAdapter();

改为:

    initializeAdapter();
    lvSettings.setAdapter(settingsAdapter);

答案 3 :(得分:0)

尝试使用数组适配器来满足此类需求。在自定义条件下,应使用查看持有人模式重新使用,您的适配器应如下所示:

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}  

如果您是Android的新手,那么您可以阅读本教程,我觉得这很简单。 http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-baseadapter/