0

我通过以编程方式膨胀行创建了一个表,并尝试在这些行中的每个视图上设置点击侦听器,但它似乎不起作用。

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

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

我做错了什么?

4

2 回答 2

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());
于 2012-10-04T10:00:53.693 回答
0

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

于 2012-10-04T10:00:06.880 回答