简体   繁体   English

单击按钮刷新列表

[英]Refresh list on click of button

I have a list with button Apply.When the user clicks on the apply button an popup is called from which user enters some values and sends the values to the web service.When i get success response the webservice i want to refresh my list so that the text on button that is Apply should be changed to Applied 我有一个带有按钮Apply的列表。当用户单击Apply按钮时,会弹出一个对话框,用户从中输入一些值并将这些值发送到Web服务。当我成功响应Web服务时,我想刷新我的列表,以便“应用”按钮上的文本应更改为“已应用”

CustomAdapter 自定义适配器

    public class BestCandidateCustomList extends BaseAdapter {
        Context c;
        ArrayList<HashMap<String, String>> data;
        private ProgressDialog pDialog;
        public String cost, comments;
        EditText etCost, etComments;
        String success;


        public BestCandidateCustomList(Context c, ArrayList<HashMap<String, String>> data) {
            super ();
            this.c = c;
            this.data = data;


        }

        @Override
        public int getCount() {
            return data.size ();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {

            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder ();
                view = LayoutInflater.from (c).inflate (R.layout.best_candidate_custom_list, viewGroup, false);

    //            view.setTag (resultp);
                holder.name = (TextView) view.findViewById (R.id.tv_name);
                holder.gender = (TextView) view.findViewById (R.id.tv_gender);
                holder.age = (TextView) view.findViewById (R.id.tv_age);
                holder.profession = (TextView) view.findViewById (R.id.tv_profession);
                holder.mobile = (TextView) view.findViewById (R.id.tv_mobile);
                holder.expYrs = (TextView) view.findViewById (R.id.tv_exp_yrs);
                holder.mnths = (TextView) view.findViewById (R.id.tv_exp_mnths);
                holder.apply = (Button) view.findViewById (R.id.bt_apply);
                view.setTag (holder);
            } else {

                holder = (ViewHolder) view.getTag ();
            }

            if (data.get (i).get ("status").equals ("null")) {
                holder.apply.setText ("Apply");
            } else {
                holder.apply.setText (data.get (i).get ("status"));
                if (holder.apply.getText ().equals ("Applied")) {
                    holder.apply.setEnabled (false);
                }


            }

    //        if (holder.apply.getText ().equals ("Applied")) {
    //            holder.apply.setFocusable (false);
    //            holder.apply.setClickable (false);
    //            holder.apply.setFocusableInTouchMode (false);
    //            holder.apply.setEnabled (false);
    //
    //
    //        }

            holder.apply.setOnClickListener (new View.OnClickListener () {
                @Override
                public void onClick(View view) {
                    final Dialog dialog = new Dialog (c);
                    dialog.setContentView (R.layout.apply_popup);
                    dialog.setTitle ("Apply");
                    etCost = (EditText) dialog.findViewById (R.id.et_cost);
                    etComments = (EditText) dialog.findViewById (R.id.et_comments);
                    holder.ok = (Button) dialog.findViewById (R.id.bt_ok);
                    holder.cancel = (Button) dialog.findViewById (R.id.bt_cancel);

                    holder.ok.setOnClickListener (new View.OnClickListener () {
                        @Override
                        public void onClick(View view) {
                            String requestId = data.get (i).get ("requestId");
                            String resourceId = data.get (i).get ("resourceId");
                            String requestorId = data.get (i).get ("requestorId");
                            String entityCode = data.get (i).get ("entityCode");

                            Apply apply = new Apply ();
                            apply.execute (requestId, resourceId, requestorId, entityCode);
                            dialog.dismiss ();
                        }
                    });

                    holder.cancel.setOnClickListener (new View.OnClickListener () {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss ();
                        }
                    });


                    dialog.show ();
                }
            });
            holder.name.setText (data.get (i).get ("name"));
            holder.age.setText (data.get (i).get ("age"));
            holder.gender.setText (data.get (i).get ("gender"));
            holder.profession.setText (data.get (i).get ("profession"));
            holder.mobile.setText (data.get (i).get ("mobile"));
            holder.expYrs.setText (data.get (i).get ("exp"));


            return view;
        }

        class ViewHolder {
            TextView name;
            TextView gender;
            TextView age;
            TextView profession;
            TextView mobile;
            TextView expYrs;
            TextView mnths;
            Button apply;
            Button ok, cancel;


        }

        public class Apply extends AsyncTask<String, String, String> {
            String vendorId = SettingPreference.getVendorId (c);
            String userId = SettingPreference.getUserId (c);
            String strCost = etCost.getText ().toString ();
            String strComments = etComments.getText ().toString ();

            @Override

            protected String doInBackground(String... strings) {
                String response = HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/AddjobApplications").send ("IJob_Request_ID=" + strings[0] + "&IJob_Resource_ID=" + strings[1] + "&IJob_Requestor_ID=" + strings[2] + "&IEntity_Code=" + strings[3] + "&Vendor_IEntity_Code=" + vendorId + "&Applied_By_Company_IEntity_Code=" + vendorId + "&Create_User_Id=" + userId + "&Estimated_Cost=" + strCost + "&Comments=" + strComments).body ();

                response = response.replaceAll ("<[^>]*>", "").replaceAll ("\n", "");
                Log.e ("best candidates", "" + response);
                return response;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute ();
                pDialog = new ProgressDialog (c);
                pDialog.setMessage ("Please wait...");
                pDialog.setCancelable (false);
                pDialog.show ();
            }

@Override
            protected void onPostExecute(String s) {
                super.onPostExecute (s);
                try {
                    JSONObject jsonObject = new JSONObject (s);
                    success = jsonObject.getString ("success");

                    if (success.equals ("0")) {
                        Toast.makeText (c, "Apply sucessful", Toast.LENGTH_LONG).show ();
                    } else {
                        Toast.makeText (c, "Apply sucessful", Toast.LENGTH_LONG).show ();
                    }
                } catch (JSONException e) {
                    e.printStackTrace ();
                }
                if (pDialog.isShowing ()) {
                    pDialog.dismiss ();
                }

            }
        }
    }

List Class 列表类

public class BestCandidate extends Activity {
    private ListView lvBestCanditate;
    BestCandidateCustomList customList;
    Context c = this;
    Intent i;
    TextView jobCode, category;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.best_candidate_list);
        initialize ();
    }

    private void initialize() {
        i = new Intent ();
        lvBestCanditate = (ListView) findViewById (R.id.listView);
        jobCode = (TextView) findViewById (R.id.tv_job_code);
        category = (TextView) findViewById (R.id.tv_category);
        customList = new BestCandidateCustomList (c, SearchJobsCustomList.candidateArray);
        lvBestCanditate.setAdapter (customList);
        lvBestCanditate.invalidateViews ();

        jobCode.setText (SearchJobsCustomList.jobCode);
        category.setText (SearchJobsCustomList.category);
    }
}  

You can give your new data to your array and the call: 您可以将新数据提供给数组和调用:

adapter.notifyDatasetChanged();

It will refresh your ListView. 它将刷新您的ListView。

获得成功之后。您可以将该值存储到list ,只需调用yourAdapter.notifyDataSetChanged()

Try- 尝试-

applyBtn=(Button) v.findViewById(R.id.apply);  
applyBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          myadapter.notifyDataSetChanged();
     }

}); 

Create an ArrayList<Integer> status with the same size as ArrayList<HashMap<String, String>> data; 创建一个与ArrayList<HashMap<String, String>> data;大小相同的ArrayList<Integer> status ArrayList<HashMap<String, String>> data; Now, whenever you make the request and get success, add the position to status . 现在,只要您提出请求并获得成功,就将职位添加到status Then inside getView() method : 然后在getView()方法内部:

if(status.contains(new Integer(position))) {
    button.setText("Applied");
}
else {
    button.setText("Apply");
}

Also, make a call to adapter.notifyDataSetChanged() after you've added the position to status arraylist. 另外,将位置添加到状态arraylist后,请调用adapter.notifyDataSetChanged() This'll ensure that the list is refreshed. 这将确保刷新列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM