45、微信-从通讯录添加好友

页面布局仍然使用通用的 activity_listview ,通过网络获取联系人:
@Override
protected void initView() {
getLoadingDialog("正在获取联系人").show();
String str_contact = Utils.getValue(this, Constants.ContactMsg);
RequestParams params = new RequestParams();
params.put("userlist", str_contact);
netClient.post(Constants.getContactFriendURL, params, new BaseJsonRes() {
@Override
public void onMySuccess(String data) {
List UserInfos = JSON.parseArray(data, User.class);
mlistview.setAdapter(new FromContactAdapter(AddFromContactActivity.this, UserInfos));
getLoadingDialog("正在获取联系人").dismiss();
}

@Override
public void onMyFailure() {
// TODO Auto-generated method stub
getLoadingDialog("正在获取联系人").dismiss();
}
});
}
适配器也比较简单,使用了和《 42、微信-添加新朋友列表适配器 》中 NewFriendsAdapter 一样的子项布局。
public class FromContactAdapter extends BaseAdapter {
protected Context context;
protected List UserInfos;

public FromContactAdapter(Context ctx, List UserInfos) {
context = ctx;
this.UserInfos = UserInfos;
}

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

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.layout_item_newfriend, parent, false);
}
ImageView img_avar = ViewHolder.get(convertView, R.id.img_photo);
TextView txt_name = ViewHolder.get(convertView, R.id.txt_name);
TextView txt_msg = ViewHolder.get(convertView, R.id.txt_msg);
final TextView txt_add = ViewHolder.get(convertView, R.id.txt_add);
final User user = UserInfos.get(position);
txt_name.setText(user.getUserName());
txt_add.setText("添加");
txt_msg.setText("微信号:" + user.getTelephone() + " "
+ user.getUserName());
txt_add.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

}
});
convertView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(context, FriendMsgActivity.class);
intent.putExtra(Constants.NAME, user.getUserName());
intent.putExtra(Constants.TYPE, ChatActivity.CHATTYPE_SINGLE);
intent.putExtra(Constants.User_ID, user.getTelephone());
context.startActivity(intent);
}
});
return convertView;
}
}

你可能感兴趣的:(45、微信-从通讯录添加好友)