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