我正在构建聊天应用程序,但是在使用FirebaseRecyclerAdapter填充视图时遇到问题。我无法弄清楚是什么原因引起的。
我的活动课:
public class HomeActivity extends AppCompatActivity implements AIListener, View.OnClickListener {
RecyclerView recyclerView;
EditText editText;
RelativeLayout addBtn;
DatabaseReference ref;
Boolean flagFab = true;
FirebaseRecyclerAdapter<ChatMessageModel,chat_viewholder> adapter;
AIDataService aiDataService;
AIRequest aiRequest;
private AIService aiService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO},1);
recyclerView = findViewById(R.id.recycleview);
editText = findViewById(R.id.editText);
addBtn = findViewById(R.id.addBtn);
recyclerView.setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
ref = FirebaseDatabase.getInstance().getReference().child("chat");
ref.keepSynced(true);
final AIConfiguration configuration = new AIConfiguration("c0a5bbcb6d2b4c17a9b8ae15d7e4f43f",
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);
aiService = AIService.getService(this,configuration);
aiService.setListener(this);
aiDataService = new AIDataService(configuration);
aiRequest = new AIRequest();
addBtn.setOnClickListener(this);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ImageView fab_img = findViewById(R.id.fab_img);
Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.ic_send_white_24dp);
Bitmap img1 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_mic_white_24dp);
if (s.toString().trim().length()!=0 && flagFab){
ImageViewAnimatedChange(HomeActivity.this,fab_img,img);
flagFab=false;
}
else if (s.toString().trim().length()==0){
ImageViewAnimatedChange(HomeActivity.this,fab_img,img1);
flagFab=true;
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
FirebaseRecyclerOptions options =
new FirebaseRecyclerOptions.Builder<ChatMessageModel>()
.setQuery(ref, ChatMessageModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<ChatMessageModel, chat_viewholder>(options){
@NonNull
@Override
public chat_viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.msg_list,parent,false);
return new chat_viewholder(view);
}
@Override
protected void onBindViewHolder(@NonNull chat_viewholder viewHolder, int position, @NonNull ChatMessageModel model) {
if(model.getMsgUser().equals("user")){
viewHolder.rightText.setText(model.getMsgText());
viewHolder.rightText.setVisibility(View.VISIBLE);
viewHolder.leftText.setVisibility(View.GONE);
}else {
viewHolder.leftText.setText(model.getMsgText());
viewHolder.leftText.setVisibility(View.VISIBLE);
viewHolder.rightText.setVisibility(View.GONE);
}
}
};
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int msgcount = adapter.getItemCount();
int lastVisiblePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if(lastVisiblePosition == -1 || (positionStart >= (msgcount -1) && lastVisiblePosition == (positionStart - 1))){
recyclerView.scrollToPosition(positionStart);
}
}
});
recyclerView.setAdapter(adapter);
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
if(adapter != null) {
adapter.stopListening();
}
}
public void ImageViewAnimatedChange(Context c, final ImageView v, final Bitmap new_image) {
final Animation anim_out = AnimationUtils.loadAnimation(c, R.anim.zoom_out);
final Animation anim_in = AnimationUtils.loadAnimation(c, R.anim.zoom_in);
anim_out.setAnimationListener(new Animation.AnimationListener()
{
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
@Override public void onAnimationEnd(Animation animation)
{
v.setImageBitmap(new_image);
anim_in.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
@Override public void onAnimationEnd(Animation animation) {}
});
v.startAnimation(anim_in);
}
});
v.startAnimation(anim_out);
}
@Override
public void onClick(View v) {
String message = editText.getText().toString().trim();
Log.d("message",message);
if(!message.equals("")){
final ChatMessageModel chatMessageModel = new ChatMessageModel(message,"user");
ref.child("chat").push().setValue(chatMessageModel);
aiRequest.setQuery(message);
new AsyncTask<AIRequest,Void,AIResponse>(){
@Override
protected AIResponse doInBackground(AIRequest... aiRequests) {
final AIRequest request = aiRequests[0];
try{
final AIResponse response = aiDataService.request(request);
return response;
} catch (AIServiceException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(AIResponse aiResponse) {
super.onPostExecute(aiResponse);
if(aiResponse!= null){
Result result = aiResponse.getResult();
String reply = result.getFulfillment().getSpeech();
ChatMessageModel chatMessageModel1 = new ChatMessageModel(reply,"bot");
ref.child("chat").push().setValue(chatMessageModel);
}
}
}.execute(aiRequest);
}else {
aiService.startListening();
}
editText.setText("");
}
@Override
public void onResult(AIResponse response) {
Result result = response.getResult();
String message = result.getResolvedQuery();
Log.d("AI response",result.getFulfillment().getSpeech());
ChatMessageModel chatMessageModel0 = new ChatMessageModel(message, "user");
ref.child("chat").push().setValue(chatMessageModel0);
String reply = result.getFulfillment().getSpeech();
ChatMessageModel chatMessageModel = new ChatMessageModel(reply, "bot");
ref.child("chat").push().setValue(chatMessageModel);
}
@Override
public void onError(AIError error) {
}
@Override
public void onAudioLevel(float level) {
}
@Override
public void onListeningStarted() {
}
@Override
public void onListeningCanceled() {
}
@Override
public void onListeningFinished() {
}}
我的模型班级:-
public class ChatMessageModel {
private String msgText;
private String msgUser;
public ChatMessageModel(String msgText, String msgUser){
this.msgText = msgText;
this.msgUser = msgUser;
}
public ChatMessageModel(){
}
public String getMsgText() {
return msgText;
}
public void setMsgText(String msgText) {
this.msgText = msgText;
}
public String getMsgUser() {
return msgUser;
}
public void setMsgUser(String msgUser) {
this.msgUser = msgUser;
}}
我的适配器类:
public class chat_viewholder extends RecyclerView.ViewHolder {
TextView leftText, rightText;
public chat_viewholder(View itemView) {
super(itemView);
leftText = itemView.findViewById(R.id.leftText);
rightText = itemView.findViewById(R.id.rightText);
}}
请帮助我解决此错误,我提供的应用是该屏幕截图,我说了什么或键入了什么内容,然后该消息未显示在ScreenShot视图中。