【问题标题】:Using ListView with custom adapter (baseAdapter) in fragment在片段中使用带有自定义适配器 (baseAdapter) 的 ListView
【发布时间】:2017-08-05 12:49:09
【问题描述】:

我正在尝试在片段中使用带有自定义适配器 (baseAdapter) 的列表视图。

当我直接在 MainActivity 中使用这段代码时,一切正常,但是当我在片段中使用它时,它没有崩溃,但没有显示任何内容,它只是一个空白片段。不在活动中显示。

片段代码

    public class fragment_activity_pisaniya extends ListFragment {
        List<String> listChapter = new ArrayList<>();
        //private CustomChapterListAdapter adapter;
        private ListView listView;
        ArrayList<String> items = new ArrayList<String>();
        Context context;
        private View view;
       public fragment_activity_pisaniya() { }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
             super.onCreateView(inflater, container, savedInstanceState);
            if (view==null) {
                view = inflater.inflate(R.layout.fragment_pisaniya_book, container, false);
            }
                listView = (ListView)view.findViewById(R.id.listView);
            CustomChapterListAdapter adapter = new CustomChapterListAdapter(getActivity());
            AssetManager assetManager = getResources().getAssets();
            int z = 1;
            int a = 1;
            try {
                for (int i = 0; i < assetManager.list("html").length; i++) {
                    String[] file = new String[0];
                    try {
                        file = assetManager.list("html");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String hhh = file[i];
                    listChapter.add(hhh.replaceAll(".html$", "").replaceAll("_", " "));
                    String arr[] = hhh.split("_", 4);
                    String three = arr[2].replaceAll(".html$", "").replaceAll("_", " ");

                    if (Integer.valueOf(arr[0]) == z) {
                        adapter.addSectionHeaderItem(new ListClassChapter((three), 0));
                        String four = arr[3].replaceAll(".html$", "").replaceAll("_", " ");

                        z++;
                    } else {
                        if (Integer.valueOf(arr[0]) == a) {
                            String item=arr[3].replaceAll(".html$", "").replaceAll("_", " ");
                            adapter.addItem(new ListClassChapter(item));
                        } else {
                            a++;
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            return view;
        }

    }

自定义适配器

 public class CustomChapterListAdapter extends BaseAdapter {
        private static final int TYPE_ITEM = 0;
        private static final int TYPE_HEADER = 1;

        private ArrayList<ListClassChapter> mData = new ArrayList<ListClassChapter>();
        private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();

        private LayoutInflater mInflater;

        public CustomChapterListAdapter(Context context) {

            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void addItem(final ListClassChapter item) {
            mData.add(item);
            notifyDataSetChanged();
        }

        public void addSectionHeaderItem(final ListClassChapter item) {
            mData.add(item);
            sectionHeader.add(mData.size() - 1);
            notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
            return sectionHeader.contains(position) ? TYPE_HEADER : TYPE_ITEM;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

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

        @Override
        public ListClassChapter getItem(int position) {
            return mData.get(position);
        }

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

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

            if (convertView == null) {
                holder = new ViewHolder();
                switch (rowType) {
                    case TYPE_ITEM:
                        convertView = mInflater.inflate(R.layout.row_item, null);
                        holder.textView = (TextView) convertView.findViewById(R.id.txtName);
                        holder.txtValue = (TextView) convertView.findViewById(R.id.txtValue);
                        break;
                    case TYPE_HEADER:
                        convertView = mInflater.inflate(R.layout.header_item, null);
                        holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if(rowType == TYPE_ITEM){
                holder.textView.setText(mData.get(position).gettName());
                holder.txtValue.setText(""+mData.get(position).getAmount());
            }else if(rowType == TYPE_HEADER){
                holder.textView.setText(mData.get(position).gettName());
            }
            return convertView;
        }
        public static class ViewHolder {
            public TextView textView;
            public TextView txtValue;
        }
    }

请帮忙。

【问题讨论】:

    标签: android listview android-fragments custom-adapter


    【解决方案1】:

    通过查看您的代码,我认为问题出在您在适配器中设置值时 即

    adapter.addSectionHeaderItem(new ListClassChapter((three), 0));
    

    每次您创建新实例时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多