找出从Custom ListView中单击的按钮

时间:2014-06-20 06:06:47

标签: android listview android-asynctask onitemclicklistener custom-lists

在我的应用程序中,T有自定义ListView,带有2个按钮。现在我想要的是当用户点击ListView中的特定按钮时,调用异步任务将少量参数发送到服务器。参数来自ArrayList 。现在我怎么知道从ListView中点击了哪个按钮,以及在该特定位置应该从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;
        }
    }
}

列出图片 enter image description here

8 个答案:

答案 0 :(得分:3)

getView 中,您可以使用 onclick

设置文本后使用

appliedJobs.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // your code here 
            }
        });

答案 1 :(得分:3)

您可以执行以下操作,

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

        }

    }

答案 2 :(得分:1)

您在onClick()传递的视图是您要查找的按钮。

public void onClick(View v) {
    // 1) Possibly check for instance of first 
    Button b = (Button)v;
    String buttonText = b.getText().toString();
}

答案 3 :(得分:1)

只需使用这样的按钮设置标记。

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


            }
        });

答案 4 :(得分:0)

    onClick(View v){

       int id=v.getId();

       if(id==R.id.matching_jobs){
          ............write your code.........
       }
       else if(id== R.id.applied_jobs){
          ...............write your code..........
       }

    }

这可能对你有帮助......

答案 5 :(得分:0)

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

答案 6 :(得分:0)

在自定义适配器的getView()方法中,您可以为两个按钮触发onClick事件。

appliedJobs.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // your code here 
        }
    });

MatchingJobs.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // your code here 
        }
    });

答案 7 :(得分:0)

不是覆盖自定义适配器的onClick方法并从那里执行AsyncTask,而是在getView()方法中定义Button时附加OnClickListener。例如,在Buttons方法中定义getView()时:

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

    Button bestCandidate = (Button) view.findViewById (R.id.bt_best_candidates);
    bestCandidate.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

               // Start your AsyncTask here.

            }   
    }); 

    Button appliedJobs = (Button) view.findViewById (R.id.bt_applied_jobs);
    appliedJobs.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

               // Start your AsyncTask here.

            }   
    }); 

 ........
} // end of getView()

这种方法的一个优点是它确保您不必担心找到包含在执行期间单击的按钮的列表项元素的位置,因为它们绑定到视图(列表项),即在您的LayoutInflater方法中被getView()夸大了。您的列表很可能会超出屏幕大小,导致ListView回放其元素,这意味着如果您想要更改按钮上的列表项的背景颜色,你会遇到意想不到的结果。在OnClickListeners方法中实施getView()也可以通过操纵getView()方法本身中列表项的位置来帮助您管理这种情况,因为它可以作为方法的参数随时可用。