HI,
I want to create a table in android, should contains a lot of rows. Each row has 4 columns, and if i click any view, i want to i开发者_如何学Cntegrate onClick event for the view. I have developed something similar to the requirement,But didn't got the click view, Here is my code :
LinearLayout lLayout1= null; LinearLayout main_lLayout= null; LinearLayout lLayout2= null; TextView myText[] = new TextView[12];
LinearLayout myLayout[] = new LinearLayout[12];
@Override
public void onCreate(Bundle icici) {
super.onCreate(icici);
main_lLayout = new LinearLayout(this);
main_lLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
main_lLayout.setOrientation(LinearLayout.VERTICAL);
int k = 0;
for(int i=0;i<4;i++) {
myLayout[i] = new LinearLayout(this);
for(int j=0;j<4;j++) {
myText[j] = new TextView(this);
myText[j].setText("asdf"+i+j);
myText[j].setPadding(0, 0, 20, 10);
myText[j].setClickable(true);
myText[j].setId(k);
myText[j].setOnClickListener(this);
k++;
System.out.println(k);
myLayout[i].addView(myText[j]);
}
main_lLayout.addView(myLayout[i]);
}
setContentView(main_lLayout);
}
@Override
public void onClick(View v) {
System.out.println(myText[0].getText());
System.out.println(myText[10].getText());
}
You have some bad array management there... you're instantiating 16 TextViews but only assign them to the first 4 elements of myText. If you fix this I find the click handler is called as expected.
精彩评论