欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第五节总结

程序员文章站 2022-06-21 18:54:41
...

总结

这一节主要实现聊天界面与自动机器人之间的对话。使用ListView实现,根据不同的type决定显示左或者右的聊天界面

1.实现

实现BaseAdapter

public class ChartListAdaoter extends BaseAdapter {
    private Context mContext;
    private List<ChartLsitData> chartLsitDatas;
    private LayoutInflater inflater;
    private ChartLsitData data;
//定义type
    private static final int left_type=1;
    private static final int right_type=2;

    public ChartListAdaoter(Context mContext,List<ChartLsitData> chartLsitDatas ) {
        this.chartLsitDatas=chartLsitDatas;
        inflater=LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return chartLsitDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return chartLsitDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        leftViewHolder leftViewHolder = null;
        rightViewHolder rightViewHolder = null;
        if (convertView == null) {
            switch (type) {
                case left_type:
                    leftViewHolder = new leftViewHolder();
                    convertView = inflater.inflate(R.layout.left, null);
                    leftViewHolder.textView_left = (TextView) convertView.findViewById(R.id.left_tv);
                    convertView.setTag(leftViewHolder);
                    break;
                case right_type:
                    rightViewHolder = new rightViewHolder();
                    convertView = inflater.inflate(R.layout.right, null);
                    rightViewHolder.textView_right = (TextView) convertView.findViewById(R.id.right_tv);
                    convertView.setTag(rightViewHolder);
                    break;
            }
        } else {
            switch (type) {
                case left_type:
                    leftViewHolder = (leftViewHolder) convertView.getTag();
                    break;
                case right_type:
                    rightViewHolder = (rightViewHolder) convertView.getTag();
                    break;
            }

        }

        switch (type) {
            case left_type:
                leftViewHolder.textView_left.setText(data.getText());
                break;
            case right_type:
                rightViewHolder.textView_right.setText(data.getText());
                break;
        }

        return convertView;
    }
   //获取type
    @Override
    public int getItemViewType(int position) {
         data=chartLsitDatas.get(position);
        int view_type=data.getType();
        return  view_type;
    }

    @Override
    public int getViewTypeCount() {
        //不知道为什么
        return 3;
    }
}
class leftViewHolder{

    public TextView textView_left;
}
class rightViewHolder{
    public TextView textView_right;
}

ChartLsitData 类

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

    public ChartLsitData(int type, String text) {
        this.type = type;
        this.text = 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 ButlreFragment extends Fragment implements View.OnClickListener{

    private ListView chartliseView;
    private ChartListAdaoter adapter;
    private EditText message_et;
    private Button Send_bu;
    private ChartLsitData chartLsitData;
    private List<ChartLsitData> chartLsitDatas;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.butlerlayout, container, false);
        chartLsitDatas=new ArrayList<>();
        findView(view);
//刚进入时显示的
        chartLsitData=new ChartLsitData(1,"欢迎");
        chartLsitDatas.add(chartLsitData);
        //通知adapter刷新
        adapter.notifyDataSetChanged();
        return view;

    }

    public void findView(View view) {
        chartliseView= (ListView) view.findViewById(R.id.chartliseView);
        message_et= (EditText) view.findViewById(R.id.message_et);
        Send_bu= (Button) view.findViewById(R.id.Send_bu);
        Send_bu.setOnClickListener(this);
         adapter=new ChartListAdaoter(getActivity(),chartLsitDatas);
        chartliseView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Send_bu:
                String str=message_et.getText().toString();

                if (!TextUtils.isEmpty(str)){
                    chartLsitData=new ChartLsitData(2,str);
                    chartLsitDatas.add(chartLsitData);
                    //通知adapter刷新
                    adapter.notifyDataSetChanged();
                    //滚动到底部
                    chartliseView.setSelection(chartliseView.getBottom());
                    String URL="http://apis.juhe.cn/mobile/get?phone="+str+"&key=7fbf0ce17daa9a2ca31626fa7c97407b";
                    RxVolley.get(URL, new HttpCallback() {
                        @Override
                        public void onSuccess(String t) {
                            L.i(t.toString());
                            prasingJSON(t);
                            super.onSuccess(t);
                        }
                    });

                }else {
                    Toast.makeText(getActivity(),"你是真的懒",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    private void prasingJSON(String t) {
        try {
             JSONObject jsonObject=new JSONObject(t);
            int resultCode=jsonObject.getInt("resultcode");
            if (resultCode==200) {
                JSONObject jsonResult = jsonObject.getJSONObject("result");
                String name= jsonResult.getString("province");
                chartLsitData=new ChartLsitData(1,name);
                chartLsitDatas.add(chartLsitData);
                //通知adapter刷新
                adapter.notifyDataSetChanged();
                //滚动到底部
                chartliseView.setSelection(chartliseView.getBottom());
            }else {
                Toast.makeText(getActivity(),"错误",Toast.LENGTH_LONG).show();
            }

            message_et.setText("");

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

布局

第五节总结第五节总结