2012-03-14 49 views
1

我正在使用表格佈局來自SQLite數據庫的行,所以我添加行動態添加視圖到一行。現在,我在處理行版本的每行中添加了imageButton,並將其添加到SQLite數據庫中。我想如果我走正確的道路,所以如果有一種方法可以將SetOnClickListener添加到每個生成的imageButton動態setOnCLickListener

+0

你做的一切都是錯的(在上一個quetion我看到你使用的表格佈局和填充從數據庫行)......對於喜歡的東西這使用ListView和適配器... Onitemclick方法已經有...使用谷歌和搜索列表視圖+ sqlite db樣品 – Selvin 2012-03-14 22:40:56

+0

這是一個想法,如果有可能更改列表視圖有更多列的表,我有一個自定義列表視圖與用於其他數據的數據庫在同一個應用程序中。這個想法是在窗口 – 2012-03-14 22:53:27

回答

2

我用這個代碼添加點擊事件動態生成按鈕

for (int position=0; position < parseInt; position++) 
     { 
      TableRow tableRow= new TableRow(this); 

      tableRow.setBackgroundColor(006400); 
//   ArrayList<Object> row = data.get(position); 


      TextView idText = new TextView(this); 
      idText.setText(Integer.toString(position + 1)); 
      idText.setGravity(Gravity.CENTER); 
      idText.setTextColor(Color.BLACK); 
      idText.setWidth(10); 
      idText.setHeight(30); 
      idText.setBackgroundResource(R.drawable.textbg); 
//   idText.setPadding(0, 0, 1,0); 

      tableRow.addView(idText); 



      //THE CLICK EVENT OF BUTTON 
      Button textOne = new Button(this); 
      textOne.setText("CLUB"); 
      textOne.setBackgroundResource(R.drawable.textbg); 
      textOne.setGravity(Gravity.CENTER); 
      textOne.setTextColor(Color.BLACK);//left top right bottom 
//   textOne.setPadding(2, 1, 1,0); 
//   textOne.setB; 

      textOne.setWidth(10); 
      textOne.setHeight(30); 

      textOne.setId(1+position); 
      tableRow.addView(textOne); 


//   textOne.setOnClickListener(this); 

      textOne.setOnClickListener(new OnClickListener() { 
       public void onClick(View arg0) { 
        // do something when the button is clicked 

        final Button button = (Button) arg0; 



System.out.println("button is clicked"); 



       }); 
+0

窗口中有phpMyadmin行,但如果我現在不創建多少個按鈕,因爲它取決於DB行號,我沒有動態偵聽器,是我在尋找的。順便說一下,我認爲這需要做很多工作......我已經使用cursorAdapter放置了一個列表視圖,並且使用onitemclick來管理一個小選項菜單。 – 2012-03-16 03:51:45