带有数据库的 ExpandableListView 中的 GridView

GridView Within ExpandableListView With Database

美好的一天!

我是 android/java 编程的新手,我正在尝试为我的应用程序创建一个类似历史记录的功能。我在内部使用 expandableListViewgridView(或 listView;我真的不知道除了这两个还能用什么)来显示上述历史。所有数据都来自 sqLite 数据库,该数据库有 3 个名为 RequestsPropertiesReqLine 的表,它们都链接在一起。

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


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


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


我能够填充 header 和 child 部分,但我在 child gridView 每个 child 上重复数据时遇到问题.我知道这是我将 ArrayList 传递给 GridView Adapter 的方式,但我不知道正确的做法或实施方式。

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

我在 SO 中看到了很多关于 post 的内容,但其中 none 似乎可以解决我的问题。谁能帮我这个?提前致谢!也欢迎提出修改建议。

这是我的历史片段代码:

@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

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

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;
}
}

希望对您有所帮助!