将项目从本地.txt文件加载到数组中并填充到可扩展列表中。
这是我的CustomAdapter:
public class MyCustomAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
private static final String TAG = "MyCustomAdapter: ";
private static final boolean DEBUG = true;
private ArrayList<Parent> arraylist;
Context aContext;
public MyCustomAdapter(Context context, ArrayList<Parent> parent){
mParent = parent;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arraylist = new ArrayList<>();
this.arraylist.addAll(parent);
}
@Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
@Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
return mParent.get(i).getArrayChildren().size();
}
@Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
public Object getGroupName(int i){
return mParent.get(i).getName();
}
@Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder.groupPosition = groupPosition;
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup, false);
}
Parent item = mParent.get(groupPosition);
//if (DEBUG)Log.e(TAG, "Selected text: " + (item.getName()));
TextView nameText = (TextView) view.findViewById(R.id.nameValue);
nameText.setText(item.getName());
TextView countryLanguageGenreText = (TextView) view.findViewById((R.id.countryLanguageGenre));
countryLanguageGenreText.setText(item.getCountryLanguageGenre());
view.setTag(holder);
//return the entire view
return view;
}
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup viewGroup) {
ViewHolder holder = new ViewHolder();
holder.childPosition = childPosition;
holder.groupPosition = groupPosition;
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup, false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
textView.setText(mParent.get(groupPosition).getArrayChildren().get(childPosition));
view.setTag(holder);
//return the entire view
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
}
protected class ViewHolder {
protected int childPosition;
protected int groupPosition;
protected Button button;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
mParent.clear();
if (charText.length() == 0) {
mParent.addAll(arraylist);
}
else
{
for (Parent wp : arraylist)
{
if (wp.getSearchString().toLowerCase(Locale.getDefault()).contains(charText))
{
mParent.add(wp);
}
}
}
notifyDataSetChanged();
}
}
答案 0 :(得分:1)
列表视图的一个特质是它可以被回收并仅为可见列表项重新创建。问题不在于列表视图中的项目长度。这是阅读文件的低效率。
我建议您使用数据库支持的设计,这将使应用程序更加稳定,并且您可以轻松地使过滤器正常工作。
答案 1 :(得分:0)
我同意@Diyoda关于将文本文件中的数据导入数据库支持的设计。然后可能看看通过droidQuery做ajax请求。 Can ajax call be done in Android?
http://phil-brown.github.io/droidQuery/
当用户向下滚动时,您会对更多数据执行异步请求。您可以像使用jQuery ajax一样轮询URL,然后将参数传递给适配器,该适配器从数据库中获取下一组数据。