Main_xml
https://www.cnblogs.com/yunfang/p/5553629.html
···
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.robot.MainActivity">
···
定义两个item 一个是自己一个是机器人
自己:
···
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
···
机器人:
代码如下:
MainActivity:
···
package com.example.robot;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
EditText et;
Button bt;
ListView listView;
ArrayList
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
bt= (Button) findViewById(R.id.button);
et= (EditText) findViewById(R.id.editText);
listView= (ListView) findViewById(R.id.list_item);
//发送
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendData();
}
});
adapter= new MyAdapter(MainActivity.this,arrayList);
listView.setAdapter(adapter);
}
public void sendData(){
final String content = et.getText().toString().trim();
if (content==null || content.equals("")){
Toast.makeText(MainActivity.this,"发送内容不能为空",Toast.LENGTH_SHORT).show();
}else{
//不管用户是否联网 ,只要输入框不为空,就把内容刷新到lisetView上
HashMap
map.put("data",content);
map.put("label","person");
arrayList.add(map);
adapter.upDataAdapter(arrayList);
listView.setSelection(arrayList.size()-1);
listView.setStackFromBottom(true);
et.setText("");
new Thread(){
@Override
public void run() {
try {
URL url = new URL("http://op.juhe.cn/robot/index?info="
+ URLEncoder.encode(content,"UTF-8")
+"&dtype=&loc=&userid=&key=761522f85f516959958a310457c29735");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
if (conn.getResponseCode()==200){
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte buffer[]=new byte[1024];
int leagth=-1;
while ((leagth=is.read(buffer))!=-1){
bos.write(buffer,0,leagth);
bos.flush();
}
is.close();
bos.close();
JSONObject jsonObject = new JSONObject(bos.toString());
JSONObject jsonObject1 = jsonObject.getJSONObject("result");
String data = jsonObject1.getString("text");
HashMap map = new HashMap<>();
map.put("data",data);
map.put("label","robot");
arrayList.add(map);
runOnUiThread(new Thread(){
@Override
public void run() {
adapter.upDataAdapter(arrayList);
listView.setSelection(arrayList.size()-1);
listView.setStackFromBottom(true);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
}
···
自定义的适配器:
···
package com.example.robot;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
- Created by lenovo on 2018/7/12.
*/
public class MyAdapter extends BaseAdapter{
Context context;
ArrayList
public MyAdapter( Context context,ArrayList> arrayList){
this.context=context;
this.arrayList=arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int i) {
return arrayList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (arrayList.get(i).get("label").equals("person")){
view=View.inflate(context,R.layout.person,null);
TextView tvPerson = view.findViewById(R.id.text_person);
tvPerson.setText(arrayList.get(i).get("data"));
}else{
view=View.inflate(context,R.layout.robot,null);
TextView tvRobot = view.findViewById(R.id.text_robot);
tvRobot.setText(arrayList.get(i).get("data"));
}
return view;
}
public void upDataAdapter(ArrayList> arrayList){
this.arrayList = arrayList;
notifyDataSetChanged();
}
}
···