2012-05-09 88 views
3

我使用下面的代碼動態地創建一個帶有內容的TableRow。它運作良好,但我希望獲得TableRow中的值。以下是示例代碼(我從Google獲得),它有TableRow中的兩個文本值。當我在任何位置點擊TableRow時,它會給出相應的值TableRow(我希望類似於ListView)。如何從動態創建的Android TableRow中獲取值?

All_CustomsActivity.java

public class All_CustomsActivity extends Activity { 

    String companies[] = { "Google", "Windows", "iPhone", "Nokia", "Samsung", 
      "Google", "Windows", "iPhone", "Nokia", "Samsung", "Google", 
      "Windows", "iPhone", "Nokia", "Samsung" }; 
    String os[] = { "Android", "Mango", "iOS", "Symbian", "Bada", "Android", 
      "Mango", "iOS", "Symbian", "Bada", "Android", "Mango", "iOS", 
      "Symbian", "Bada" }; 

    TableLayout tl; 
    TableRow tr; 
    TableRow mTable = null; 
    TextView companyTV, valueTV; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tl = (TableLayout) findViewById(R.id.maintable); 
     // addHeaders(); 
     addData(); 

    } 

    /** This function add the headers to the table **/ 
    public void addHeaders() { 

     /** Create a TableRow dynamically **/ 
     tr = new TableRow(this); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     /** Creating a TextView to add to the row **/ 
     TextView companyTV = new TextView(this); 
     companyTV.setText("Companies"); 
     companyTV.setTextColor(Color.GRAY); 
     companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     companyTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     companyTV.setPadding(5, 5, 5, 0); 
     tr.addView(companyTV); // Adding textView to tablerow. 

     /** Creating another textview **/ 
     TextView valueTV = new TextView(this); 
     valueTV.setText("Operating Systems"); 
     valueTV.setTextColor(Color.GRAY); 
     valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     valueTV.setPadding(5, 5, 5, 0); 
     valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     tr.addView(valueTV); // Adding textView to tablerow. 

     // Add the TableRow to the TableLayout 
     tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     // we are adding two textviews for the divider because we have two 
     // columns 
     tr = new TableRow(this); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

     /** Creating another textview **/ 
     TextView divider = new TextView(this); 
     divider.setText("-----------------"); 
     divider.setTextColor(Color.GREEN); 
     divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     divider.setPadding(5, 0, 0, 0); 
     divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     tr.addView(divider); // Adding textView to tablerow. 

     TextView divider2 = new TextView(this); 
     divider2.setText("-------------------------"); 
     divider2.setTextColor(Color.GREEN); 
     divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
     divider2.setPadding(5, 0, 0, 0); 
     divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
     tr.addView(divider2); // Adding textView to tablerow. 

     // Add the TableRow to the TableLayout 
     tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
    } 

    /** This function add the data to the table **/ 
    public void addData() { 

     for (int i = 0; i < companies.length; i++) { 
      /** Create a TableRow dynamically **/ 
      tr = new TableRow(this); 
      tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      // ImageView im = new ImageView(this); 
      // im.setBackgroundResource(R.drawable.sample_image); 
      // im.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
      // LayoutParams.WRAP_CONTENT)); 
      // tr.addView(im); 
      /** Creating a TextView to add to the row **/ 
      companyTV = new TextView(this); 
      companyTV.setText(companies[i]); 
      companyTV.setTextColor(Color.RED); 
      companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
      companyTV.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      companyTV.setPadding(5, 5, 5, 5); 
      tr.addView(companyTV); // Adding textView to tablerow. 

      /** Creating another textview **/ 
      valueTV = new TextView(this); 
      valueTV.setText(os[i]); 
      valueTV.setTextColor(Color.GREEN); 
      valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      valueTV.setPadding(5, 5, 5, 5); 
      valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 
      tr.addView(valueTV); // Adding textView to tablerow. 

      // Add the TableRow to the TableLayout 
      tl.addView(tr, new TableLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

      // tr.setOnClickListener(new View.OnClickListener() { 
      // public void onClick(View view) { 
      // view.setBackgroundColor(Color.DKGRAY); 
      // } 
      // }); 
      // 
      // tr.setOnLongClickListener(new View.OnLongClickListener() { 
      // public boolean onLongClick(View v) { 
      // mTable = (TableRow) v; // assign selected TableRow gobally 
      // openContextMenu(v); 
      // return true; 
      // } 
      // }); 

     } 
    } 

    // @Override 
    // public void onCreateContextMenu(ContextMenu menu, View v, 
    // ContextMenu.ContextMenuInfo menuInfo) { 
    // super.onCreateContextMenu(menu, v, menuInfo); 
    // menu.add(0, v.getId(), 0, "Do YourStuff"); 
    // 
    // } 
    // 
    // @Override 
    // public boolean onContextItemSelected(MenuItem item) { 
    // int ccount = (mTable).getChildCount(); 
    // String[] str = new String[ccount]; 
    // for (int i = 0; i < ccount; i++) { 
    // TextView tv = (TextView) (((TableRow) mTable)).getChildAt(i); 
    // str[i] = tv.getText().toString(); // set selected text data into the 
    // // String array 
    // } 
    // Toast.makeText(All_CustomsActivity.this, Arrays.toString(str), 2) 
    // .show(); 
    // return true; 
    // } 

} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scrollbars="none" > 

     <TableLayout 
      android:id="@+id/maintable" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:stretchColumns="0,1" > 
     </TableLayout> 
    </ScrollView> 

</LinearLayout> 

在上面一些行被註釋掉的代碼,這些線是什麼,我已經試圖讓行值,但它失敗了。誰能幫我這個?

回答

12

我想你正在談論獲取這些值在TableRow點擊。如果是這樣的話,你可以監聽器添加到您的TableRow並使用getChildAt獲得兩個TextViews的保持和獲取數據:

//... 
tr.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     TableRow t = (TableRow) view; 
     TextView firstTextView = (TextView) t.getChildAt(0); 
     TextView secondTextView = (TextView) t.getChildAt(1); 
     String firstText = firstTextView.getText().toString(); 
     String secondText = secondTextView.getText().toString(); 
    } 
}); 
//... 
+0

感謝很多:) – Aerrow

+0

真的幫了我,太感謝你了! – ricardo

1

要想從TextView的數據第一ü需要從識別其父ü需要得到你的TextView孩子的位置,然後ü可以從它那裏得到的數據...這裏是一個樣本

String myText; 
((TextView) findViewById(R.id.tv)).setOnClickListener(new OnClickListener() { 
    @Override 
public void onClick(View v) {       
    TableRow tablerow = (TableRow)v.getParent(); 
    TextView items = (TextView) tablerow.getChildAt(2); 

    myText = items.getText().toString(); 
    } 
}); 
相關問題