Android_基于BMOB的聊天软件_添加好友

之前提过为什么要用BMOB,前段时间完成了用户的注册与登录,现在做添加好友功能,主要

参考BMOB官网

提供的资料。
主要流程就是,输入好友关键字,点击搜索,显示服务器上对应的搜索结果,用BMOB的好处就是不用自己建数据库啊,省了一个做前端还要处理后端的问题。
搜索代码如下:

private void serachFriend() {
        if(TextUtils.isEmpty(et_search.getText().toString())){
            Toast.makeText(getApplicationContext(), "查找内容不能为空哦。", Toast.LENGTH_LONG).show();
            return;
        }
        String searchName = et_search.getText().toString();
        progress = new ProgressDialog(AddFrindActivity.this);
        progress.setMessage("正在搜索...");
        progress.setCanceledOnTouchOutside(true);
        progress.show();
        searchService(searchName,progress);
    }
    private void searchService(String searchName, final ProgressDialog progress2) {
        // TODO Auto-generated method stub
        BmobQuery<MyUser> query = new BmobQuery<MyUser>();
        query.addWhereContains("username", searchName);

        query.findObjects(getApplicationContext(), new FindListener<MyUser>() {

            @Override
            public void onSuccess(List<MyUser> arg0) {
                progress2.dismiss();
//              Toast.makeText(getApplicationContext(), arg0.size()+arg0.toString(), Toast.LENGTH_LONG).show();
                System.out.println(arg0.size()+arg0.toString());
                if(adapter == null){
                    adapter = new AddFriendAdapter(getApplicationContext(), arg0);
                    lv_search_friend.setAdapter(adapter);
                }
                else{
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int arg0, String arg1) {
                progress2.dismiss();
                Toast.makeText(getApplicationContext(), "查询失败:" + arg0 + "  , "+arg1, Toast.LENGTH_LONG).show();
            }
        });
    }

为显示搜索结果,采用ListView显示,为该ListView创建基于BaseAdapter的适配器,代码如下:

package com.ppl.get_loc.chat;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.image.SmartImageView;
import com.ppl.get_loc.R;

public class AddFriendAdapter extends BaseAdapter {

    private Context mContext;
    private List<MyUser>data;
    private LayoutInflater layoutInflater;

    public AddFriendAdapter(Context mContext, List<MyUser> data) {
        super();
        this.mContext = mContext;
        this.data = data;
        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if(convertView == null){
            holder = new viewHolder();
            convertView = layoutInflater.inflate(R.layout.item_search_friend, null);
            holder.bt = (Button) convertView.findViewById(R.id.bt_search_add);
            holder.SIV = (SmartImageView) convertView.findViewById(R.id.SIV_search_avatar);
            holder.tv = (TextView) convertView.findViewById(R.id.tv_search_username);
            convertView.setTag(holder);
        }else{
            holder = (viewHolder)convertView.getTag();
        }
        MyUser tmpUserInfo = data.get(position);
        holder.SIV.setImageUrl(tmpUserInfo.getNick_URL_NET());
        holder.tv.setText(tmpUserInfo.getUsername());

        //点击添加按钮
        holder.bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "点击了添加按钮", Toast.LENGTH_LONG).show();
            }
        });
        return convertView;
    }
    private class viewHolder{
        private SmartImageView  SIV;
        private TextView tv;
        private Button bt;
    }
}

其中要用到ImageView显示一个网址提供的图片,所以用了之前在github上下载的一个库com.loopj.android.image 它提供了该功能,效果如下:
这里写图片描述这里写图片描述
接下来就是 处理用户关系啦!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值