1

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 ?

Stefan Stef
  • 53
  • 1
  • 1
  • 9
  • 1
    Check out this : https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – Anonymous May 08 '18 at 11:56
  • I tryed that one but if i write something it doesn't display 2 view it displayes one type and then when i write again it toggles the view to another type i need to create in the same type 2 views not toggle one at a time.Hope i explained myself ok – Stefan Stef May 08 '18 at 12:22
  • 1
    Your LIST_MESSAGES should be an arraylist and the model should have a type key in which so that you can filter out which type of messages to display , if you still don't get it I will write a sample answer for you by editing your code ! – Anonymous May 08 '18 at 12:36
  • If it's not a lot to ask can you please write a sample answer by editing my code please? thank you – Stefan Stef May 08 '18 at 12:52
  • 2
    and rework your on click listener - my eyes are bleeding! Dont setAdapter every time you want to add something, write an "add(String txt)" methode in your adapter and notifyItemChanged – PKAP May 08 '18 at 13:02
  • 1
    As I said @PKAP new with this tools but still your answer resolved my problem :)) !! Grazie mille. – Stefan Stef May 08 '18 at 13:14

0 Answers0