package com.example.share2; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import android.widget.ListView; import android.widget.Toast; import com.example.share2.fragment.photo.GetPhotosList; import com.example.share2.fragment.photo.PhotoData; import com.example.share2.fragment.photo.PhotosAdapter; import com.example.share2.fragment.photo.Record; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import okhttp3.Call; import okhttp3.Callback; import okhttp3.Headers; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class MyWorkActivity extends AppCompatActivity { private static ListView lvList; private static List photosList; private static PhotosAdapter photosAdapter; private GetPhotosList getPhotosList; SharedPreferences sp; private static final Handler mHandler = new Handler(Looper.getMainLooper()){ public void handleMessage(Message msg){ ResponseBody>> photoDataResponseBody; switch (msg.what) { case 1: photoDataResponseBody = (ResponseBody>>) msg.obj; PhotoData> photoData = photoDataResponseBody.getData(); if (photoData == null)//防止崩溃 { Toast.makeText(lvList.getContext(), "您还没有发布作评!",Toast.LENGTH_SHORT).show(); break; } photosList = photoData.getRecords(); for(Record record:photosList) photosAdapter.add(record); photosAdapter.notifyDataSetChanged(); break; case 2: int id = msg.arg1; photosAdapter.remove(photosList.get(id)); photosList.remove(id); photosAdapter.notifyDataSetChanged(); Toast.makeText(lvList.getContext(), "删除动态成功",Toast.LENGTH_SHORT).show(); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_work); lvList = findViewById(R.id.lv_photo_MyWork); photosList = new ArrayList(); photosAdapter = new PhotosAdapter(this,R.layout.list_item,photosList,2); lvList.setAdapter(photosAdapter); sp = getSharedPreferences("config", Context.MODE_PRIVATE); getPhotosList = new GetPhotosList(mHandler, Constants.USER_MY_SHARE, sp); getPhotosList.getRemoteList(0); photosAdapter.setOnItemCollect(new PhotosAdapter.OnItemCollectListener() { @Override public void OnCollect(int id, int flag) { DeletcItem(id); } }); photosAdapter.setOnItemComment(new PhotosAdapter.OnItemCommentListener() { @Override public void OnComment(int id, int flag) { Intent intentComment = new Intent(lvList.getContext(), CommentActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("record",photosList.get(id)); intentComment.putExtras(bundle); startActivity(intentComment); } }); } private void DeletcItem(int id) { new AlertDialog.Builder(this).setTitle("信息提示")//设置对话框标题 .setMessage("确定删除此动态吗?") .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加确定按钮 @Override public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件,点击事件没写,自己添加 onDeletcItem(id); } }).setNegativeButton("否", new DialogInterface.OnClickListener() {//添加返回按钮 @Override public void onClick(DialogInterface dialog, int which) {//响应事件,点击事件没写,自己添加 } }).show();//在按键响应事件中显示此对话框 } private void onDeletcItem(int id){ ConnectServer connectServer = new ConnectServer(); Map m = new HashMap<>(); m.put("Content-Type","application/json"); Headers headers = connectServer.getHeaders(m); Map body_map = new HashMap<>(); body_map.put("shareId",photosList.get(id).getCollectId()); RequestBody r_body = connectServer.getJsonBody(body_map); HashMap bodyParams = new HashMap<>(); bodyParams.put("shareId",photosList.get(id).getId()); String userId = sp.getString("id",""); bodyParams.put("userId",userId); String url = Constants.SERVER_URL+Constants.USER_SHARE_DELETE+connectServer.getBodyParams(bodyParams); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).headers(headers).post(r_body).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d("删除动态失败","aaaaaa"); } @Override public void onResponse(Call call, Response response) throws IOException { final String Rbody = response.body().string(); Log.d("删除动态成功","aaaaaa"); //取消点赞之后更新列表 Message message = new Message(); message.what = 2; message.arg1 = id; mHandler.sendMessage(message); } }); } }