- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 FirebaseDatabase 和 DialogFlow(api.ai) 构建一个 android 聊天机器人应用程序,并且我正在使用 RecyclerView 将消息加载到应用程序中。但是我无法将消息加载到应用程序中,每次运行该应用程序时它都会显示黑屏。
我已经尝试了 StackOverflow 上的所有建议答案,但我仍然无法将消息加载到应用程序中。
主 Activity
import ai.api.AIConfiguration;
import ai.api.AIListener;
import ai.api.AIServiceException;
import ai.api.android.AIDataService;
import ai.api.android.AIService;
import ai.api.model.AIError;
import ai.api.model.AIRequest;
import ai.api.model.AIResponse;
import ai.api.model.Result;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.firebase.ui.database.SnapshotParser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.List;
import java.util.Objects;
public class MainActivity extends AppCompatActivity implements AIListener{
RecyclerView recyclerView;
EditText editText;
RelativeLayout addBtn;
DatabaseReference ref;
private AIService aiService;
private Context context;
FirebaseRecyclerAdapter<ChatMessage, chat_rec>adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.RecyclerView);
editText = findViewById(R.id.TEXT);
addBtn = findViewById(R.id.addBtn);
context = getApplicationContext();
recyclerView.setHasFixedSize(true);
final ai.api.android.AIConfiguration configuration = new ai.api.android.AIConfiguration("8c9665a53fba45d9a3015e0ba7330417", AIConfiguration.SupportedLanguages.English,
ai.api.android.AIConfiguration.RecognitionEngine.System);
aiService = AIService.getService(this, configuration);
aiService.setListener(this);
final AIRequest aiRequest = new AIRequest();
final AIDataService aiDataService = new AIDataService(context, configuration);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
ref = FirebaseDatabase.getInstance().getReference();
ref.keepSynced(true);
addBtn.setOnClickListener(new View.OnClickListener() {
@SuppressLint("StaticFieldLeak")
@Override
public void onClick(View v) {
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
String message = editText.getText().toString().trim();
if (!message.equals("")) {
ref = FirebaseDatabase.getInstance().getReference().child("chat").push();
ChatMessage chatMessage = new ChatMessage(message, "user");
ref.child("chat").push().setValue(chatMessage);
ChatMessage model1 = new ChatMessage();
model1.setMessageUser(chatMessage.toString());
new AsyncTask<AIRequest, Void, AIResponse>() {
@Override
protected AIResponse doInBackground(AIRequest... aiRequests) {
final AIRequest request = aiRequests[0];
try {
final AIResponse response = aiDataService.request(aiRequest);
} catch (AIServiceException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(AIResponse response) {
if (response != null) {
Result result = response.getResult();
String reply = result.getFulfillment().getSpeech();
ChatMessage chatMessage = new ChatMessage(reply, "bot");
ref.child("chat").push().setValue(chatMessage);
}
}
}.execute(aiRequest);
} else {
aiService.startListening();
}
editText.setText("");
}
});
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) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
FirebaseRecyclerOptions<ChatMessage>options = new FirebaseRecyclerOptions.Builder<ChatMessage>()
.setQuery(ref, ChatMessage.class)
.build();
adapter = new FirebaseRecyclerAdapter<ChatMessage, chat_rec>(options) {
@Override
protected void onBindViewHolder(@NonNull final chat_rec holder, int i, @NonNull final ChatMessage model) {
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(Objects.requireNonNull(dataSnapshot.child("chat").getValue()).equals( model.getMessageUser())) {
holder.rightText.setText(model.getMessageText());
holder.rightText.setVisibility(View.VISIBLE);
}
else {
holder.leftText.setText(model.getMessageText());
holder.leftText.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@NonNull
@Override
public chat_rec onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.messagelist, parent, false);
return new chat_rec(view);
}
};
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
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onResult(AIResponse result) {
Result result1 = result.getResult();
String message = result1.getResolvedQuery();
ChatMessage chatMessage = new ChatMessage(message, "user");
ref.child("chat").push().setValue(chatMessage);
String reply = result1.getFulfillment().getSpeech();
ChatMessage chatMessage1 = new ChatMessage(reply,"bot");
ref.child("chat").push().setValue(chatMessage1);
}
@Override
public void onError(AIError error) {
}
@Override
public void onAudioLevel(float level) {
}
@Override
public void onListeningStarted() {
}
@Override
public void onListeningCanceled() {
}
@Override
public void onListeningFinished() {
}
}
有人可以让我知道我应该做哪些更改,以便我可以使它正常工作。
最佳答案
我找到了这个问题的替代解决方案。而不是填充 RecyclerView。可以创建两种不同的 xml 布局 - 机器人布局和用户布局。
机器人消息布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:paddingBottom="0dp">
<LinearLayout
android:id="@+id/botMsgLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:background="@drawable/bot_bg_bubble"
android:gravity="start|center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/chatMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:fontFamily="@font/adamina"
android:lineSpacingExtra="1sp"
android:padding="12dp"
android:text="Hello There!"
android:textAlignment="viewStart"
android:textAllCaps="false"
android:textSize="18sp"
android:typeface="normal" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
</FrameLayout>
</LinearLayout>
</FrameLayout>
用户消息布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<LinearLayout
android:id="@+id/userMsgLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="8dp"
android:background="@drawable/user_bg_bubble"
android:gravity="end|center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/chatMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:padding="12dp"
android:text="Hello!"
android:textSize="18sp" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
</FrameLayout>
</LinearLayout>
</FrameLayout>
现在,在 MainActivity 中根据需要使用 switch case 来扩充上述布局。
关于java - 无法使用来自用户和 DialogFlow(api.ai) 的对话填充我的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53980050/
更新:下面提到的stackoverflow给出了另一种解决方案,即导入json并用正确格式的数据替换文本。我现在试图看看如何适应给定的格式,它看起来像这样: { "text" : "use
有谁知道三者之间的具体区别和功能,或者作为开发人员是否有更多功能/更灵活地使用? 最佳答案 wit.ai 与 Dialogflow 与 luis.ai ╔═══════════════════════
我知道 Wit.ai 引擎可以通过手动验证案例来训练,但是有没有办法用一组定义的输入和输出来训练它? 最佳答案 您可能可以查看其中一个应用程序的导出格式并对其进行调整以导入新应用程序。 https:/
来自 wit.ai 团队的一些人请回答这个 我们计划将 wit.ai 用于商业目的。有使用政策吗?请提供一些注意事项指南。此外,这项服务将来是否也是免费的,还是您计划推出企业版? 主要是 API 命中
我需要添加一个确认实体,以便在对话框流 (api.ai) 中的某个操作的参数中得到"is"或“取消”。假设用户正在购买咖啡,我会询问有关咖啡和数量的详细信息,最后我需要确认,我应该向哪个实体申请?任何
谁能帮我解决上面的问题。我们必须在数组 (a1,a2),(a1,a3),(a1,a4).... 等中找到元素的组合,然后选择满足条件 (ai*aj) <= max 的组合(A) 其中 A 是数组并返回
我正在尝试训练我的 Wit.ai 机器人以识别某人的名字。我不太确定我是否完全理解 NLP 的工作原理,所以我会给你一个例子。 我定义了很多表达,比如“我的名字是XXXX”、“大家都叫我XXXX” 在
我想知道是否存在一个网站,人们可以上传他们的 AI 在不同的棋盘游戏中相互竞争:国际象棋、五子棋等。 该站点将接受程序的源代码(以某种通用语言编写)、对其进行编译并相互运行程序。所有程序都必须使用一些
大家好,我是程序员幽鬼。 你想为后代开发一个令人难以置信的应用程序,你想到的第一件事——人工智能!还有什么比模仿人类智能的机器更令人着迷的呢?如果你期待打破刻板印象并准备推出出色的 AI 应用
我在 Application Insights Analytics 中创建了几个查询来获取我想要的图表。 示例: customEvents | where timestamp >= ago(31d)
我似乎无法让这个请求生效: https://wit.ai/docs/http/20160526#delete--entities-:entity-id-values-link 我已经设置了一个值为“C
我正在尝试在 wit.ai 中编写示例应用程序。我使用在 https://wit.ai/docs/quickstart 中显示的 node.js 客户端跟随快速启动应用程序。 .那里显示的示例只有一个
在 api.i(dialogflow) 中,我想获取用户的输入,如姓名、年龄、邮件等,并使用 PHP 将它们存储在我的 MYSQL 数据库中。 如何在对话流中生成我的代理的公共(public) API
我目前正在与 Wit.ai 合作 webpage in CodePen.io .我想知道是否可以使用 HTTP API 检索 Wit.ai 机器人的文本响应(“Bot says”)。 例如:如果用户要
我接到了一项任务,要编写一个由人类玩家和 AI 玩家组成的 NIM 游戏。游戏是“Misere”(最后一个必须拿起一根棍子的人输了)。 AI 应该使用 Minimax 算法,但它正在采取使其输得更快的
为了通过 shell 从端点取消部署模型,我必须指定 deployed-model-id如 gcloud ai endpoints undeploy-model 中所述 我如何获得这个已部署的模型 I
我在谷歌的 Vertex AI 中运行自定义训练作业。执行自定义作业的简单 gcloud 命令将使用类似于以下语法的内容(可以查看命令的完整文档 here ): gcloud beta ai cust
Wit AI project 在他们的 Converse 功能中添加了一个名为 Story 的新概念。有没有办法通过 HTTP API 管理(创建/编辑/验证)这些 Wit AI 故事? 最佳答案 W
我想为 Google 智能助理部署一个应用。但是,我想使用不同的 AI 后端而不是 api.ai。 有人知道这是否可能吗?如何? 或者如果我想使用 Google 智能助理,我会被 api.ai 困住吗
我的项目有一个依赖项,需要 python v3.6+。因此,它会在通过 pip 在 python 3 内核中安装时抛出错误,因为 AI Platform Notebooks 默认附带 v3.5。如何使
我是一名优秀的程序员,十分优秀!