【问题标题】:How can I modify an Android SimpleCursorAdapter to use a different layout depending on data from the database?如何根据数据库中的数据修改 Android SimpleCursorAdapter 以使用不同的布局?
【发布时间】:2016-05-10 20:29:23
【问题描述】:

我的安卓应用有一个简单的聊天功能。聊天记录存储在板载 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

我怎样才能做到这一点?

【问题讨论】:

  • 在您的 ChatSimpleCursorAdapter 构造函数中测试 if flag == 1 因此您将布局设置为发件人,否则将您的布局设置为接收
  • 不知道该怎么做。我知道如何进行测试,但我不知道如何在我的扩展 SimpleCursorAdapter 中使用ChatSimpleCursorAdapter simpleCursorAdapter = new ChatSimpleCursorAdapter(getContext(), R.layout.outgoing_line_of_chat, chatCursor, fromColumns, toViews, 0);
  • ChatSimpleCursorAdapter 构造函数仍然相同,只是删除“int 布局”并进行测试以在您的 super() 方法中设置布局,需要的所有参数是您的 super() 方法而不是 ChatSimpleCursorAdapter构造函数
  • 我仍然不清楚你的建议。

标签: android listview simplecursoradapter


【解决方案1】:

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

您可以在 Chat 类中执行此操作:

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 的构造方法中使用 this

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

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

【讨论】:

  • 使用您的第一种方法,我不确定如何在不先循环光标的情况下获取本地标志。使用您的第二种方法,我收到一个错误 Call to 'super()' must be first statement in constructor body.
【解决方案2】:

我在四处搜寻答案时发现了this 的帖子。它符合我的需要并且运行良好。希望这将在将来对某人派上用场。这是一个自定义的 SimpleCursorAdapter 类,我必须创建它才能将它与我的 Chat.java 类一起使用:

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多