2016-12-02 54 views
0

我從json響應中獲取數據並在listView中顯示它。刪除listView中的項目時,刪除成功,但不會刷新listView。我使用ArrayAdapter而不是BaseAdapter。 適配器對象上CustomList.java如何刷新用ArrayAdapter填充的片段中的列表視圖

public class CustomList extends ArrayAdapter<String> { 

    ImageButton imgdelete; 
    private String[] clientid; 
    private String[] mobile; 
    private String[] grand; 
    private String[] billid; 
    View listViewItem; 
    TextView textViewname; 


    private Activity context; 

    public CustomList(Activity context, String[] clientid, String[] mobile, String[] grand, String[] billid) { 
     super(context, R.layout.activity_listview, clientid); 
     this.context = context; 
     this.clientid = clientid; 
     this.mobile = mobile; 
     this.grand = grand; 
     this.billid = billid; 
    } 


    @Override 
    public View getView(final int position, final View convertView, ViewGroup parent) { 
     final LayoutInflater inflater = context.getLayoutInflater(); 
     listViewItem = inflater.inflate(R.layout.activity_listview, null, true); 
     textViewname = (TextView) listViewItem.findViewById(R.id.viewClientMobile); 
     TextView textViewmobile = (TextView) listViewItem.findViewById(R.id.viewClientName); 
     TextView textViewgrand = (TextView) listViewItem.findViewById(R.id.viewCompnayName); 
     TextView textViewbillid = (TextView) listViewItem.findViewById(R.id.viewCompanyEmail); 
     ImageButton imgedit = (ImageButton)listViewItem.findViewById(R.id.imgeditbtn); 
     imgdelete = (ImageButton)listViewItem.findViewById(R.id.imgdeletebtn); 
     textViewname.setText(clientid[position]); 
     textViewmobile.setText(mobile[position]); 
     textViewgrand.setText(grand[position]); 
     textViewbillid.setText(billid[position]); 

     imgdelete.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       new AlertDialog.Builder(context) 
         .setTitle("Delete entry") 
         .setMessage("Are you sure you want to delete this entry?") 
         .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // continue with delete 
           DeleteClient_api(); 



          } 
         }) 
         .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // do nothing 
          } 
         }) 
         .setIcon(android.R.drawable.ic_dialog_alert) 
         .show(); 


       notifyDataSetChanged(); 
      } 
     }); 

     return listViewItem; 

    } 
    protected void DeleteClient_api() { 
     final StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://befoodie.co/billingsystem/client_api.php", 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         // Toast.makeText(getContext(), response, Toast.LENGTH_LONG).show(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<String, String>(); 
       // final String session = ActivityLogin.MostSecureRandom.nextSessionId(); 
       params.put("Action", "RemoveClient"); 
       params.put("ClientID",textViewname.getText().toString()); 

       return params; 
      } 

     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(getContext()); 
     requestQueue.add(stringRequest); 



    } 
} 

ViewAllClient.java

public class ViewAllClient extends Fragment { 

    private ListView listView; 


    //create Array of product Details 

    public ViewAllClient() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_view_all_client, container, false); 


     // listView = (ListView) rootView.findViewById(R.id.listviewclient); 
     listView = (ListView) rootView.findViewById(R.id.listviewclient); 
     View_api(); 
       return rootView; 
    } 
    protected void View_api(){ 
     final StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://befoodie.co/billingsystem/client_api.php", 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
        showJSON(response); //Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show(); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show(); 
        } 
       }) { 
      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<String, String>(); 
       // final String session = ActivityLogin.MostSecureRandom.nextSessionId(); 
       params.put("Action", "ViewClients"); 
       return params; 
      } 

     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); 
     requestQueue.add(stringRequest); 



    } 

    protected void showJSON(String json){ 
     ParseJSON pj = new ParseJSON(json); 
     pj.parseJSON(); 

     final CustomList cl = new CustomList(getActivity(), ParseJSON.CLIENTID,ParseJSON.mobile,ParseJSON.grand, ParseJSON.billid); 
     listView.setAdapter(cl); 


     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 


      } 
     }); 


    } 

} 

ParseJSON.java

public class ParseJSON { 


    public static String[] CLIENTID; 
    public static String[] mobile; 
    public static String[] grand; 
    public static String[] billid; 



    public static final String JSON_ARRAY = "ClientDetails"; 
    public static final String KEY_ID = "ClientID"; 
    public static final String KEY_MOBILE = "ClientName"; 
    public static final String KEY_GRAND = "EmailID"; 
    public static final String KEY_BILL = "StartingCommunicationDate"; 



    private JSONArray users = null; 

    private String json; 

    public ParseJSON(String json){ 
     this.json = json; 
    } 

    protected void parseJSON(){ 


     JSONObject jsonObject=null; 
     try { 
      jsonObject = new JSONObject(json); 
      users = jsonObject.getJSONArray(JSON_ARRAY); 

      CLIENTID = new String[users.length()]; 
      mobile = new String[users.length()]; 
      grand = new String[users.length()]; 
      billid = new String[users.length()]; 

      for(int i=0;i<users.length();i++){ 
       JSONObject jo = users.getJSONObject(i); 
       CLIENTID[i] = jo.getString(KEY_ID); 
       mobile[i] = jo.getString(KEY_MOBILE); 
       grand[i] = jo.getString(KEY_GRAND); 
       billid[i] = jo.getString(KEY_BILL); 


      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+2

首先,我建議使用替代的ListView RecyclerView。其次,你可以通知你的適配器有任何改變,只需調用adapter.notifyDataSetChanged();改變完成後。 –

+0

分享你的代碼。 – EKN

回答

0

呼叫adapter.notifyDataSetChanged()一旦你在該適配器修改的數據。 ;

注:對於ArrayAdapternotifyDataSetChanged只有當你的適配器使用add()insert()remove()clear()工作。

0

嘗試下面的代碼,

YourAdapter adapter = new YourAdapter(context, ListDataHeader, ListDataChild, data); 
adapter.notifyDataSetChanged(); 
0

嗨@Dipu首先,你必須刪除從本地jsonresponse項目(這是你的填充列表)。之後,你必須通知該數據已適配器如果從適配器內刪除,只需在刪除項目後寫入notifyDataSetChanged();即可。 如果您是從類中刪除,那麼你必須簡單地寫

adapter.notifyDataSetChanged(); 

我希望它能幫助

+0

我試試這個,但它不工作..我把這個在我的適配器.. –

0
// in ViewAllClient fragment 

    public void delete(int position){ 
     adapter.notifyItemChanged(position);//-- only update the required row 
    // notifyDataSetChanged for full listview 

}

// in adapter 
ViewAllClient viewAllClient =new ViewAllClient(); 
viewAllClient .delete(position); 
+0

我做這個代碼,但它不工作。:-( –

+0

嘗試adapter.notifydatasetchanged存在片段類 –