2011-12-13 73 views
0

我創建了一個帶有無線組的表格佈局,通過動態地爲它添加單選按鈕,並且我想知道包含無線電組的哪一行被選中,並且我想在同一行中檢索texview數據。我怎麼能這樣做,任何建議對我來說都是很大的幫助。滾動視圖中的表格行選擇錯誤

public class TabActivity extends Activity { 
TableLayout table; 
RadioGroup mRadioGroup; 
ArrayList<String> list_name; 

int color_blue = -16776961; 
int color_gray = -7829368; 
int color_black = -16777216; 
int color_white = -1; 

final int CHECK_BUTTON_ID = 982301; 
int ids_check[]; 
boolean bool_check[]; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    table = (TableLayout) findViewById(R.id.tableLayout1); 

    list_name = new ArrayList<String>(); 

    list_name.add("Close"); 
    list_name.add("Cristiano"); 
    list_name.add("David"); 
    list_name.add("Fernando"); 
    list_name.add("Messi"); 
    list_name.add("Kaka"); 
    list_name.add("Wayne"); 

    list_name.add("use"); 
    list_name.add("e"); 
    list_name.add("eff"); 
    list_name.add("euyr"); 
    list_name.add("ejjyytuty"); 
    list_name.add("madre"); 
    list_name.add("yuir"); 
    list_name.add("eyrty"); 
    list_name.add("etytr"); 
    list_name.add("ewrrtt"); 

    bool_check = new boolean[list_name.size()]; 
    ids_check = new int[list_name.size()]; 
    createTableRows(); 

    } 

    public void createTableRows() 
    { 
    for (int i = 0; i < list_name.size(); i++) 
    { 
    final TableRow table_row = new TableRow(this); 
    TextView tv_name = new TextView(this); 
    Button btn_check = new Button(this); 
    ImageView img_line = new ImageView(this); 

    table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    table_row.setBackgroundColor(color_black); 
    table_row.setGravity(Gravity.CENTER_HORIZONTAL); 
    // table_row.setFocusable(true); 

    mRadioGroup = new RadioGroup(this); 

    // test adding a radio button programmatically 

    final RadioButton[] mbutton=new RadioButton[7]; 
    for(int l=0;l<7;l++){ 
     mbutton[l]=new RadioButton(this); 
     mbutton[l].setText("test"+l); 
     mRadioGroup.addView(mbutton[l]); 

    } 


// LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
      // RadioGroup.LayoutParams.WRAP_CONTENT, 
      // RadioGroup.LayoutParams.WRAP_CONTENT); 
    // mRadioGroup.addView(newRadioButton); 






    tv_name.setText((CharSequence) list_name.get(i)); 
    tv_name.setTextColor(color_blue); 
    tv_name.setTextSize(30); 
    tv_name.setTypeface(Typeface.DEFAULT_BOLD); 
    tv_name.setWidth(150); 

    btn_check.setLayoutParams(new LayoutParams(30, 30)); 
    btn_check.setBackgroundResource(R.drawable.small_checkbox_unchecked); 

    img_line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2)); 
    img_line.setBackgroundResource(R.drawable.separater_line); 

    table_row.addView(tv_name); 
    table_row.addView(btn_check); 
    table_row.addView(mRadioGroup); 
    table.addView(table_row); 
    table.addView(img_line); 
//table.addView(mRadioGroup); 

    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) { 
      for(int i=0; i<mRadioGroup.getChildCount(); i++) { 
       RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i); 
       int t=table.indexOfChild(table_row); 
       System.out.println(t); 
       if(btn.getId() == checkedId) { 
         String text = btn.getText().toString(); 
         // do something with text 
         Log.d(text," event1"); 
         return; 
       } 
      } 
     } 
    }); 



    } 

    } 
    } 
+0

什麼在您的代碼中不起作用?請包括有關您收到的錯誤的詳細信息(logCat有例外) – Guillaume

回答

0

爲什麼不使用ListView?代碼中沒有任何東西不能通過列表視圖來完成? 您可以通過設置自定義適配器來添加布局。

相關問題