带数据库的可扩展列表视图中的 GridView [英] GridView Within ExpandableListView With Database

查看:22
本文介绍了带数据库的可扩展列表视图中的 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!

我是 android/java 编程的新手,我正在尝试为我的应用程序创建一个历史函数.我在内部使用 expandableListViewgridView(或 listView;我真的不知道除了这两个之外还能使用什么)显示所述历史.所有数据都来自 sqlite 数据库,该数据库有 3 个表,分别命名为 RequestsProperties &ReqLine 全部链接在一起.

I'm a newbie at android/java programming and I'm trying to create, kinda like, a history function to my app. I'm using an expandableListView with a gridView (or listView; I don't really know what else to use aside from these two) inside to show said history. All the data comes from sqLite database which has 3 tables named Requests, Properties & ReqLine all linked together.

我正在尝试创建的格式是这样的(抱歉,还不能发布图片):

The format I'm trying to create is something like this (Sorry, cant post images yet):

HEADER: TextView ----- 来自 db 1st table 的数据


HEADER: TextView ----- data from db 1st table

CHILD:TextView ----- 来自第二个
的数据
GRID:
1) 来自 DB 3rd 表的项目
2) 项目来自 DB 3rd table

CHILD: TextView ----- data from 2nd
GRID:
1)item from DB 3rd table
2)item from DB 3rd table

我能够填充标题和子部分,但我在子 gridView 重复每个子项的数据时遇到问题.我知道这是我将 ArrayList 传递给 GridView Adapter 的方式,但我没有正确的方法或如何实现它.

I was able to populate the header and child portions but I'm having trouble with the child gridView repeating data on every child. I know it's with the way I'm passing the ArrayList to the GridView Adapter but I don't the correct way of doing it or how to implement it.

我尝试了很多方法,包括将另一个数组列表添加到我的头类模型中,但我不知道应该如何在我的适配器中获取它的 childcount.

I tried a bunch of ways including adding another arraylist to my header class model but I don't know how I should get the childcount for it in my adapter.

我在 SO 中看到很多帖子,但似乎没有一个可以解决我的问题.谁能帮我这个?提前致谢!也欢迎提出修改建议.

I've seen lots on posts here in SO but none of them seems to have the solution to my problem. Can anyone help me with this? Thanks in advance! Suggestions on revisions are welcome as well.

这是我的历史片段代码:

Here's my code for the history fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.history_tab_layout, container, false);

    openHelper = new DatabaseHelper(getActivity());
    myDb = openHelper.getReadableDatabase();
    TextView _number = (TextView) rootView.findViewById(R.id.lblNumber);
    TextView _date = (TextView) rootView.findViewById(R.id.lblHistoryDate);
    TextView _transtype = (TextView) rootView.findViewById(R.id.lblHistoryTransType);
    TextView _amount = (TextView) rootView.findViewById(R.id.lblHistoryAmount);

    Cursor mCursor = null;
    Cursor dataCursor = null;
    Cursor itemCursor = null;
    String Query = "SELECT * FROM Requests";
    mCursor = myDb.rawQuery(Query, null);
    transHistory = new ArrayList<HistoryHeader>();
    gridHistory = new ArrayList<GridItems>();
    int count = 1;
    if(mCursor.getCount()!=0) {
        while (mCursor.moveToNext()) {
            String reqDate = mCursor.getString(mCursor.getColumnIndex("RequestDate"));
            String reqTransType = mCursor.getString(mCursor.getColumnIndex("TransType"));
            String reqTotalAmt = mCursor.getString(mCursor.getColumnIndex("AmtTotal"));
            String reqPCode = mCursor.getString(mCursor.getColumnIndex("Property"));
            String BaseId = mCursor.getString(0);
            HistoryHeader history = new HistoryHeader(count, reqDate, reqTransType, reqTotalAmt);

            String Query2 = "SELECT * FROM Projects WHERE PrjCode = '"+ mCursor.getString(3) +"'";
            dataCursor = myDb.rawQuery(Query2, null);
            if (dataCursor.getCount()!=0){
                while (dataCursor.moveToNext()){
                    String reqPName = dataCursor.getString(dataCursor.getColumnIndex("PrjName"));
                    history.setItemList(createItems(reqPCode, reqPName, 1));
                }
            }

            String Query3 = "SELECT * FROM ReqLine WHERE Base_Id = "+ BaseId;
            itemCursor = myDb.rawQuery(Query3, null);
            if (itemCursor.getCount()!=0) {
                while (itemCursor.moveToNext()) {
                    String reqPurpose = itemCursor.getString(itemCursor.getColumnIndex("Purpose"));
                    String reqAmount = itemCursor.getString(itemCursor.getColumnIndex("AmtLine"));
                    Integer reqNum = itemCursor.getInt(itemCursor.getColumnIndex("Linenum"));
                    GridItems test = new GridItems(reqNum, reqPurpose, reqAmount);
                    gridHistory.add(test);
                }
            }

            transHistory.add(history);
            count++;
        }
    }

    final ExpandableListView _Content = (ExpandableListView) rootView.findViewById(R.id.historyList);
    _Content.setIndicatorBounds(5,5);
    HistoryAdapter exAdpt = new HistoryAdapter(getActivity(), transHistory, gridHistory);
    _Content.setIndicatorBounds(0,20);
    _Content.setAdapter(exAdpt);

    return rootView;
}

private List<HistoryDetail> createItems(String _strPropertyCode, String _strPropertyName, int num) {
    List<HistoryDetail> result = new ArrayList<HistoryDetail>();
    for (int i=0; i < num; i++) {
        HistoryDetail item = new HistoryDetail(i, _strPropertyCode, _strPropertyName);
        result.add(item);
    }
    return result;
}

以及我的 HistoryAdapter.java 的代码

And the Code for my HistoryAdapter.java

public class HistoryAdapter extends BaseExpandableListAdapter {
private Context context;
private List<HistoryHeader> _listDataHeader;
private ArrayList<GridItems> _listGridItems;

public HistoryAdapter(Context context, List<HistoryHeader> _listDataHeader, ArrayList<GridItems> _listGridItems) {
    this.context = context;
    this._listDataHeader = _listDataHeader;
    this._listGridItems = _listGridItems;
}

@Override
public int getGroupCount() {
    return _listDataHeader.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    /*Integer size = _listDataHeader.get(groupPosition).getItemList().size();
    return size;*/
    return 1;
}

@Override
public Object getGroup(int groupPosition) {
    return _listDataHeader.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return _listDataHeader.get(groupPosition).getItemList().get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return _listDataHeader.get(groupPosition).hashCode();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return _listDataHeader.get(groupPosition).getItemList().get(childPosition).hashCode();
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.history_list_group, null);
    }
    TextView groupNum = (TextView) v.findViewById(R.id.historyNumber);
    TextView groupDate = (TextView) v.findViewById(R.id.historyDate);
    TextView groupTransType = (TextView) v.findViewById(R.id.historyTransType);
    TextView groupAmount = (TextView) v.findViewById(R.id.historyTotalAmt);

    HistoryHeader header = _listDataHeader.get(groupPosition);

    groupNum.setText(String.valueOf(header.getId()));
    groupDate.setText(header.getDate());
    groupTransType.setText(header.getTransType());
    groupAmount.setText(header.getTotalAmt());

    return v;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.history_list_child, null);
    }
    TextView itemPropCode = (TextView) v.findViewById(R.id.historyPropertyCode);
    TextView itemPropName = (TextView) v.findViewById(R.id.historyPropertyName);
    GridView itemGrid = (GridView) v.findViewById(R.id.historyItemList);
    ItemGridAdapter adapter = new ItemGridAdapter(context,_listGridItems);
    itemGrid.setAdapter(adapter);

    HistoryDetail detail = _listDataHeader.get(groupPosition).getItemList().get(childPosition);

    itemPropCode.setText(detail.getPropertyCode());
    itemPropName.setText(detail.getPropertyName());

    return v;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

然后是 ItemGridAdapter.java

Then the ItemGridAdapter.java

public class ItemGridAdapter extends BaseAdapter {
Context context;
ArrayList<GridItems> itemList;

public ItemGridAdapter(Context context, ArrayList<GridItems> itemList) {
    this.context = context;
    this.itemList = itemList;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return itemList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.history_grid_layout, null);
    }

    TextView _rowPurpose = (TextView) convertView.findViewById(R.id.rowPurpose);
    TextView _rowAmount = (TextView) convertView.findViewById(R.id.rowAmount);

    GridItems gridItems = itemList.get(position);
    _rowPurpose.setText(gridItems.getPurpose());
    _rowAmount.setText(gridItems.getAmount());

    return convertView;
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    // Return true for clickable, false for not
    return false;
}

模型类

public class HistoryHeader implements Serializable {
private long id;
private String _strDate;
private String _strTransType;
private String _strTotalAmt;

private List<HistoryDetail> itemDetails = new ArrayList<HistoryDetail>();
private List<GridItems> itemGrid = new ArrayList<>();

public HistoryHeader(long id, String _strDate, String _strTransType, String _strTotalAmt) {
    this.id = id;
    this._strDate = _strDate;
    this._strTransType = _strTransType;
    this._strTotalAmt = _strTotalAmt;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getDate() {
    return _strDate;
}

public void setDate(String _strDate) {
    this._strDate = _strDate;
}

public String getTransType() {
    return _strTransType;
}

public void setTransType(String _strTransType) {
    this._strTransType = _strTransType;
}

public String getTotalAmt() {
    return _strTotalAmt;
}

public void setTotalAmt(String _strTotalAmt) {
    this._strTotalAmt = _strTotalAmt;
}

public List<HistoryDetail> getItemList() {
    return itemDetails;
}

public void setItemList(List<HistoryDetail> itemDetails) {
    this.itemDetails = itemDetails;
}

public List<GridItems> getItemGrid(){ return itemGrid; }

public void setItemGrid(List<GridItems> itemGrid) { this.itemGrid = itemGrid; }


public class GridItems {
private long id;
private String _strPurpose, _strAmount;

public GridItems(long id, String _strPurpose, String _strAmount) {
    this.id = id;
    this._strPurpose = _strPurpose;
    this._strAmount = _strAmount;
}

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public String getPurpose() {
    return _strPurpose;
}
public void setPurpose(String _strPurpose) {
    this._strPurpose = _strPurpose;
}
public String getAmount() {
    return _strAmount;
}
public void setAmount(String _strAmount) {
    this._strAmount = _strAmount;
}

推荐答案

试试这个:

Cursor mCursor = null;
Cursor dataCursor = null;
Cursor itemCursor = null;
String Query = "SELECT * FROM Requests";
mCursor = myDb.rawQuery(Query, null);
transHistory = new ArrayList<HistoryHeader>();
//allGridHistory = new ArrayList<ArrayList<GridItems>>(); // Changed2
//gridHistory = new ArrayList<GridItems>();
int count = 1;
if(mCursor.getCount()!=0) {
    while (mCursor.moveToNext()) {
        String reqDate = mCursor.getString(mCursor.getColumnIndex("RequestDate"));
        String reqTransType = mCursor.getString(mCursor.getColumnIndex("TransType"));
        String reqTotalAmt = mCursor.getString(mCursor.getColumnIndex("AmtTotal"));
        String reqPCode = mCursor.getString(mCursor.getColumnIndex("Property"));
        String BaseId = mCursor.getString(0);
        HistoryHeader history = new HistoryHeader(count, reqDate, reqTransType, reqTotalAmt);

        String Query2 = "SELECT * FROM Projects WHERE PrjCode = '"+ mCursor.getString(3) +"'";
        dataCursor = myDb.rawQuery(Query2, null);
        if (dataCursor.getCount()!=0){
            while (dataCursor.moveToNext()){
                String reqPName = dataCursor.getString(dataCursor.getColumnIndex("PrjName"));
                history.setItemList(createItems(reqPCode, reqPName, 1));
            }
        }

        String Query3 = "SELECT * FROM ReqLine WHERE Base_Id = "+ BaseId;
        itemCursor = myDb.rawQuery(Query3, null);
        if (itemCursor.getCount()!=0) {
            gridHistory = new ArrayList<GridItems>(); // Added
            while (itemCursor.moveToNext()) {
                String reqPurpose = itemCursor.getString(itemCursor.getColumnIndex("Purpose"));
                String reqAmount = itemCursor.getString(itemCursor.getColumnIndex("AmtLine"));
                Integer reqNum = itemCursor.getInt(itemCursor.getColumnIndex("Linenum"));
                GridItems test = new GridItems(reqNum, reqPurpose, reqAmount);
                gridHistory.add(test);
            }
            history.setItemGrid(gridHistory); // Changed2
        }

        transHistory.add(history);
        count++;
    }
}

final ExpandableListView _Content = (ExpandableListView) rootView.findViewById(R.id.historyList);
_Content.setIndicatorBounds(5,5);
HistoryAdapter exAdpt = new HistoryAdapter(getActivity(), transHistory); // Changed2

适配器:

public class HistoryAdapter extends BaseExpandableListAdapter {
private Context context;
private List<HistoryHeader> _listDataHeader;
//private ArrayList<GridItems> _listGridItems;

public HistoryAdapter(Context context, List<HistoryHeader> _listDataHeader) {
    this.context = context;
    this._listDataHeader = _listDataHeader;
    //this._listGridItems = _listGridItems;
}

@Override
public int getGroupCount() {
    return _listDataHeader.size();
}

@Override
public int getChildrenCount(int groupPosition) {
/*Integer size = _listDataHeader.get(groupPosition).getItemList().size();
return size;*/
    return 1;
}

@Override
public HistoryHeader getGroup(int groupPosition) {
    return _listDataHeader.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return _listDataHeader.get(groupPosition).getItemList().get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return _listDataHeader.get(groupPosition).hashCode();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return _listDataHeader.get(groupPosition).getItemList().get(childPosition).hashCode();
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.history_list_group, null);
    }
    TextView groupNum = (TextView) v.findViewById(R.id.historyNumber);
    TextView groupDate = (TextView) v.findViewById(R.id.historyDate);
    TextView groupTransType = (TextView) v.findViewById(R.id.historyTransType);
    TextView groupAmount = (TextView) v.findViewById(R.id.historyTotalAmt);

    HistoryHeader header = _listDataHeader.get(groupPosition);

    groupNum.setText(String.valueOf(header.getId()));
    groupDate.setText(header.getDate());
    groupTransType.setText(header.getTransType());
    groupAmount.setText(header.getTotalAmt());

    return v;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
    View v = convertView;

    // As for using grid view, there is only one child, no suitable view for reuse/recycle.
    //if (v == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.history_list_child, null);
    //}

    TextView itemPropCode = (TextView) v.findViewById(R.id.historyPropertyCode);
    TextView itemPropName = (TextView) v.findViewById(R.id.historyPropertyName);
    GridView itemGrid = (GridView) v.findViewById(R.id.historyItemList);

    // Get the child list of this group/header.
    List<GridItems> history_list_child = getGroup(groupPosition).getItemGrid();

    // As for using grid view, onChildClickListener cannot be used. You may need to pass the group/header object
    // to grid view adapter and do onClick inside getView. First try your original way to display data correctly.
    ItemGridAdapter adapter = new ItemGridAdapter(context, history_list_child);
    itemGrid.setAdapter(adapter);

    HistoryDetail detail = _listDataHeader.get(groupPosition).getItemList().get(childPosition);
    itemPropCode.setText(detail.getPropertyCode());
    itemPropName.setText(detail.getPropertyName());

    return v;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

希望有帮助!

这篇关于带数据库的可扩展列表视图中的 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆