【问题标题】:How a merge 3 database lists to one and show it on a ListView;如何将 3 个数据库合并为一个并在 ListView 上显示;
【发布时间】:2019-12-05 19:16:15
【问题描述】:

我有 3 个数据库列表,我想将它们合并为一个并将数据显示到列表视图中,如下所示:

vathmoss + " " firstnames + " " +lastnames 

现在我只显示名字...

Cursor c = database.rawQuery("Select * From " + PDFDatabaseManager.DATABASE_USERS_TABLE + ";", null);

        ArrayList<String> vathmoss = new ArrayList<>();
        ArrayList<String> firstnames = new ArrayList<>();
        ArrayList<String> lastnames = new ArrayList<>();

        while (c.moveToNext()) {
            vathmoss.add(c.getString(0));
            firstnames.add(c.getString(1));
            lastnames.add(c.getString(2));
        }
        c.close();

        final ListView listUser = (ListView) popup.findViewById(R.id.listview_user);

        if (!vathmoss.isEmpty() && !firstnames.isEmpty() && !lastnames.isEmpty()) {
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.context, android.R.layout.simple_list_item_1,firstnames);
            listUser.setAdapter(adapter);

        }

【问题讨论】:

    标签: android sql listview merge


    【解决方案1】:

    做这样的事情:

    Cursor c = database.rawQuery("Select * From " + 
    PDFDatabaseManager.DATABASE_USERS_TABLE + ";", null);
    
        ArrayList<String> vathmoss = new ArrayList<>();
        ArrayList<String> firstnames = new ArrayList<>();
        ArrayList<String> lastnames = new ArrayList<>();
    
        ArrayList<String> mergednames = new ArrayList<>();
    
        while (c.moveToNext()) {
            vathmoss.add(c.getString(0));
            firstnames.add(c.getString(1));
            lastnames.add(c.getString(2));
    
            mergednames.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2))
        }
        c.close();
    
        final ListView listUser = (ListView) popup.findViewById(R.id.listview_user);
    
        if (!vathmoss.isEmpty() && !firstnames.isEmpty() && !lastnames.isEmpty()) {
    
            //In the below line, pass mergednames instead of firstname as last parameter
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.context, android.R.layout.simple_list_item_1,mergednames);
            listUser.setAdapter(adapter);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2013-12-08
      • 2014-05-15
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多