【问题标题】:android listview checkbox eventandroid listview复选框事件
【发布时间】:2017-08-23 23:21:27
【问题描述】:

我正在尝试实现自定义列表视图。 但是,我无法获得复选框的点击事件;

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final int pos = position;
    View view = convertView;

    if(view == null){
        LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.listview,parent, false);
    }
    TextView textview = (TextView)view.findViewById(R.id.listview_text);
    final CheckBox checkbox = (CheckBox)view.findViewById(R.id.listview_check);
    checkbox.setFocusable(false);
    checkbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, pos+"", Toast.LENGTH_SHORT).show();
        }
    });

    return super.getView(position, convertView, parent);
}

如果我单击 ListView 中的复选框,我希望弹出关于行位置的 Toast。 但是,此代码仅运行复选框单击事件仅 0 行。 其他行中的复选框不会触发事件。

我该如何解决?

【问题讨论】:

    标签: android listview checkbox


    【解决方案1】:

    getView() 的最后一行似乎很可疑。 您应该返回您创建的视图或 convertView 传递给您的视图。

    【讨论】:

      【解决方案2】:

      复选框上的 Onclicklisteners 不起作用。

      通常复选框点击监听应该是这样的

      CheckBox chkbbx = (CheckBox) findViewById(R.id.chkbbx);
      chkbbx.setOnCheckedChangeListener( new OnCheckedChangeListener() {
      
      @Override
      public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
      // TODO Auto-generated method stub
      Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
        }
      } );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 2016-02-03
        • 2015-06-07
        • 2023-04-06
        相关资源
        最近更新 更多