单击表格单元格上的监听器而不是在单击方法中调用?

时间:2012-10-04 09:47:32

标签: android onclick tablelayout

我通过以编程方式对行进行膨胀来创建一个表,并尝试将click侦听器设置为这些行中的每个视图,但它似乎不起作用。

这是我的充气机以及我如何调用我的点击方法。

  inflater = (LayoutInflater) getActivity().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);

    for (int i = 0; i < time.length; i++) {

        TableRow row = (TableRow) inflater.inflate(R.layout.week_view_cust,
                t_layout, false);
        TextView tv = (TextView) row.findViewById(R.id.time_week_tv);
        LinearLayout ll = (LinearLayout) row.findViewById(R.id.time_ll1);
        ll = (LinearLayout) row.findViewById(R.id.time_ll2);
        ll = (LinearLayout) row.findViewById(R.id.time_ll3);
        ll = (LinearLayout) row.findViewById(R.id.time_ll4);
        ll = (LinearLayout) row.findViewById(R.id.time_ll5);
        ll = (LinearLayout) row.findViewById(R.id.time_ll6);
        ll = (LinearLayout) row.findViewById(R.id.time_ll7);
        (new CustomListener()).onClick(ll);
        tv.setText("" + time[i]);
        t_layout.addView(row);

    }

这是我的点击监听器类。

    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.time_ll1:

        LinearLayout ll = (LinearLayout)v.findViewById(R.id.time_ll1);
        TextView tv = new TextView(v.getContext());
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tv.setTextSize(12);
        tv.setTextColor(000000);
        Log.i("============", "success");
        tv.setText("hello");
        ll.addView(tv);
        break;

    case R.id.time_ll2:
        break;
    case R.id.time_ll3:
        break;
    case R.id.time_ll4:
        break;
    case R.id.time_ll5:
        break;
    case R.id.time_ll6:
        break;
    case R.id.time_ll7:
        break;
    }

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我想你应该为每个LinearLayout设置 onClickListener ,如下所示:

猜测CustomListener正在实现你的onClick()方法

LinearLayout ll = (LinearLayout) row.findViewById(R.id.time_ll1);
ll.setOnClickListener(new CustomListener());
ll = (LinearLayout) row.findViewById(R.id.time_ll2);
ll.setOnClickListener(new CustomListener());
ll = (LinearLayout) row.findViewById(R.id.time_ll3);
ll.setOnClickListener(new CustomListener());
ll = (LinearLayout) row.findViewById(R.id.time_ll4);
ll.setOnClickListener(new CustomListener());
ll = (LinearLayout) row.findViewById(R.id.time_ll5);
ll.setOnClickListener(new CustomListener());
ll = (LinearLayout) row.findViewById(R.id.time_ll6);
ll.setOnClickListener(new CustomListener());
ll = (LinearLayout) row.findViewById(R.id.time_ll7);
ll.setOnClickListener(new CustomListener());

答案 1 :(得分:0)

据我所知,你没有设置onClickListener 您需要为每个单元格调用ll.setOnClickListener(this);