2012-10-04 25 views
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; 
    } 

我在做什么错了?

回答

1

我想你应该设置onClickListener每个LinearLayout中是这样的:

猜测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()); 
+0

谢谢:)日志在Logcat中更新,但textView is'nt正在创建....任何想法y?为什么,nt我的实现点击监听器的方式不起作用? – zoram

+0

您为每个元素创建CustomListener()的新实例,这是不必要的,并会使用大量内存 – Dimanoid

0

至于我看到你没有设置onClickListener 你需要调用ll.setOnClickListener(this);的每一个细胞

+0

btw,你是什么意思?(new CustomListener())。onClick(ll);'? – Dimanoid

+0

CustomListner()是我的类实现onclicklistener – zoram

相关问题