我正在尝试将List View与片段中的自定义适配器(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;
}
}
请帮忙。
答案 0 :(得分:0)
通过查看您的代码,我认为问题在于您在适配器中设置值 即
adapter.addSectionHeaderItem(new ListClassChapter((three), 0));
每次创建新实例时。