ExpandableListView或嵌套ListViews中的多个ListView

时间:2012-09-07 05:04:12

标签: android android-layout android-listview

我一直在尝试在另一个“母亲”Listview中导入多个Listviews。

我有一个创建ListViews的类,然后是调用这些视图的Activity类。我唯一能做的就是用ListPerms @条目填充“母亲”(我猜对象)

这是我的多个ListViews类

public ListPerms(Context context, String a, int docid) {
    super(context);
    table=a;
    did=docid;
    list= (ListView) findViewById(R.id.specific_perm_list);
    getdata(list,ar_list);
}


private void getdata( ListView list, ArrayList<String> arlist){
    Database openHelper = new Database(getContext());
    myDB = openHelper.getReadableDatabase(); 
    myDB=SQLiteDatabase.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READONLY);

    try{fill(list,arlist);}catch(Exception e){Log.e("NAT EXISTANT","THIS->"+e);}

}

private void fill( ListView list, ArrayList<String> arlist){
    Cursor temp = null;
    Cursor buffer = null;
    String type_from_table = null;
    String[] items = null;

    if(table=="med") {type_from_table = "medication";}
    if(table=="test") {type_from_table = "test";}
    if(table=="all") {type_from_table = "allergy";}
    if(table=="proc") {type_from_table = "procedure";}
    if(table=="cond") {type_from_table = "condition";}
    if(table=="vacc") {type_from_table = "vaccine";}


    temp = fetchOption("SELECT * FROM permission WHERE did="+did+" AND type='"+type_from_table+"'");
    if(temp.getCount()>0){

        buffer = fetchOption("SELECT * FROM user_"+table+" WHERE id="+temp.getString(temp.getColumnIndex("fileid")));
        items = new String[] {buffer.getString(buffer.getColumnIndex("name"))};
        arlist.addAll( Arrays.asList(items) ); 
        listAdapter = new ArrayAdapter<String>(getContext(), R.layout.item, arlist); 

        for(int i=1;i<temp.getCount();i++){
            temp.moveToNext();
            buffer = fetchOption("SELECT * FROM user_"+table+" WHERE id="+temp.getString(temp.getColumnIndex("fileid")));
            listAdapter.add(buffer.getString(buffer.getColumnIndex("name"))); 
        }

        list.setAdapter(listAdapter);
    }else{
        items = new String[] { "None."}; 
        arlist.addAll( Arrays.asList(items) ); 
        listAdapter = new ArrayAdapter<String>(getContext(), R.layout.item, arlist);
        list.setAdapter(listAdapter);
    }
}

Activity类跟在哪里(现在)我有一个ExpandableListView,但我真的很想知道ELV是如何工作的

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.docp);
        general = (ExpandableListView) findViewById(R.id.Tot_perm_list);
    //random data
}
protected void onResume(){
    super.onResume();

    ListPerms me = new ListPerms(getApplicationContext(),"med",did);
    ListPerms te = new ListPerms(getApplicationContext(),"test",did);
    ListPerms all = new ListPerms(getApplicationContext(),"all",did);
    ListPerms proc = new ListPerms(getApplicationContext(),"proc",did);
    ListPerms cond = new ListPerms(getApplicationContext(),"cond",did);
    ListPerms vacc = new ListPerms(getApplicationContext(),"vacc",did);

    me.setActivated(isChild());
    te.setActivated(isChild());
    all.setActivated(isChild());
    proc.setActivated(isChild());
    cond.setActivated(isChild());
    vacc.setActivated(isChild());
    list.add(me);
    list.add(te);
    list.add(all);
    list.add(proc);
    list.add(cond);
    list.add(vacc);

    general.setAdapter(listAdapter);
    general.addChildrenForAccessibility(list);
    //what do I do
}

有关如何进行ListView {LV,Lv ...}或ExpandableListView {LV,LV ....}的建议

1 个答案:

答案 0 :(得分:1)

根据我的理解,将两个或多个列表视图合并为一个并不是一个好主意。你肯定会遇到滚动问题(当然,一些肮脏的黑客可以帮助修复它),而且可能性能会比单个列表更差。

我建议您使用ExpandableList和ExpandableListAdapter。将列表合并为一个您需要编写它的适配器 例如它可能如下所示:

...
// Your implementation of ExpandableListAdapter

// To store all 'child' adapters, it should be filled by the owner
SparseArray<BaseAdapter> mChildAdapters = new SparseArray<BaseAdapter>();

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final BaseAdapter childAdapter = mChildAdapters.get(groupPosition);
    View view = null;

    if (childAdapter != null) {
        view = childAdapter.getView(childPosition, convertView, parent);
    }

    return view;
}

@Override
public int getChildrenCount(int groupPosition) {
    int childrenCount = 0;
    final BaseAdapter childAdapter = mChildAdapters.get(groupPosition);

    if (childAdapter != null) {
        childrenCount = mChildAdapter.getCount();
    }

    return childrenCount;
}
...

请注意,上面的代码只是草稿,但我希望这个想法很清楚。这个方法你可能面临的唯一技巧 - convertView你在一个childAdapter中收到的可能来自另一个,所以在重用convertView之前应该应用一些(非常简单的)检查。

此外,可扩展列表可以让您很好地为每个列表提供标题(通过groupView)。