之前提过为什么要用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 它提供了该功能,效果如下:
接下来就是 处理用户关系啦!