gpt4 book ai didi

java - 从数据库填充ExpandableListView - 无法正常工作

转载 作者:IT王子 更新时间:2023-10-29 06:29:24 25 4
gpt4 key购买 nike

我有表格收据和日志,表格收据有很多日志。所以在logsrapitation.class上,我按排序和等级对日志进行分组(这两列来自表日志)。
我试图通过ExpandableListView来实现这一点。每组日志的父元素将由数据库中每组的排序和等级组成。
子元素将在子元素、质量、价格和某些计算价格中具有日志计数的文本视图。它应该是这样的:
enter image description here
我已经创建了ExpandableListView布局和适配器。但现在我无法从数据库中填充父元素和子元素。
这是我的代码,你可以看看我到目前为止做了什么。
这是expandablelistaadapter类:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listDataChild;
}

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

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expendable_items, null);
}

TextView logsMass = (TextView) convertView.findViewById(R.id.exp_lis_mass);
TextView logsPrice = (TextView) convertView.findViewById(R.id.exp_lis_price);
TextView logsMassPrice = (TextView) convertView.findViewById(R.id.ext_lis_sum_price);
logsMass.setText(childText);
logsPrice.setText(childText);
logsMassPrice.setText(childText);

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}

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

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

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.expandable_group, null);
}

TextView logsSort = (TextView) convertView.findViewById(R.id.exp_gr_sort);
TextView logsGrade = (TextView) convertView.findViewById(R.id.exp_gr_grade);
TextView logsCount = (TextView) convertView.findViewById(R.id.exp_gr_logs_count);
logsSort.setText(headerTitle);
logsGrade.setText(headerTitle);
logsCount.setText(headerTitle);

return convertView;
}

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

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

这是我的日志投降课:
public class LogsRecapitulation extends AppCompatActivity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_layout);
//recieve RecepitID and query to group_by logs
final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).groupBy("SortID, Grade").execute();
//get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
}

private void prepareListData() {

}
}

我现在的问题是用数据库中的数据填充ExpandableListView。以下是我的表类:
//receipt table
@Table(name = "Receipt")
public class Receipt extends Model {
@Column(name="Place")
String place;
@Column(name="ShippingNumber")
String shippingNumber;
@Column(name="Warehouse")
String warehouse;
@Column(name="Carrier")
String carrier;
@Column(name="LicencePlate")
String licencePlate;
@Column(name = "Driver")
String driver;
@Column(name = "Customer")
String customer;
@Column(name= "DestWarehouse")
String destWarehouse;
@Column(name = "Employee")
String employee;
@Column(name = "PriceType")
String priceType;
@Column(name = "PriceCorrection")
Double priceCorrection;
@Column(name = "PriceCorrection2")
Double priceCorrection2;
@Column(name = "Supplier")
String supplier;
@Column(name = "CreatedAt")
Date createdAt;
}
//logs table
@Table(name = "Logs")
public class Logs extends Model {
@Column(name="PlateNumber")
String plate_number;
@Column(name="SortID")
String sort_id;
@Column(name="Grade")
String grade;
@Column(name = "Diametar")
double diameter;
@Column(name="Length")
double length;
@Column(name="CreatedAt")
Date createdAt;
@Column(name="Receipt")
Receipt receipt;
@Column(name = "Price")
Price price;
}
//price table
@Table(name = "Price")
public class Price extends Model {
@Column(name = "Sort")
String sort;
@Column(name = "Grade")
String grade;
@Column(name = "Diameter")
double diameter;
@Column(name = "LengthDG")
double lengthDG;
@Column(name = "LengthGG")
double lengthGG;
@Column(name = "StumpPriceKn")
double stumpPrice_kn;
@Column(name = "RoadPriceKn")
double roadPrice_kn;
}

注意:我正在使用ActiveanDroid。
如果有人知道如何从数据库填充Expandable ListView,请提供帮助。
问题:如何从数据库成功填充ExpandableListView?

最佳答案

我不是ActiveAndroid的专家,但下面是使用光标创建ExpandableListView的步骤。
在db helper类中需要两个方法来收集需要的行。

public Cursor fetchGroup() {
String query = "SELECT * FROM rooms"
return mDb.rawQuery(query, null);
}

public Cursor fetchChildren(String room) {
String query = "SELECT * FROM devices WHERE id_room = '" + room + "'";
return mDb.rawQuery(query, null);
}

然后需要配置适配器(在活动中):
  public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
public MyExpandableListAdapter(Cursor cursor, Context context,int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
}

@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Cursor childCursor = mDbHelper.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("id_room"));
getActivity().startManagingCursor(childCursor);
childCursor.moveToFirst();
return childCursor;
}
}

最后,调用适配器并将其配置到列表中(在活动中):
private void fillData() {
mGroupsCursor = mDbHelper.fetchGroup();
getActivity().startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();

ExpandableListView elv = (ExpandableListView) getActivity().findViewById(android.R.id.list);

mAdapter = new MyExpandableListAdapter(mGroupsCursor, getActivity(),
R.layout.rowlayout_expgroup, // Your row layout for a group
R.layout.rowlayout_itemlist_exp, // Your row layout for a child
new String[] { "id_room" }, // Field(s) to use from group cursor
new int[] { android.R.id.room }, // Widget ids to put group data into
new String[] { "name_device", "state_device" }, // Field(s) to use from child cursors
new int[] { R.id.device, R.id.state }); // Widget ids to put child data into

lv.setAdapter(mAdapter); // set the list adapter.
}
}

所以所有的代码
public class List_Exp extends Activity {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mDbHelper = new YourDB(getActivity());
mDbHelper.open();
fillData();
}

private void fillData() {
// set list adapter here
}

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
// Your adapter
}
}

如果要捕获组/子级上的Click事件,请处理事件:
lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// Your child click code here
return true;
}
});

lv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, int groupPosition, long id) {
// Your group click code here
return true;
}
});

关于java - 从数据库填充ExpandableListView - 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37526988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com