1

我想从 listview 孩子的 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;

    }
4

5 回答 5

3

在设置点击监听器之前:

holder.form_details.setId(position);

在 onClick 方法中:

v.getId()会给你职位。

于 2012-05-02T12:35:24.083 回答
1

我们可以在点击事件中获得位置的两种方式:1)使用 OnItemClickListener

        list.setOnItemClickListener(new OnItemClickListener(){

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

        }
    };

2) 在 getView() 方法中,您可以获得位置。只需将其更改为“最终 int 位置”即可在视图的 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;

}
于 2012-05-02T12:45:58.237 回答
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));
于 2012-05-02T12:39:05.067 回答
0

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

于 2012-05-02T12:34:18.200 回答
0

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

于 2012-05-02T12:36:45.767 回答