单击按钮时刷新列表

时间:2014-06-24 06:35:21

标签: android listview

我有一个带有按钮Apply的列表。当用户点击“应用”按钮时,会调用一个弹出窗口,用户输入一些值并将值发送到Web服务。当我获得成功响应时,我想要刷新我的webservice列表,以便应用按钮上的文本应更改为应用

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 ();
                }

            }
        }
    }

列表类

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);
    }
}  

4 个答案:

答案 0 :(得分:0)

您可以将新数据提供给阵列和电话:

adapter.notifyDatasetChanged();

它将刷新ListView。

答案 1 :(得分:0)

获得成功后..您可以将该值存储到list,然后只需致电yourAdapter.notifyDataSetChanged()

答案 2 :(得分:0)

尝试 -

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

}); 

答案 3 :(得分:0)

创建与ArrayList<Integer> status大小相同的ArrayList<HashMap<String, String>> data;现在,无论何时发出请求并获得成功,请将位置添加到status。然后在getView()方法中:

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

此外,在您将状态添加到状态arraylist之后,请致电adapter.notifyDataSetChanged()。这将确保刷新列表。