我通過以編程方式膨脹行創建了一個表,並嘗試將單擊偵聽器設置爲這些行中的每個視圖,但似乎不起作用。單擊表單單元上的偵聽器不會在點擊方法中調用?
這是我的充氣機,以及我怎麼稱呼我的點擊方法。
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;
}
我在做什麼錯了?
謝謝:)日誌在Logcat中更新,但textView is'nt正在創建....任何想法y?爲什麼,nt我的實現點擊監聽器的方式不起作用? – zoram
您爲每個元素創建CustomListener()的新實例,這是不必要的,並會使用大量內存 – Dimanoid