好库网首页 | 我的好库
好库网 好库网社区
IT社区 » 其它板块 » 手机开发(Android/iPhone/WP8) » 智能机器人的快速实现
回复 发帖

查看:940    回复:0 智能机器人的快速实现
访问uoou123的空间
发表于 2015/5/26 14:35:41
1楼

http://club.okbase.net/upload/club/2015/05/26/A143513281-7.rar 借助图灵机器人提供的API可快速实现一个智能机器人,让你的APP更聪明智能,下面是部分代码片段,可下载附件中的源码

1. ChatMessageAdapter

package com.example.android_robot_01;
 
import java.util.List;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
import com.example.android_robot_01.bean.ChatMessage;
import com.example.android_robot_01.bean.ChatMessage.Type;
 
public class ChatMessageAdapter extends BaseAdapter
{
    private LayoutInflater mInflater;
    private List<ChatMessage> mDatas;
 
    public ChatMessageAdapter(Context context, List<ChatMessage> datas)
    {
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }
 
    @Override
    public int getCount()
    {
        return mDatas.size();
    }
 
    @Override
    public Object getItem(int position)
    {
        return mDatas.get(position);
    }
 
    @Override
    public long getItemId(int position)
    {
        return position;
    }
 
    /**
     * 接受到消息为1,发送消息为0
     */
    @Override
    public int getItemViewType(int position)
    {
        ChatMessage msg = mDatas.get(position);
        return msg.getType() == Type.INPUT ? 1 : 0;
    }
 
    @Override
    public int getViewTypeCount()
    {
        return 2;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ChatMessage chatMessage = mDatas.get(position);
 
        ViewHolder viewHolder = null;
 
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            if (chatMessage.getType() == Type.INPUT)
            {
                convertView = mInflater.inflate(R.layout.main_chat_from_msg,
                        parent, false);
                viewHolder.createDate = (TextView) convertView
                        .findViewById(R.id.chat_from_createDate);
                viewHolder.content = (TextView) convertView
                        .findViewById(R.id.chat_from_content);
                convertView.setTag(viewHolder);
            } else
            {
                convertView = mInflater.inflate(R.layout.main_chat_send_msg,
                        null);
 
                viewHolder.createDate = (TextView) convertView
                        .findViewById(R.id.chat_send_createDate);
                viewHolder.content = (TextView) convertView
                        .findViewById(R.id.chat_send_content);
                convertView.setTag(viewHolder);
            }
 
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
 
        viewHolder.content.setText(chatMessage.getMsg());
        viewHolder.createDate.setText(chatMessage.getDateStr());
 
        return convertView;
    }
 
    private class ViewHolder
    {
        public TextView createDate;
        public TextView name;
        public TextView content;
    }
 
}
您需要登录后才可以回帖 登录 | 注册
回复 发帖