【问题标题】:Fill ExpandableListAdapter with SQLite用 SQLite 填充 ExpandableListAdapter
【发布时间】:2014-07-01 02:17:09
【问题描述】:

我已经尝试了将近一天来解决这个问题,但我不知道出了什么问题。

我得到了这个代码

private void someFunction() {       

    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    List<String> tempNomoi = new ArrayList<String>();

    List<GeneralInfo> diamerismata = db.getAllDiamerismata();
    List<GeneralInfo> nomoi = new ArrayList<GeneralInfo>();

    for (GeneralInfo a : diamerismata) {
        listDataHeader.add(a.getDiamerisma());

        nomoi = db.GetNomousDiamerismatos(a.getDiamerisma());

        tempNomoi.clear();

        for (GeneralInfo b : nomoi) {

            tempNomoi.add(b.getNomo());

        }

        listDataChild.put(a.getDiamerisma(), tempNomoi);
        System.out.println(listDataChild);
    }

}

我得到了这个输出

I/System.out(5750): SELECT nomos FROM info WHERE diamerisma = 'diamerisma1'

I/System.out(5750): {diamerisma1=[nomos1, nomos3, nomos5]}

I/System.out(5750): SELECT nomos FROM info WHERE diamerisma = 'diamerisma2'

I/System.out(5750): {diamerisma2=[nomos2, nomos4], diamerisma1=[nomos2, nomos4]}

I/System.out(5750): SELECT nomos FROM info WHERE diamerisma = 'diamerisma3'

I/System.out(5750): {diamerisma2=[nomos6], diamerisma3=[nomos6], diamerisma1=[nomos6]}

(选择语句来自负责管理查询的函数)

从输出中可以看出,在每个循环之后,值都会被新的值覆盖...我想要{diamerisma1=[nomos1, nomos3, nomos5], diamerisma2=[nomos2, nomos4], diamerisma3=[nomos6]}

也许我想念 HashMaps 的工作原理……如果有人能指出我错在哪里,我将不胜感激

提前感谢您的阅读!

【问题讨论】:

  • Maybe there is something I miss about how HashMaps work 而不是...似乎您错过了不创建新对象不是创建新对象的观点(因此您使用的是相同的 - tempNomoi 实例)
  • @Selvin 所以你是说我需要和 listDataHeader 一样多的对象才能使 Hashmap 工作?
  • 嗯,不,我认为您应该创建 tempNomoi 的新实例,而不是在循环内清除它...因为现在您添加了相同的实例但使用不同的键(到 listDataChild 哈希图)
  • @Selvin 成功了!非常感谢!你真的为我节省了很多时间,我永远不会想到这一点!

标签: java android list sqlite hashmap


【解决方案1】:

您好,下面的代码对您有帮助

  View.OnClickListener listOnClick = new OnClickListener() {
      @Override
      public void onClick(View v) {
       favoriteList = db.getFavList();
       listView.setAdapter(new ViewAdapter());
      }
     };

//Get FavList
 public List<FavoriteList> getFavList(){
  String selectQuery = "SELECT  * FROM " + TABLE_TEST;
  SQLiteDatabase db = this.getWritableDatabase();
  Cursor cursor = db.rawQuery(selectQuery, null);
  List<FavoriteList> FavList = new ArrayList<FavoriteList>();
  if (cursor.moveToFirst()) {
   do {
    FavoriteList list = new FavoriteList();
    list.setId(Integer.parseInt(cursor.getString(0)));
    list.setName(cursor.getString(1));
    list.setAge(cursor.getString(2));
    FavList.add(list);
   } while (cursor.moveToNext());
  }
  return FavList;
 }

如果您需要更多详细信息,请参阅以下链接 http://mylearnandroid.blogspot.in/2014/04/android-sqlite-with-custom-listview.html

【讨论】:

  • 好吧,这对我的问题没有帮助,因为我知道如何填充我的列表......我只是被那个 HashMap 卡住了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多