ListView实现点赞功能并用okHttp更新点赞数量
程序员文章站
2022-05-25 19:58:22
...
主类:
1,继承OnItemClickListener
2,listview实现监听器:
listView.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(attention_main.this, "listview的item被点击了!,点击的位置是-->" + position,
Toast.LENGTH_SHORT).show();
}
3,
自定义适配器:
1,继承BaseAdapter
2,重写方法:
//私有成员
private LinkedList<Attention_listBean> aData;
private Context mContext;
//构造器
public Attention_listAdapter(LinkedList<Attention_listBean> aData, Context mContext) {
this.aData = aData;
this.mContext = mContext;
}
//重写的方法
@Override
public int getCount() {
return aData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//设置item项的值
ViewHolder holder = null;
if (convertView == null) {
//获取item项的组件
convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_attention_main, parent, false);
holder = new ViewHolder();
holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
holder.user_pic = (ImageView) convertView.findViewById(R.id.user_pic);
holder.create_time = (TextView) convertView.findViewById(R.id.create_time);
holder.content_text = (TextView) convertView.findViewById(R.id.content_text);
holder.place_name = (TextView) convertView.findViewById(R.id.place_name);
holder.comment_num = (TextView) convertView.findViewById(R.id.comment_num);
holder.like_num = (TextView) convertView.findViewById(R.id.like_num);
holder.like = (ImageButton)convertView.findViewById(R.id.like);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//设置item项组件的值
holder.user_name.setText(aData.get(position).getUser_name());
Glide.with(mContext).load(aData.get(position).getUser_pic()).into(holder.user_pic);
holder.create_time.setText(aData.get(position).getCreate_time());
holder.content_text.setText(aData.get(position).getContent_text());
holder.place_name.setText(aData.get(position).getPlace_name());
holder.comment_num.setText(aData.get(position).getComment_num());
holder.like_num.setText(aData.get(position).getLike_num());
holder.like.setOnClickListener(new MyClickListener(position,convertView));//点赞实现监听器类
holder.like.setTag(position);
holder.like_num.setTag(position);
return convertView;
}
//定义ViewHolder类
static class ViewHolder{
TextView user_name;
ImageView user_pic;
TextView create_time;
TextView content_text;
TextView place_name;
TextView comment_num;
TextView like_num;
ImageButton like;
}
//实现监听器类
public class MyClickListener implements OnClickListener {
private int position;
private View convertView;
ImageButton like;
TextView likes_num;
//改造方法,获取position和convertView
public MyClickListener(int position,View convertView) {
this.position = position;
this.convertView = convertView;
}
/**
* 基类的onClick方法
*/
@Override
public void onClick(View v) {
//通过convertView获取组件id
likes_num =(TextView)convertView.findViewById(R.id.like_num);
like = (ImageButton)v;
//通过position获取组件值
note_id = aData.get(position).getNotes_id();
//使用handler实现消息传递,由于OkHttp回调不能在子线程更新UI
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
String like = msg.getData().getString("like");
likes_num.setText(like);
break;
case 2:
String not_like = msg.getData().getString("not_like");
likes_num.setText(not_like);
break;
}
}
};
if(v.getId() == like.getId()) {
if (aData.get(position).getLike_TF().equals("0")) { //判断是否点击,0为未点击
like.setBackground(convertView.getResources().getDrawable(R.drawable.choose_like));//更新点赞图片
//使用Okhttp获取点赞数量,消息传递到handler
OkHttpClient okHttpClient = new OkHttpClient();
//2.创建Request对象,设置一个url地址,设置请求方式。
Request request = new Request.Builder()
.url("http://192.168.43.243:9000/like/" + note_id)
.get()
.build();
//3.创建一个call对象,参数就是Request请求对象
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String err = e.getMessage().toString();
System.out.println("----------------------------sbsbsbs+err" + err);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String rtn = response.body().string();
System.out.println("----------------------------sbsbsbs+success" + rtn);
Message message = new Message();
message.what = 1;
//携带参数
Bundle bundle = new Bundle();
bundle.putString("like",rtn);
message.setData(bundle);
handler.sendMessage(message);
}
});
aData.get(position).setLike_TF("1");
}else {
like.setBackground(convertView.getResources().getDrawable(R.drawable.dz));
OkHttpClient okHttpClient = new OkHttpClient();
//2.创建Request对象,设置一个url地址,设置请求方式。
Request request = new Request.Builder()
.url("http://192.168.43.243:9000/dont_like/" + note_id)
.get()
.build();
//3.创建一个call对象,参数就是Request请求对象
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
String err = e.getMessage().toString();
System.out.println("----------------------------sbsbsbs+err" + err);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String rtn = response.body().string();
System.out.println("----------------------------sbsbsbs+success" + rtn);
Message message = new Message();
message.what = 2;
Bundle bundle = new Bundle();
bundle.putString("not_like",rtn);
message.setData(bundle);
handler.sendMessage(message);
}
});
aData.get(position).setLike_TF("0");
}
}
}
}
上一篇: ListView数据不显示?我教你