2012-10-04 21 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

相關問題