Anuj
Anuj

Reputation: 129

Refreshing the list and the data to the list is coming from asynctask

I am having a list which should be refreshed when user clicks on a button.The data on the list is comming from json and i m parsing it and dispaying in the list.Now my prob is that when user clicks on the button the asynctask class whch does all the json parsing shud b called and the list should b refreshed

CustomList

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


    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 = 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);
            btapply = (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);
                        holder.apply.setEnabled (false);
                        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 unsucessful", Toast.LENGTH_LONG).show ();
                }
            } catch (JSONException e) {
                e.printStackTrace ();
            }

//            BestCandidate.notifyAdapter ();


            if (pDialog.isShowing ()) {
                pDialog.dismiss ();
            }


        }
    }
}

MainClass

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


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

    private void initialize() {
        i = new Intent ();
        c = BestCandidate.this;

        lvBestCanditate = (ListView) findViewById (R.id.bestcadidate_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);
    }


    public static void notifyAdapter() {
//        SearchJobsCustomList.BestCandidateDisplay a = new SearchJobsCustomList.BestCandidateDisplay ();
//        a.execute (SettingPreference.getButtonClick (c));
        SearchJobs.SearchJobsAsync a = new SearchJobs.SearchJobsAsync (c, true);
        a.execute ();
        customList = new BestCandidateCustomList (c, SearchJobsCustomList.candidateArray);
//        customList.notifyDataSetChanged ();
        lvBestCanditate.setAdapter (customList);

    }

//
}

While setting the adapter the (SearchJobsCustomList.candidateArray) contains the data for the list,so i want to update that ArrayList and refresh the list on button click

Upvotes: 0

Views: 28

Answers (1)

Kaustubh
Kaustubh

Reputation: 653

after the AsyncTask has done its task, i.e. in onPostExecute() method you can say

if (success.equals ("0")) {
    Toast.makeText (c, "Apply sucessful", Toast.LENGTH_LONG).show ();
    ((BaseAdapter) yourListView.getAdapter()).notifyDataSetChanged();
} else {
    Toast.makeText (c, "Apply unsucessful", Toast.LENGTH_LONG).show ();
}

Upvotes: 2

Related Questions