0

My android app has a simple chat function. The chats are stored in the on-board SQLite database. I am using a ListView with a SimpleCursorAdapter to display the chats. This is what I have:

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

I have a chat model that has a boolean, named localFlag, as one of its fields. If the chat is sent from the device, localFlag is set as true (or 1), if the chat is received from external to the device, localFlag is set as false (or 0).

My SQL call:

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

What I want to do is if the local flag is set, I would like to use this:

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

and if the local flag is not set, I would like to use this:

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

Notice I want to use R.layout.outgoing_line_of_chat vs R.layout.incoming_line_of_chat.

How can I go about accomplishing this?

Brian
  • 1,726
  • 2
  • 24
  • 62
  • in your ChatSimpleCursorAdapter constructor test if flag == 1 so you set your layout to sender else set your layout to recever – Oussema Aroua May 10 '16 at 20:33
  • Not sure how to do that. I know how to do the test, but what I don't know is how to use `ChatSimpleCursorAdapter simpleCursorAdapter = new ChatSimpleCursorAdapter(getContext(), R.layout.outgoing_line_of_chat, chatCursor, fromColumns, toViews, 0); ` within my extended SimpleCursorAdapter. – Brian May 10 '16 at 20:36
  • ChatSimpleCursorAdapter constructor still the same just delete the "int layout" and make the test to set the layout at your super() method, what need all that the params is you super() methode not the ChatSimpleCursorAdapter constructor – Oussema Aroua May 10 '16 at 20:39
  • I'm still not clear on what you are suggesting. – Brian May 11 '16 at 13:49

2 Answers2

0

Really I don't if I understood your question, but I'm going to try to help you.

You can do this in the 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);
}

Or this in the constructor method from your 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);
Crash
  • 338
  • 1
  • 2
  • 12
  • With your first method, I'm not sure how to get the local flag without looping through the cursor first. With your second method, I get an error that `Call to 'super()' must be first statement in constructor body`. – Brian May 11 '16 at 13:47
0

I found this post while googling around for an answer. It fit my needs and worked well. Hopefully, this will come in handy for somebody in the future. Here is a custom SimpleCursorAdapter class that I had to create in order to use it with my 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;
    }
}
Community
  • 1
  • 1
Brian
  • 1,726
  • 2
  • 24
  • 62