gpt4 book ai didi

android - 从 Custom ListView 中找出点击了哪个按钮

转载 作者:行者123 更新时间:2023-11-29 14:31:46 26 4
gpt4 key购买 nike

在我的应用程序中,T 有带有 2 个按钮的自定义 ListView。现在我想要的是当用户单击 ListView 中的特定按钮时,将调用一个异步任务,该任务将少量参数发送到服务器。参数来自 ArrayList .现在我如何知道从 ListView 中单击了哪个按钮,以及在那个特定位置应该从 ArrayList 发送相同的数据。

自定义适配器.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;
}
}
}

列表图片 enter image description here

最佳答案

你可以像下面那样做,

-首先在 Button 初始化之后,在 getView() 中为您的 Button 设置标签,

bestCandidate.setTag(i);

然后在onClick方法中,你可以通过下面的操作得到列表行位置的Button标签,

 @Override
public void onClick(View view) {

switch (view.getId ()){
case R.id.bt_best_candidates:

Toast.makeText(getActivity(), view.getTag()+" is clicked", Toast.LENGTH_SHORT).show();
BestCandidateDisplay display=new BestCandidateDisplay ();
display.execute ();

}

}

关于android - 从 Custom ListView 中找出点击了哪个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24320922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com