问答机器人

news/2024/7/8 4:08:48

项目说明:这是一个简单的问答机器人项目,利用不是图灵机器人api,是聚合数据上面的API


首先activity_main布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/wechat_bg"
    android:gravity="center"
    android:orientation="vertical" >


    <ListView
        android:id="@+id/mChatListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:dividerHeight="0dip"
        android:scrollbars="none" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >


        <EditText
            android:id="@+id/et_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="输入" />


        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/button_bg"
            android:text="发送"
            android:textColor="@android:color/white" />
    </LinearLayout>


</LinearLayout>

然后是聊天的左右item布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">


    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/assistant"/>


    <TextView
        android:id="@+id/tv_left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="@drawable/chat_bg_cloud"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="左边"
        android:textColor="@android:color/white"/>


</LinearLayout>

right_item布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right|center_vertical"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tv_right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:background="@drawable/chat_bg_user"
        android:gravity="center_vertical"
        android:padding="20sp"
        android:text="右边"
        android:textColor="@android:color/white"/>
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/user"/>
</LinearLayout>

定一个实体类,这里就两个参数,一个文本和一个类型

public class ChatListData {
private int type;
private String text;


public int getType() {
return type;
}


public void setType(int type) {
this.type = type;
}


public String getText() {
return text;
}


public void setText(String text) {
this.text = text;
}
}

然后就是适配器(这里需要判断类型,然后对类型进行分析):

public class MyAdapter extends BaseAdapter {
private List<ChatListData> datas;
private Context context;
// 左边的type
public static final int VALUE_LEFT_TEXT = 1;
// 右边的type
public static final int VALUE_RIGHT_TEXT = 2;
// 加载布局
private LayoutInflater inflater;


public MyAdapter(Context context, List<ChatListData> datas) {
this.context = context;
this.datas = datas;
inflater = LayoutInflater.from(context);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}


@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
int type = datas.get(position).getType();
return type;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 3;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderLeftText leftHolder = null;
ViewHolderRightText rightHolder = null;
int type = getItemViewType(position);
if (convertView == null) {
if (type == VALUE_LEFT_TEXT) {
convertView = inflater.inflate(R.layout.left_item, null);
leftHolder = new ViewHolderLeftText();
leftHolder.tv_left_text = (TextView) convertView
.findViewById(R.id.tv_left_text);
convertView.setTag(leftHolder);
} else if (type == VALUE_RIGHT_TEXT) {
convertView = inflater.inflate(R.layout.right_item, null);
rightHolder = new ViewHolderRightText();
rightHolder.tv_right_text = (TextView) convertView
.findViewById(R.id.tv_right_text);
convertView.setTag(rightHolder);
}
} else {
switch (type) {
case VALUE_LEFT_TEXT:
leftHolder = (ViewHolderLeftText) convertView.getTag();
break;
case VALUE_RIGHT_TEXT:
rightHolder = (ViewHolderRightText) convertView.getTag();
break;


default:
break;
}
}
switch (type) {
case VALUE_LEFT_TEXT:
leftHolder.tv_left_text.setText(datas.get(position).getText());
break;
case VALUE_RIGHT_TEXT:
rightHolder.tv_right_text.setText(datas.get(position).getText());
break;


default:
break;
}
return convertView;
}


// 左边的文本
class ViewHolderLeftText {
private TextView tv_left_text;
}


// 右边的文本
class ViewHolderRightText {
private TextView tv_right_text;
}
}

最后是MainActvity(这里用的jar包是volley):

public class MainActivity extends Activity implements OnClickListener {
private Button btn_send;
private MyAdapter adapter;
private ListView mChatListView;
private List<ChatListData> datas;
private EditText et_text;
private String url;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_send = (Button) findViewById(R.id.btn_send);
mChatListView = (ListView) findViewById(R.id.mChatListView);
et_text = (EditText) findViewById(R.id.et_text);
datas = new ArrayList<ChatListData>();
btn_send.setOnClickListener(this);
adapter = new MyAdapter(this, datas);
mChatListView.setAdapter(adapter);
mChatListView
.setOnItemLongClickListener(new MyOnItemLongClickListener());
addLeftMessage("大家好,我是小东东,很高心为你服务");
}


// 长按删除
class MyOnItemLongClickListener implements OnItemLongClickListener {


@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
String text = datas.get(position).getText();
datas.remove(position);
adapter.notifyDataSetChanged();
return false;
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_send:
sendMessage();
break;
default:
break;
}
}
// 发送消息
private void sendMessage() {
String text = et_text.getText().toString();
if (text != null) {
url = "http://op.juhe.cn/robot/index?info=+" + text + "&key="
+ Constans.jiq_key;
addRightMessage(text);
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(url,
new Listener<String>() {


@Override
public void onResponse(String result) {
parsionjson(result);


}


}, new ErrorListener() {


@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub


}
});
queue.add(request);
}
}


// json解析数据
private void parsionjson(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
String json = jsonObject.getString("reason");
Log.e("main", json);
JSONObject object = jsonObject.getJSONObject("result");
String text = object.getString("text");
addLeftMessage(text);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


// 添加右边信息
private void addRightMessage(String text) {
ChatListData data = new ChatListData();
data.setType(MyAdapter.VALUE_RIGHT_TEXT);
data.setText(text);
datas.add(data);
adapter.notifyDataSetChanged();
// mChatListView.setSelection(mChatListView.getBottom());//第一种方式
// mChatListView.setSelection(datas.size()-1);//第二种方式
mChatListView.setSelection(adapter.getCount() - 1);// 第三种方式
}


// 添加左边信息
private void addLeftMessage(String text) {
ChatListData data = new ChatListData();
data.setType(MyAdapter.VALUE_LEFT_TEXT);
data.setText(text);
datas.add(data);
adapter.notifyDataSetChanged();
// mChatListView.setSelection(mChatListView.getBottom());//第一种方式
// mChatListView.setSelection(datas.size()-1);//第二种方式
mChatListView.setSelection(adapter.getCount() - 1);// 第三种方式
}


}


http://www.niftyadmin.cn/n/3649457.html

相关文章

后端接口测试工具推荐——Postman

用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的&#xff0c;用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具。今天给大家介绍的这款网页调试工具不仅可以调试简单的css、html、脚本等简单的网页基本信息&#xff0c…

[sip]SIP多方会话消息之实例讲解 幻灯片

这是我编写的第6个针对SIP的幻灯片&#xff0c;实例讲述邀请多个好友聊天的SIP消息&#xff0c;可用于Team内讲解并演示SIP协议的讲座。本讲义的版权归郑昀所有。允许拷贝、分发和在“GNU Free Documentation License”下的定制。对于关注SIP应用的你&#xff0c;任何的建议和修…

自己搭建Android项目框架必备的框架与第三方应用

闲来无事、想搭个框架试试 分析一般应用 将资料整理整理 粗略统计 需要以下资料 1、android-pulltorefresh 一个强大的拉动刷新开源项目&#xff0c;支持各种控件下拉刷新 ListView、ViewPager、WevView、ExpandableListView、GridView、(Horizontal )ScrollView、Fragment上下…

mongodb 字段检索_如何在MongoDB中创建,检索,更新和删除记录

mongodb 字段检索介绍 (Introduction) MongoDB is a free and open-source NoSQL document database used commonly in modern web applications. In this short tutorial you’ll explore how to work with data in MongoDB. You’ll create, retrieve, update, and delete re…

Java 集成开发环境——Eclipse JEE的安装和配置Tomcat

Eclipse是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言&#xff0c;它只是一个框架和一组服务&#xff0c;用于通过插件组件构建开发环境。幸运的是&#xff0c;Eclipse 附带了一个标准的插件集&#xff0c;包括Java开发工具(Java Development Kit&#xff0c;JD…

Android Binder机制(超级详尽)

1&#xff0e;binder通信概述 binder通信是一种client-server的通信结构&#xff0c; 1.从表面上来看&#xff0c;是client通过获得一个server的代理接口&#xff0c;对server进行直接调用&#xff1b; 2.实际上&#xff0c;代理接口中定义的方法与server中定义的…

instagram架构_使用Facebook,Instagram和LinkedIn进行Django身份验证

instagram架构介绍 (Introduction) Manually signing up for and signing into accounts online can sometimes prove onerous. Modern web applications solve this through social authentication, which is primarily a way to allow users to sign in (and sign-up) with t…

[sip]SIP消息之逐项讲解 幻灯片

这是我编写的第四个针对SIP的幻灯片&#xff0c;实例讲述SIP消息中的各个字段&#xff0c;可用于Team内讲解并演示SIP协议的讲座。本讲义的版权归郑昀所有。允许拷贝、分发和在“GNU Free Documentation License”下的定制。对于关注SIP应用的你&#xff0c;任何的建议和修正都…