In my application T have custom ListView with 2 buttons.Now what I want is when user clicks on a particular button in the ListView an async task is called which sends the few parameters to the server.The parameters are comming from the ArrayList.Now how will I come to know which button was clicked from the ListView and also at that particular position the same data should be sent from the ArrayList.
CustomAdapter.Java
public class SearchJobsCustomList extends BaseAdapter implements View.OnClickListener {
Context c;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String> ();
public SearchJobsCustomList(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(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from (c).inflate (R.layout.custom_search_jobs_lists, viewGroup, false);
resultp = data.get (i);
view.setTag (resultp);
TextView JobCode = (TextView) view.findViewById (R.id.tv_job_code);
TextView Category = (TextView) view.findViewById (R.id.tv_name);
TextView ExpYrs = (TextView) view.findViewById (R.id.tv_exp_yrs);
TextView ExpMnths = (TextView) view.findViewById (R.id.tv_exp_mnths);
TextView Date = (TextView) view.findViewById (R.id.tv_date);
Button bestCandidate = (Button) view.findViewById (R.id.bt_best_candidates);
Button appliedJobs = (Button) view.findViewById (R.id.bt_applied_jobs);
bestCandidate.setOnClickListener (this);
appliedJobs.setOnClickListener (this);
if (resultp.get ("counts").equals (0)) {
bestCandidate.setFocusable (false);
bestCandidate.setText (0);
} else {
bestCandidate.setText (resultp.get ("counts"));
}
if (resultp.get ("applied").equals (0)) {
appliedJobs.setFocusable (false);
appliedJobs.setText (0);
} else {
appliedJobs.setText (resultp.get ("applied"));
}
JobCode.setText (resultp.get ("code"));
Category.setText (resultp.get ("category"));
ExpYrs.setText (resultp.get ("minExp"));
ExpMnths.setText (resultp.get ("maxExp"));
Date.setText (resultp.get ("postedOn"));
}
return view;
}
@Override
public void onClick(View view) {
switch (view.getId ()){
case R.id.bt_best_candidates:
BestCandidateDisplay display=new BestCandidateDisplay ();
display.execute ();
}
}
public class BestCandidateDisplay extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... strings) {
String response= HttpRequest.post ("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/GetBestCandidates").send ("Vendor_IEntity_Code=" + "&IJob_Req_ID=" + resultp.get ("reqId") + "&IJob_Requestor_ID=" + resultp.get ("iReqId") + "&Mode=" + "TTL").body ();
return null;
}
}
}
List Image
Try this way,hope this will help you to solve your problem.
public class SearchJobsCustomList extends BaseAdapter{
Context context;
ArrayList<HashMap<String, String>> data;
public SearchJobsCustomList(Context context, ArrayList<HashMap<String, String>> data) {
super ();
this.context = context;
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) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.custom_search_jobs_lists, null, false);
holder.JobCode = (TextView) view.findViewById(R.id.tv_job_code);
holder.Category = (TextView) view.findViewById(R.id.tv_name);
holder.ExpYrs = (TextView) view.findViewById(R.id.tv_exp_yrs);
holder.ExpMnths = (TextView) view.findViewById(R.id.tv_exp_mnths);
holder.Date = (TextView) view.findViewById(R.id.tv_date);
holder.bestCandidate = (Button) view.findViewById(R.id.bt_best_candidates);
holder.appliedJobs = (Button) view.findViewById(R.id.bt_applied_jobs);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
if (data.get(i).get("counts").equals("0")) {
holder.bestCandidate.setFocusable (false);
holder.bestCandidate.setText (0);
} else {
holder.bestCandidate.setText (data.get(i).get("counts"));
}
if (data.get(i).get("applied").equals("0")) {
holder.appliedJobs.setFocusable (false);
holder.appliedJobs.setText (0);
} else {
holder.appliedJobs.setText (data.get(i).get ("applied"));
}
holder.JobCode.setText (data.get(i).get("code"));
holder.Category.setText (data.get(i).get("category"));
holder.ExpYrs.setText (data.get(i).get("minExp"));
holder.ExpMnths.setText (data.get(i).get("maxExp"));
holder.Date.setText (data.get(i).get("postedOn"));
holder.appliedJobs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String,String> row = data.get(i);
BestCandidateDisplay display=new BestCandidateDisplay ();
display.execute(row.get("reqId"), row.get("reqId"));
}
});
holder.appliedJobs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap<String,String> row = data.get(i);
BestCandidateDisplay display=new BestCandidateDisplay ();
display.execute(row.get("reqId"),row.get("reqId"));
}
});
return view;
}
class ViewHolder {
TextView Category;
TextView ExpYrs;
TextView ExpMnths;
TextView Date;
TextView JobCode;
Button bestCandidate;
Button appliedJobs;
}
}
public class BestCandidateDisplay extends AsyncTask<String,String,String> {
@Override
protected String doInBackground(String... strings) {
String response= HttpRequest.post("https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/GetBestCandidates").send ("Vendor_IEntity_Code=" + "&IJob_Req_ID=" +strings[0]+ "&IJob_Requestor_ID=" +strings[1]+ "&Mode=" + "TTL").body ();
return null;
}
}