Android 如何将两个数据库表中的数据合并到一个listView中

Android 如何将两个数据库表中的数据合并到一个listView中,android,listview,join,mergecursor,Android,Listview,Join,Mergecursor,我对如何将两个单独的数据库查询合并到一个listView感到非常困惑 当前,我的listView由以下适配器填充,该适配器查询数据库中的损坏组件表,并提供特定位置的损坏组件列表: private class MyListAdapter extends ResourceCursorAdapter { // In your ListActivity class, create a new inner class that extends ResourceCursorAdapter.

我对如何将两个单独的数据库查询合并到一个listView感到非常困惑

当前,我的listView由以下适配器填充,该适配器查询数据库中的损坏组件表,并提供特定位置的损坏组件列表:

private class MyListAdapter extends ResourceCursorAdapter { 

    // In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. 
    //This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:

    public MyListAdapter(Context context, Cursor cursor) { 
        super(context, R.layout.row_location, cursor);
    } 

    @Override
    public void bindView(View view, Context context, Cursor cursor) { 
        TextView text_first_line = (TextView) view.findViewById(R.id.location_row_item_main_text);
        TextView text_second_line = (TextView) view.findViewById(R.id.location_row_item_secondary_text);
        ImageView flagIcon = (ImageView) view.findViewById(R.id.flagIcon);

        String row_text_component = cursor.getString(cursor.getColumnIndex(RMDbAdapter.COMPONENT));
        String row_text_position = ", Position " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.POSITION));
        if(row_text_position.equals(", Position Not Applicable")){
            row_text_position = "";
        }
        String row_text_action = " - " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.ACTION_REQUIRED));

        text_first_line.setText(row_text_component + row_text_position + row_text_action);
        text_second_line.setText("Dexion Speedlock, S Duty, 3000mm");

        String risk = cursor.getString(cursor.getColumnIndex(RMDbAdapter.RISK));
        if (risk.equals("Red Risk")){
            flagIcon.setImageResource(R.drawable.red_flag);
        }
        else if (risk.equals("Green Risk")){
            flagIcon.setImageResource(R.drawable.green_flag);
        }
        else if (risk.equals("No Risk")){
            flagIcon.setImageResource(R.drawable.note);
        }

    }
}
当我在活动启动时调用以下命令时会触发此操作:

private void setAdapter(){

    // Get a Cursor for the list items

    Cursor listComponentCursor = rmDbHelper.fetchDamagedComponentsForLocation(locationId);
    componentCursorSize = listComponentCursor.getCount();
    startManagingCursor(listComponentCursor); 
    // set the custom list adapter     
    setListAdapter(new MyListAdapter(this, listComponentCursor));

}
因此,我还想在一个单独的表(这次是issues表)上创建第二个查询,并将其添加到受损组件列表下的listView中

阅读本文,我认为应该使用Join合并光标。然而,根据我的研究,我仍然不知道如何将这两个概念集成到我的代码中


有人能给我指出正确的方向吗?

尽管您没有显示它,但我假设您覆盖了游标适配器的
newView
方法。MergeCursor只会将一个查询的结果堆叠在下一个查询的顶部。CursorJoiner几乎可以将结果并排放置。听起来你想要一个合并光标

如果只想将第二个查询的结果连接到第一个查询,请执行两个查询并创建一个合并游标。如果需要它们具有不同的布局,或者它们具有不同的列名,则需要覆盖适配器中的
getItemViewType
方法。然后,在
newView
方法中,可以根据类型对不同的视图进行充气。在
bindView
中,需要检查类型并将正确的值应用于正确的视图

为您的代码提供一些随机提示:

1) 使用ViewHolder模式,速度更快


2) 最好是使用
“literal string”.equals(variable)
而不是
variable.equals(“literal string”)
,因为literal string不能抛出NullPointerException

嗨,多谢您的快速响应和提示(我有机会会集成这些)。您看到的是填充listView的所有代码-我没有覆盖newView(以前从未听说过)。你能详细说明一下这个要素吗?此外,我在每个表中都有不同的列名,您是否有一个如何覆盖getItemViewType的示例?感谢您迄今为止的帮助。您必须使用游标适配器覆盖两种方法:newView和bindView。newView膨胀或创建视图。bindView将值指定给视图。看起来ResourceCursorAdapter子类为您解决了这个问题。您是否有任何特定的理由使用该子类?如果没有,就扩展游标Adapter是的。我发布了一个非常基本的游标适配器版本,让你看到很抱歉,这让我很痛苦,但是你所说的很多话我都有点听不懂。我在setAdapter()中创建了我的listComponentCursor,所以在这里我使用创建第二个cursor和使用MergeCursor吗?如果是这样,那么bindView中的代码将与第二组数据无关。迷惑网!在进行两个查询之后。创建一个mergecursor(
newmergecursor(newcursor[]{a,b})
。将其传递给适配器