ListView获取单击视图和相应的行

时间:2012-05-02 12:28:53

标签: android listview onclicklistener

我想从listview child的onClick事件中获得点击的行。我能够获得点击的项目,但没有点击该事件中的行。请帮我解决问题。

提前致谢! 我的代码是

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        Holder holder = new Holder();

        pos = position;

        if(convertView == null)
        {

            System.out.println(position);


            convertView= inflater.inflate(R.layout.list_layout,(ViewGroup)parent.findViewById(R.layout.list_layout));

            holder.form_name  = (TextView) convertView.findViewById(R.id.list_text);
            holder.form_details = (TextView) convertView.findViewById(R.id.button_text);

            holder.form_details.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    System.out.println(pos);
                                   //Here i am able to get clicked view but pos has always 
                    // last value
                    System.out.println("button view clicked for "+cipt_list.get(pos).cipt);

                }});


            System.out.println(holder.form_name);

            convertView.setTag(holder);


        }
        else{

            holder = (Holder) convertView.getTag();
        }

        ciptDetails cipt = cipt_list.get(position);

        holder.form_name.setText(cipt.cipt);

        return convertView;

    }

5 个答案:

答案 0 :(得分:3)

设置点击监听器之前

holder.form_details.setId(position);

并在onClick方法中:

v.getId()会给你这个位置。

答案 1 :(得分:1)

我们可以在点击事件中占据一席之地: 1)使用OnItemClickListener

        list.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {

        }
    };

2)在getView()方法中,您获得了位置。只需将其更改为“final int position”,然后您就可以访问视图的onClickListener()

public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    Holder holder = new Holder();

    pos = position;

    if(convertView == null)
    {

        System.out.println(position);


        convertView= inflater.inflate(R.layout.list_layout,(ViewGroup)parent.findViewById(R.layout.list_layout));

        holder.form_name  = (TextView) convertView.findViewById(R.id.list_text);
        holder.form_details = (TextView) convertView.findViewById(R.id.button_text);

        holder.form_details.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                // you can access the position here
                System.out.println(pos);
                               //Here i am able to get clicked view but pos has always 
                // last value
                System.out.println("button view clicked for "+cipt_list.get(pos).cipt);

            }});


        System.out.println(holder.form_name);

        convertView.setTag(holder);


    }
    else{

        holder = (Holder) convertView.getTag();
    }

    ciptDetails cipt = cipt_list.get(position);

    holder.form_name.setText(cipt.cipt);

    return convertView;

}

答案 2 :(得分:0)

您需要实施onListItemClick方法。第三个参数是保存您在适配器中单击的信息的位置。

答案 3 :(得分:0)

你的意思是列表适配器中的点击行吗?如果是这样,请使用listAdatper.get(position)

答案 4 :(得分:0)

一个简单的解决方法就是为侦听器创建一个子类,并为行添加一个int值,如此

private class MyClickListener implements View.OnClickListener{
  int row;
  public MyClickListener(int r){
    row = r;
  }
  //rest of your code goes here
  ...
}

现在当你创建它时只需要调用它

 holder.form_details.setOnClickListener(new MyClickListener(pos));