I am trying to make a ChatBot in android for my college project, I am trying to send a message in the chat (RecyclerView) and it is displayed to me (Good part) but I don't know how I can add to the RecyclerView another View that will represent the response from the ChatBot module (when I will create it).
My adapator class:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> LIST_MESSAGES = new ArrayList<>();
private String text_message_name = new String();
private Context nContext;
private String typeHumanOrBot;
public RecyclerViewAdapter(ArrayList<String> MESSAGE_BODY, String USER_NAME, Context nContext,String typeHumanOrBot) {
Log.d("Activitati","<<<<< IN RecyclerViewAdapter() >>>>");
this.LIST_MESSAGES = MESSAGE_BODY;
this.text_message_name = USER_NAME;
this.nContext = nContext;
this.typeHumanOrBot= typeHumanOrBot;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if(typeHumanOrBot.equals("HUMAN")){
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_send,parent, false);
}else{
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_receive,parent, false);
}
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d(TAG,"onBindViewHolder: CALLED");
holder.text_message_body.setText(LIST_MESSAGES.get(position));
holder.text_message_name.setText(text_message_name);
}
@Override
public int getItemCount() {
return LIST_MESSAGES.size();
}
/**
* Tine widget-urile in memorie pentru fiecare widget ...
*/
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image_message_profile;
TextView text_message_name;
TypeWriter text_message_body;
public ViewHolder(View item){
super(item);
image_message_profile = item.findViewById(R.id.image_message_profile);
text_message_name = item.findViewById(R.id.text_message_name);
text_message_body = item.findViewById(R.id.text_message_body);
}
}
And I call it like this:
private String HUMAN = "Human";
private String BOT = "Ben";
private ArrayList<String> questions = new ArrayList<>();
private ArrayList<String> responses = new ArrayList<>();
final RecyclerView recyclerView = findViewById(R.id.list_of_messages);
a_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String questionSTr =questionRAW.getText().toString();
questionRAW.setText("");
questions.add(questionSTr);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(questions,HUMAN,getApplicationContext(),"HUMAN");
recyclerView.setAdapter(adapter);
responses.add("Hmm... neah");
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
});
I can only display one type of View not 2 at once as I wish, I am still new at the RecyclerView is there something I must do so that I can insert 2 view's at a time ?