如何根据数据库中的数据修改 Android SimpleCursorAdapter 以使用不同的布局?

How can I modify an Android SimpleCursorAdapter to use a different layout depending on data from the database?

我的 android 应用程序有一个简单的聊天功能。聊天记录存储在机载 SQLite 数据库中。我正在使用带有 SimpleCursorAdapter 的 ListView 来显示聊天。这是我的:

public class Chat extends Fragment {
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        rootView = inflater.inflate(R.layout.chat, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View rootView, Bundle savedInstanceState){
        super.onViewCreated(rootView, savedInstanceState);
        displayChats();
    }

    public void displayChats(){
        DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
        Cursor chatCursor = databaseHelper.getChatsCursor();
        String[] fromColumns = {"messageInfo","messageText"};
        int toViews{R.id.message_info, R.id.message_text};
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.line_of_chat, chatCursor, fromColumns, toViews, 0);
        ListView listView = (ListView) rootView.findViewById(R.id.chat_text_display);
        listView.setAdapter(simpleCursorAdapter);
        databaseHelper.close();
    }
}

我有一个聊天模型,它有一个名为 localFlag 的布尔值作为其字段之一。如果聊天是从设备发送的,localFlag 设置为 true(或 1),如果聊天是从设备外部接收的,localFlag 设置为 false(或 0)。

我的 SQL 电话:

public static final String GET_ALL_CHATS = "select _id, localFlag, messageText, messageInfo from ChatTable";

public Cursor getChatsCursor(){
    SQLiteDatabase sqliteDB = this.getReadableDatabase();
    return sqliteDB.rawQuery(GET_ALL_CHATS, null);
}

我想做的是如果设置了本地标志,我想使用这个:

SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.outgoing_line_of_chat, chatCursor, fromColumns, toViews, 0);

如果没有设置本地标志,我想使用这个:

SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.incoming_line_of_chat, chatCursor, fromColumns, toViews, 0);

注意我想使用 R.layout.outgoing_line_of_chatR.layout.incoming_line_of_chat

我怎样才能完成这个?

如果我理解你的问题,我真的不知道,但我会尽力帮助你。

您可以在聊天中执行此操作 class:

if (localFlag) {
   ChatSimpleCursorAdapter simpleCursorAdapter = new ChatSimpleCursorAdapter(getContext(), R.layout.outgoing_line_of_chat, chatCursor, fromColumns, toViews, 0);
} else {
    ChatSimpleCursorAdapter simpleCursorAdapter = new ChatSimpleCursorAdapter(getContext(), R.layout.incoming_line_of_chat, chatCursor, fromColumns, toViews, 0);
}

或者在您的 ChatSimpleCursorAdapter 的构造方法中这样做

if (localFlag) {
   layout = R.layout.outgoing_line_of_chat;
} else {
   layout = R.layout.incoming_line_of_chat;
}

super(getContext(), layout, chatCursor, fromColumns, toViews, 0);

我在谷歌搜索寻找答案时找到了 this post。它符合我的需要并且运行良好。希望这会在将来对某些人派上用场。这是我必须创建的自定义 SimpleCursorAdapter class,以便与我的 Chat.java class:

一起使用
public class ChatSimpleCursorAdapter extends SimpleCursorAdapter {
    private Context context;

    private ChatSimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags){
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        if(convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            switch(getItemViewType(position)){
                case 0:
                    convertView = layoutInflater.inflate(R.layout.incoming_line_of_chat, null);
                    break;
                case 1:
                    convertView = layoutInflater.inflate(R.layout.outgoing_line_of_chat, null);
                    break;
            }
        }

        return super.getView(position, convertView, parent);
    }

    @Override
    public int getItemViewType(int position){
        Cursor cursor = getCursor();
        cursor.moveToPosition(position);
        int localFlag = cursor.getInt(1);
        if(localFlag == 1){
            return 1;
        }
        return 0;
    }

    @Override
    public int getViewTypeCount(){
        return 2;
    }
}