lawang code

lawang Code


Tanyakan kode disini

Cara refresh List data di recycleview setelah delete

ditanyakan oleh Muhammad Iqbal 6 bulan yang lalu

saya sudah berhasil menghapus data dari database. tapi masalahnya data yang saya hapus masih ada di listview kecuali saya merefreshnya. ini kode AdapterList :

public class AdapterList extends RecyclerView.Adapter<AdapterList.ViewHolder>{

Context context;
ArrayList<HashMap<String, String>> list_data;
public static final String URL_Delete = "http://bls.hol.es/delete.php?id=";
    public AdapterList(galery galery, ArrayList<HashMap<String, String>> list_data) {
    this.context = galery.getContext();
    this.list_data = list_data;

}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Glide.with(context)
            .load("http://bls.hol.es/" + list_data.get(position).get("poto"))
            .crossFade()
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.imghape);
    final String nama=list_data.get(position).get("nama");
    final String f_id=list_data.get(position).get("id"); //This is my string

    holder.txthape.setText(nama);
    holder.txtalamat.setText(list_data.get(position).get("alamat"));
    holder.txthape.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            Snackbar snackbar = Snackbar
               .make(view, nama+" dipilih", Snackbar.LENGTH_INDEFINITE)
               .setAction("hapus", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        hapus(f_id);
//Saya mau menaruh refreshnya disini

                    }
               });

            snackbar.show();
            return true;


        }
    });
@Override
public int getItemCount() {
    return list_data.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView txthape;
    TextView txtalamat;
    ImageView imghape;
    CardView cv;


    public ViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.lvhape);
        txthape = (TextView) itemView.findViewById(R.id.txthape);
        txtalamat = (TextView) itemView.findViewById(R.id.txtalamat);
        imghape = (ImageView) itemView.findViewById(R.id.imghp);
    }
}
private void hapus(final String f_id){
    class cari extends AsyncTask<String,Void,String> {
        RequestHandler rh = new RequestHandler();
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        @Override
        protected void onPostExecute(String r) {
            Toast.makeText(context,r, Toast.LENGTH_LONG).show();
            getItemCount();
        }
        @Override
        protected String doInBackground(String... params) {

            HashMap<String,String> data = new HashMap<>();
            String result = rh.sendPostRequest(URL_Delete+f_id,data);
            return result;
        }
    }

    cari ui = new cari();
    ui.execute();
}


dan ini kode galery.java

public class galery extends Fragment {
private RecyclerView lvhape;
private RequestQueue requestQueue;
private StringRequest stringRequest;
ArrayList<HashMap<String, String>> list_data;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_galery, container, false);
    String url = "http://bls.hol.es/read.php";

    lvhape = (RecyclerView) rootView.findViewById(R.id.lvhape);
    LinearLayoutManager llm = new LinearLayoutManager(getContext());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    lvhape.setLayoutManager(llm);

    requestQueue = Volley.newRequestQueue(getContext());

    list_data = new ArrayList<HashMap<String, String>>();

    stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("response", response);
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("result");
                for (int a = 0; a < jsonArray.length(); a++) {
                    JSONObject json = jsonArray.getJSONObject(a);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("id", json.getString("id"));
                    map.put("nama", json.getString("nama"));
                    map.put("alamat", json.getString("alamat"));
                    map.put("poto", json.getString("poto"));
                    list_data.add(map);
                    AdapterList adapter = new AdapterList(galery.this, list_data);
                    lvhape.setAdapter(adapter);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    requestQueue.add(stringRequest);
    return rootView;
}
}



Anda harus login terlebih dahulu untuk memberikan komentar

Recent Tag

X 6
X 49
X 14
X 10
X 1
X 5
X 4
X 11
X 10

Bantuan

Help

Feedback

Help