【发布时间】:2018-11-28 14:42:26
【问题描述】:
我有一个包含大约 30 多个复选框的 ExpandableListView 的活动。因为我已经在使用 SQLite 在我的 android 应用程序中存储其他信息。我希望能够在我的 SQLite 中存储复选框状态(选中/未选中)。现在的问题是,如果我创建一个专门用于存储所有 30+ 复选框的表,这意味着我需要在该表的单行中包含 30+ 列?有没有更简单的方法来使用 SQLite 存储它们?
下面是我的清单的 ExpandableListView 代码:-
public class Checklist extends AppCompatActivity {
// ExpandableListAdapter listAdapter;
ExpListViewAdapterWithCheckbox listAdapter;
ExpandableListView expListView;
ArrayList<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
public TextView name_checklist;
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checklist);
myDb = new DatabaseHelper(this);
final int position = getIntent().getExtras().getInt("Position");
TextView name_checklist = (TextView) findViewById(R.id.checklistname);
Cursor gettripname = myDb.getTripName(position+1);
if (gettripname.getCount() == 0) {
Toast.makeText(Checklist.this, "No amount found !", Toast.LENGTH_LONG).show();
} else {
gettripname.moveToFirst();
String temp = gettripname.getString(0);
name_checklist.setText(temp+ " Trip Checklist");
}
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpListViewAdapterWithCheckbox(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding header data
listDataHeader.add("Toiletries");
listDataHeader.add("Clothes");
listDataHeader.add("Essentials");
listDataHeader.add("Travel Comfort");
//listDataHeader.add("")
// Adding child data
List<String> toiletries = new ArrayList<String>();
toiletries.add("Bandages");
toiletries.add("Contacts");
toiletries.add("Contacts Solution");
toiletries.add("Cologne");
toiletries.add("Conditioner");
toiletries.add("Cotton Buds");
toiletries.add("Deodorant");
toiletries.add("Hairbrush");
toiletries.add("Nail Clippers");
toiletries.add("Razor");
toiletries.add("Shampoo");
toiletries.add("Shaving Gel");
toiletries.add("Toothbrush");
toiletries.add("Toothpaste");
List<String> clothes = new ArrayList<String>();
clothes.add("Belt ");
clothes.add("Bras");
clothes.add("Casual Pants");
clothes.add("Casual Shirts");
clothes.add("Heavy Coat");
clothes.add("Jumper");
clothes.add("Light Jacket");
clothes.add("Pyjamas");
clothes.add("Scarf");
clothes.add("Shoes");
clothes.add("Shorts");
clothes.add("Socks");
clothes.add("Swimwear");
List<String> essentials = new ArrayList<String>();
essentials.add("Digital Camera");
essentials.add("Headache Pills");
essentials.add("Fever Pills");
essentials.add("Diarrhea Pills");
essentials.add("Flu Pills");
essentials.add("Cough Medicine");
essentials.add("Powerbank");
essentials.add("USB Power Socket");
essentials.add("Sunglasses");
essentials.add("Earphones");
List<String> travelcomfort = new ArrayList<String>();
travelcomfort.add("Travel Pillow");
travelcomfort.add("Travel Blanket");
travelcomfort.add("Eye Mask");
travelcomfort.add("Ear Plugs");
travelcomfort.add("Books");
travelcomfort.add("Magazines");
travelcomfort.add("Card games");
listDataChild.put(listDataHeader.get(0), toiletries); // Header, Child data
listDataChild.put(listDataHeader.get(1), clothes);
listDataChild.put(listDataHeader.get(2), essentials);
listDataChild.put(listDataHeader.get(3), travelcomfort);
}
}
【问题讨论】:
标签: android sqlite expandablelistview checklistbox