0
我試圖使用與片段自定義適配器(baseAdapter)列表視圖自定義適配器(baseAdapter)。使用的ListView與片段
當我我們這次代碼直接在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;
}
}
請幫助。