已更新
我正在使用排球库创建一个简单的聊天机器人。我已经在具有3列ID,问题,答案的MySQL数据库中创建了表。相同的PHP代码运行正常,并已通过POSTMAN进行了测试。
我无法并排显示消息。
我尝试创建2 Adapter类,但是它不起作用。我试图通过使用if else将两个适配器文件的代码放入一个。
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText que;
ImageButton btn;
ListView messageView;
ArrayList<Message> arrayList;
MessageAdapterUser adp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
que = findViewById(R.id.editText);
btn = findViewById(R.id.btn);
messageView = findViewById(R.id.messages_view);
arrayList = new ArrayList();
adp = new MessageAdapterUser(MainActivity.this, arrayList);
messageView.setAdapter(adp);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList.add(new Message("user",que.getText().toString()));
adp.notifyDataSetChanged();
que.getText().clear();
CallAPiQueAns();
}
});
}
private void CallAPiQueAns() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest=new StringRequest(Request.Method.POST,
"http://www.cstech.poornima.org/project/android/que_ans.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// Toast.makeText(MainActivity.this, response+"", Toast.LENGTH_SHORT).show();
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("success").equals("true")){
// JSONObject jsonObject1 = jsonObject.getJSONObject("data");
// String ans = jsonObject.getString("data");
JSONArray jsonArray = jsonObject.getJSONArray("data");
// Toast.makeText(MainActivity.this, ans, Toast.LENGTH_SHORT).show();
arrayList.add(new Message("bot",jsonArray.getString(0)));
// MessageAdapterUser adp = new MessageAdapterUser(MainActivity.this, arrayList);
messageView.setAdapter(adp);
// adp.notifyDataSetChanged();
}
else {
Toast.makeText(MainActivity.this, "Try Again", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(MainActivity.this, ""+error, Toast.LENGTH_SHORT).show();
}
}){
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("que",que.getText().toString());
return params;
}
};
requestQueue.add(stringRequest);
}
}
MessageAdapterUser.java
public class MessageAdapterUser extends BaseAdapter {
List<Message> messages = new ArrayList<Message>();
Context context;
LayoutInflater inflater;
public MessageAdapterUser(Context context, List<Message> messages) {
this.messages = messages;
this.context = context;
inflater = LayoutInflater.from(context);
notifyDataSetChanged();
}
@Override
public int getCount() {
return messages.size();
}
@Override
public Object getItem(int position) {
return messages.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
// Toast.makeText(context, messages.get(position).getAns()+"", Toast.LENGTH_SHORT).show();
if (convertView == null){ // Initially view is empty
convertView = inflater.inflate(R.layout.message_user, null);
vh = new ViewHolder(convertView);
convertView.setTag(vh);
}else{
vh = (ViewHolder)convertView.getTag();
}
if (messages.get(position).getType().equals("user")) {
vh.user_message.setText(messages.get(position).getAns());
}else if (messages.get(position).getType().equals("bot")){
vh.bot_message.setText(messages.get(position).getAns());
}
return convertView;
}
class ViewHolder {
TextView user_message;
TextView bot_message;
ViewHolder(View v){
user_message = (TextView) v.findViewById(R.id.user_message);
bot_message = (TextView) v.findViewById(R.id.bot_message);
}
}
}
Message.java
public class Message {
private String ans="";
private String type = "";
public Message(String type,String ans) {
this.ans = ans;
this.type = type;
}
public String getType() {return type;}
public String getAns() {return ans; }
public void setAns(String ans) { this.ans = ans; }
}
MainActivity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:focusableInTouchMode="true"
tools:context=".MainActivity">
<ListView
android:id="@+id/messages_view"
android:layout_width="match_parent"
android:layout_weight="2"
android:divider="#fff"
android:layout_height="wrap_content"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="Write a message"
android:inputType="text"
android:paddingHorizontal="10dp"
android:text="" />
<ImageButton
android:id="@+id/btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:layout_marginHorizontal="10dp"
android:background="@drawable/ic_send_black"
/>
</LinearLayout>
</LinearLayout>
message_user.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp"
android:paddingRight="15dp"
android:paddingLeft="60dp"
android:clipToPadding="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/user_message"
android:background="@drawable/my_message"
android:textColor="#fff"
android:padding="10dp"
android:elevation="2dp"
android:textSize="18dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:layout_below="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp"
android:paddingLeft="15dp"
android:paddingRight="60dp"
android:clipToPadding="false"
>
<View
android:id="@+id/avatar"
android:layout_alignParentLeft="true"
android:scaleType="centerInside"
android:background="@drawable/circle"
android:layout_width="34dp"
android:layout_height="34dp" />
<TextView
android:id="@+id/name"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/avatar"
android:layout_alignTop="@+id/avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:text="Chat Bot"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bot_message"
android:layout_below="@+id/name"
android:layout_alignLeft="@+id/name"
android:background="@drawable/bot_message"
android:paddingVertical="12dp"
android:paddingHorizontal="16dp"
android:elevation="2dp"
android:textSize="18dp"
/>
</RelativeLayout>
</RelativeLayout>
查看图片以供参考
这是屏幕截图。当我输入“你好吗”时,它就会给我输出。
答案 0 :(得分:0)
MessageAdapterUser.java中的getView函数将视图返回为null,因为如果消息类型等于'user',则您将进行两次比较;在第二个中,如果类型是“ bot”,则必须进行比较:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (messages.get(position).getType().equals("user")) {
...
}else if (messages.get(position).getType().equals(**"user"**)){ <--- HERE change it to "bot"
...
}
return convertView;
}