Reputation: 512
I created a table by inflating rows programatically and tried setting on click listener to each views in those rows but it doesn't seem to work.
This is my inflator and how i call my click method.
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);
}
And this is my click listener class.
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;
}
what am i doin wrong?
Upvotes: 0
Views: 608
Reputation: 55
I guess you should set the onClickListener for each LinearLayout like this:
Guessing that CustomListener is implementing your onClick() method
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());
Upvotes: 1
Reputation: 7289
As far as i see you didnt set onClickListener
You need to call ll.setOnClickListener(this);
for every cell
Upvotes: 0