Android 找出在自定义列表视图中单击的按钮
在我的应用程序中,我没有带2个按钮的自定义ListView。现在我想要的是,当用户单击ListView中的某个特定按钮时,会调用一个异步任务,该任务将少数参数发送到服务器。这些参数来自ArrayList。现在,我如何知道从ListView中单击了哪个按钮,以及在该特定位置单击了哪个按钮位置相同的数据应该从ArrayList发送 JavaAndroid 找出在自定义列表视图中单击的按钮,android,listview,android-asynctask,onitemclicklistener,custom-lists,Android,Listview,Android Asynctask,Onitemclicklistener,Custom Lists,在我的应用程序中,我没有带2个按钮的自定义ListView。现在我想要的是,当用户单击ListView中的某个特定按钮时,会调用一个异步任务,该任务将少数参数发送到服务器。这些参数来自ArrayList。现在,我如何知道从ListView中单击了哪个按钮,以及在该特定位置单击了哪个按钮位置相同的数据应该从ArrayList发送 Java public class SearchJobsCustomList extends BaseAdapter implements View.OnClickLis
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;
}
}
}
公共类SearchJobsCustomList扩展BaseAdapter实现View.OnClickListener{
上下文c;
阵列列表数据;
HashMap resultp=newhashmap();
公共搜索作业列表(上下文c、数组列表数据){
超级();
这个.c=c;
这个数据=数据;
}
@凌驾
public int getCount(){
返回data.size();
}
@凌驾
公共对象getItem(int i){
返回null;
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
如果(视图==null){
view=LayoutFlater.from(c).充气(R.layout.custom\u search\u jobs\u List,viewGroup,false);
resultp=data.get(i);
view.setTag(resultp);
TextView作业代码=(TextView)view.findViewById(R.id.tv\u作业代码);
TextView类别=(TextView)view.findViewById(R.id.tv_名称);
TextView ExpYrs=(TextView)view.findViewById(R.id.tv\u exp\u yrs);
TextView ExpMnths=(TextView)view.findViewById(R.id.tv\u exp\u mnths);
TextView日期=(TextView)view.findViewById(R.id.tv_日期);
按钮最佳候选项=(按钮)view.findViewById(R.id.bt\u最佳候选项);
Button appliedJobs=(Button)view.findViewById(R.id.bt_applied_jobs);
bestCandidate.setOnClickListener(此);
appliedJobs.setOnClickListener(此);
如果(resultp.get(“counts”).等于(0)){
bestCandidate.setFocusable(false);
bestCandidate.setText(0);
}否则{
bestCandidate.setText(resultp.get(“计数”));
}
如果(resultp.get(“applicated”).equals(0)){
appliedJobs.setFocusable(false);
appliedJobs.setText(0);
}否则{
appliedJobs.setText(resultp.get(“applied”);
}
JobCode.setText(resultp.get(“代码”));
Category.setText(resultp.get(“Category”);
ExpYrs.setText(resultp.get(“minExp”);
ExpMnths.setText(resultp.get(“maxExp”);
Date.setText(resultp.get(“postedOn”);
}
返回视图;
}
@凌驾
公共void onClick(视图){
开关(view.getId()){
案例R.id.bt_最佳候选人:
BestCandidateDisplay=新的BestCandidateDisplay();
display.execute();
}
}
公共类BestCandidateDisplay扩展异步任务{
@凌驾
受保护的字符串背景(字符串…字符串){
字符串响应=HttpRequest.post(“https://beta135.hamarisuraksha.com/web/WebService/HsJobService.asmx/GetBestCandidates发送(“供应商客户代码=“+”&IJob_请求ID=“+resultp.get(“reqId”)+”&IJob_请求者ID=“+resultp.get(“iReqId”)+”&Mode=“+”TTL”).body();
返回null;
}
}
}
列表图像
在
onClick()
上传入的视图就是您要查找的按钮
public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}
在
onClick()
上传入的视图就是您要查找的按钮
public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}
这可能会帮助你
这可能会帮助您……只需使用如下按钮设置标签
bestCandidate.setTag(10*i+1);
appliedJobs.setTag(10*i+2);
在onClick方法中,项目在列表中的位置可以通过这样的反向过程来计算
int tag = (Integer) view.getTag();
int pos = (tag - 1)/10;
// or
int pos = (tag - 2)/10;
或者试试这个
bestCandidate.setTag(10*i);
appliedJobs.setTag(10*i);
bestCandidate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Integer pos = (Integer) v.getTag();
}
});
appliedJobs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Integer pos = (Integer) v.getTag();
}
});
用这样的按钮设置标签
bestCandidate.setTag(10*i+1);
appliedJobs.setTag(10*i+2);
在onClick方法中,项目在列表中的位置可以通过这样的反向过程来计算
int tag = (Integer) view.getTag();
int pos = (tag - 1)/10;
// or
int pos = (tag - 2)/10;
或者试试这个
bestCandidate.setTag(10*i);
appliedJobs.setTag(10*i);
bestCandidate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Integer pos = (Integer) v.getTag();
}
});
appliedJobs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Integer pos = (Integer) v.getTag();
}
});
在getViewit self中,您可以使用onclick 设置文本后,请像这样使用
appliedJobs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// your code here
}
});
在getViewit self中,您可以使用onclick 设置文本后,请像这样使用
appliedJobs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// your code here
}
});
你可以像下面这样做 -在
按钮
初始化之后,首先在getView()
中为您的按钮
设置标记
bestCandidate.setTag(i);
然后在onClick
方法中,您可以执行以下操作以获得按钮
标记,该标记是列表行的位置
@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 ();
}
}
你可以像下面这样做 -在
按钮
初始化之后,首先在getView()
中为您的按钮
设置标记
bestCandidate.setTag(i);
然后在onClick
方法中,您可以执行以下操作以获得按钮
标记,该标记是列表行的位置
@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 ();
}
}
试试这个方法,希望这能帮你解决问题。
公共类SearchJobsCustomList扩展了BaseAdapter{
语境;
阵列列表数据;
public SearchJobsCustomList(上下文、ArrayList数据){
超级();
this.context=上下文;
这个数据=数据;
}
@凌驾
public int getCount(){
返回data.size();
}
@凌驾
公共对象getItem(int i){
返回null;
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共视图getView(最终int i、视图视图、视图组视图组){
视窗座;
如果(视图==null){
holder=新的ViewHolder();
view=LayoutFlater.from(context).充气(R.layout.custom\u search\u jobs\u list,null,false);
holder.JobCode=(TextView)view.findViewById(R.id.tv\u job\u code);
holder.Category=(文本视图)vie