66import android .content .DialogInterface ;
77import android .content .Intent ;
88import android .content .SharedPreferences ;
9- import android .os .AsyncTask ;
109import android .support .annotation .NonNull ;
1110import android .support .v4 .content .LocalBroadcastManager ;
1211import android .support .v7 .app .AlertDialog ;
2019import com .google .gson .JsonObject ;
2120import com .google .gson .JsonParser ;
2221
22+ import java .io .IOException ;
2323import java .util .List ;
24+ import java .util .Objects ;
25+
26+ import okhttp3 .Call ;
27+ import okhttp3 .Callback ;
28+ import okhttp3 .OkHttpClient ;
29+ import okhttp3 .Request ;
30+ import okhttp3 .RequestBody ;
31+ import okhttp3 .Response ;
2432
2533public class RecyclerViewAdapter extends RecyclerView .Adapter <RecyclerViewAdapter .NewsViewHolder > {
2634
27- private static final String MY_BROADCAST_TAG = "com.reallct.qwe7002.smartblog_client" ;
2835 static SharedPreferences sharedPreferences ;
2936 private List <Post_List_Serialzable > post_list_serialzables ;
3037 private Context context ;
3138
32- public RecyclerViewAdapter (List <Post_List_Serialzable > post_list_serialzables , Context context ) {
39+ RecyclerViewAdapter (List <Post_List_Serialzable > post_list_serialzables , Context context ) {
3340 this .post_list_serialzables = post_list_serialzables ;
3441 this .context = context ;
35-
3642 }
3743
3844 @ NonNull
@@ -69,9 +75,46 @@ public void onClick(DialogInterface dialogInterface, int i) {
6975 new DialogInterface .OnClickListener () {
7076 @ Override
7177 public void onClick (DialogInterface dialogInterface , int i ) {
78+ final ProgressDialog mpDialog = new ProgressDialog (context );
79+ mpDialog .setProgressStyle (ProgressDialog .STYLE_SPINNER );
80+ mpDialog .setTitle (context .getString (R .string .loading ));
81+ mpDialog .setMessage (context .getString (R .string .loading_message ));
82+ mpDialog .setIndeterminate (false );
83+ mpDialog .setCancelable (false );
84+ mpDialog .show ();
7285 JsonObject postobj = public_value .post_list .get (position ).getAsJsonObject ();
7386 String title = postobj .get ("title" ).getAsString ();
74- new delete_post ().execute (Integer .toString (position ), title );
87+ RequestBody body = RequestBody .create (public_value .JSON , "{\" post_id\" :" + Integer .toString (position ) + ",\" sign\" :\" " + public_func .getMD5 (Integer .toString (position ) + title + public_value .password ) + "\" }" );
88+ Request request = new Request .Builder ().url ("https://" + public_value .host + "/control/delete" ).method ("POST" , body ).build ();
89+ OkHttpClient okHttpClient = new OkHttpClient ();
90+ Call call = okHttpClient .newCall (request );
91+ call .enqueue (new Callback () {
92+ @ Override
93+ public void onFailure (Call call , IOException e ) {
94+ mpDialog .cancel ();
95+ Intent intent = new Intent ();
96+ intent .putExtra ("result" , context .getString (R .string .submit_error ));
97+ intent .putExtra ("success" , false );
98+ intent .setAction (public_value .MY_BROADCAST_TAG );
99+ LocalBroadcastManager .getInstance (context ).sendBroadcast (intent );
100+ }
101+
102+ @ Override
103+ public void onResponse (Call call , Response response ) throws IOException {
104+ mpDialog .cancel ();
105+ JsonParser parser = new JsonParser ();
106+ final JsonObject objects = parser .parse (Objects .requireNonNull (response .body ()).string ()).getAsJsonObject ();
107+ String result_message = context .getString (R .string .submit_error );
108+ if (objects .get ("status" ).getAsBoolean ()) {
109+ result_message = context .getString (R .string .submit_success );
110+ }
111+ Intent intent = new Intent ();
112+ intent .putExtra ("result" , result_message );
113+ intent .putExtra ("success" , true );
114+ intent .setAction (public_value .MY_BROADCAST_TAG );
115+ LocalBroadcastManager .getInstance (context ).sendBroadcast (intent );
116+ }
117+ });
75118 }
76119 }).setNegativeButton (R .string .cancel , null ).show ();
77120 break ;
@@ -88,56 +131,16 @@ public int getItemCount() {
88131 return post_list_serialzables .size ();
89132 }
90133
91- //自定义ViewHolder类
92134 static class NewsViewHolder extends RecyclerView .ViewHolder {
93135
94136 CardView cardView = (CardView ) itemView .findViewById (R .id .card_view );
95137 TextView title = (TextView ) itemView .findViewById (R .id .title );
96138 TextView excerpt = (TextView ) itemView .findViewById (R .id .excerpt );
97139
98- public NewsViewHolder (final View itemView ) {
140+ NewsViewHolder (final View itemView ) {
99141 super (itemView );
100- //设置TextView背景为半透明
101- //news_title.setBackgroundColor(Color.argb(20, 0, 0, 0));
102142 }
103143
104144
105145 }
106-
107- @ SuppressLint ("StaticFieldLeak" )
108- private class delete_post extends AsyncTask <String , Integer , String > {
109- ProgressDialog mpDialog = new ProgressDialog (context );
110-
111- @ Override
112- protected void onPreExecute () {
113- mpDialog .setProgressStyle (ProgressDialog .STYLE_SPINNER );
114- mpDialog .setTitle (context .getString (R .string .loading ));
115- mpDialog .setMessage (context .getString (R .string .loading_message ));
116- mpDialog .setIndeterminate (false );
117- mpDialog .setCancelable (false );
118- mpDialog .show ();
119- }
120-
121- @ Override
122- protected String doInBackground (String ... args ) {
123-
124- return request .send_request ("{\" post_id\" :" + args [0 ] + ",\" sign\" :\" " + request .getMD5 (args [0 ] + args [1 ] + public_value .password ) + "\" }" , "delete" );
125- }
126-
127- @ Override
128- protected void onPostExecute (String result ) {
129- mpDialog .cancel ();
130- JsonParser parser = new JsonParser ();
131- final JsonObject objects = parser .parse (result ).getAsJsonObject ();
132- String result_message = context .getString (R .string .submit_error );
133- if (objects .get ("status" ).getAsBoolean ()) {
134- result_message = context .getString (R .string .submit_success );
135- }
136- Intent intent = new Intent ();
137- intent .putExtra ("result" , result_message );
138- intent .putExtra ("success" , true );
139- intent .setAction (MY_BROADCAST_TAG );
140- LocalBroadcastManager .getInstance (context ).sendBroadcast (intent );
141- }
142- }
143146}
0 commit comments