今天主要完成的任务:构造我的笔记界面实现长按删除,点击跳转
遇到的困难:长按会触发点击
解决办法:将长按结果值返回true即可
源程序代码:
package com.itheima.network.fragment; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.itheima.network.R; import com.itheima.network.activity.DetailNoteActivity; import com.itheima.network.adpter.Note_localAdapter; import com.itheima.network.enity.Note_pojo; import com.itheima.network.store.FindUserId; import com.zhy.http.okhttp.OkHttpUtils; import com.zhy.http.okhttp.callback.StringCallback; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import okhttp3.Call; public class Note_local extends Fragment { @BindView(R.id.mynote_list_view) ListView mynoteListView; private Context mContext; private AlertDialog.Builder mDialogBuilder; private List<Note_pojo> notePojoList = new ArrayList<>( ); private static final String url = "http://39.101.190.190:8080/CloudNoteServlet/NoteServlet"; private int userid; private Note_localAdapter note_localAdapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity( ), R.layout.fragment_note_local, null); mContext = view.getContext( ); ButterKnife.bind(this, view); mDialogBuilder = new AlertDialog.Builder(mContext); FindUserId findUserId=new FindUserId(mContext); userid=findUserId.ReadUserId(); initData(0); return view; } public void initData(int a) { if(a!=0) { notePojoList.subList(0,notePojoList.size()).clear(); } OkHttpUtils .get() .url(url) .addParams("method", "getMyNote") .addParams("user_id", "" + userid) .build( ) .execute(new StringCallback( ) { @Override public void onError(Call call, Exception e, int id) { Toast.makeText(mContext, "获取数据失败,请检查网络", Toast.LENGTH_SHORT).show( ); } @Override public void onResponse(String response, int id) { JSONArray array = JSONArray.parseArray(response); for (int i = 0; i < array.size( ); i++) { JSONObject json = array.getJSONObject(i); Note_pojo notePojo = new Note_pojo(); notePojo.setNote_id(json.getInteger("note_id")); notePojo.setTitle(json.getString("title")); notePojo.setCourse(json.getString("course")); notePojo.setDate(json.getString("date")); notePojoList.add(notePojo); } if(a==0) { initListView(); } else { note_localAdapter.refresh(); Toast.makeText(mContext,"删除数据成功",Toast.LENGTH_SHORT).show(); } } }); } private void initListView() { note_localAdapter = new Note_localAdapter(mContext,R.layout.mynote_list_item, notePojoList); mynoteListView.setAdapter( note_localAdapter); mynoteListView.setOnItemClickListener(new AdapterView.OnItemClickListener( ) { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Note_pojo notePojo = notePojoList.get(position); Toast.makeText(mContext, "你好" + notePojo.getNote_id(), Toast.LENGTH_SHORT).show( ); Intent intent=new Intent(view.getContext(), DetailNoteActivity.class); intent.putExtra("note_id",notePojo.getNote_id()); intent.putExtra("view","none"); startActivity(intent); } }); mynoteListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener( ) { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { mDialogBuilder.setTitle("提示"); mDialogBuilder.setMessage("您是否要删除该条记录?"); mDialogBuilder.setPositiveButton("是", new DialogInterface.OnClickListener( ) { @Override public void onClick(DialogInterface dialog, int which) { Note_pojo notePojo = notePojoList.get(position); delete(notePojo.getNote_id()); Toast.makeText(mContext, "删除成功" + notePojo.getNote_id(), Toast.LENGTH_SHORT).show( ); } }); mDialogBuilder.setNegativeButton("否", new DialogInterface.OnClickListener( ) { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss( ); } }); mDialogBuilder.show( ); return true; } }); } private void delete(int note_id) { OkHttpUtils .post() .url(url) .addParams("method", "deleteMyNote") .addParams("user_id", ""+userid) .addParams("note_id", ""+note_id) .build() .execute(new StringCallback( ) { @Override public void onError(Call call, Exception e, int id) { Toast.makeText(mContext,"删除数据失败,请检查网络",Toast.LENGTH_SHORT).show(); } @Override public void onResponse(String response, int id) { if(response.equals("success")) { initData(1); } } }); } public interface noterefresh { public void refresh(); } }