0

我一直在尝试在另一个“母亲”列表视图中导入多个列表视图。

我有一个创建 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 ....} 的建议

4

1 回答 1

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)。

于 2012-09-07T05:39:08.263 回答