2017-07-27 92 views
0

加入表格佈局動態添加可點擊的TextView到餐桌布局點擊TextView的動態

TextView Delete = new TextView(this); 
Delete.setText("Delete"); 
Delete.setClickable(true); 
Delete.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v) { 
     TransDelete(ac); 
    } 
}); 

這是動態添加我行。

public void TransDelete(final String TransID) { 
    class TransDeleteClass extends AsyncTask<String, Void, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected void onPostExecute(String httpResponseMsg) { 
      super.onPostExecute(httpResponseMsg); 
      Toast.makeText(ViewTransactions.this, httpResponseMsg.toString(), Toast.LENGTH_LONG).show(); 
      finish(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // Sending Trans id. 
      hashMap.put("TransID", params[0]); 
      finalResult = httpParse.postRequest(hashMap, HttpUrlDeleteRecord); 

      return finalResult; 
     } 
    } 

    TransDeleteClass TransDeleteClass = new TransDeleteClass(); 
    TransDeleteClass.execute(TransID); 
} 

這是我的onclick函數。我試圖在啓動模擬器時點擊我的textview,但textview沒有響應onclick。我見過其他論壇,但因爲我需要通過我的TransID,這不是我在屏幕上顯示的ID。例如,我嘗試過濾數據,儘管屏幕顯示的是按數字順序添加的數字,但我並不使用數據庫。

編輯:

<ScrollView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" 
     android:scrollbars="none" 
     android:layout_below="@+id/textView1"> 
     <TableLayout android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:stretchColumns="1,1,1" 
      android:id="@+id/maintable" > 

     </TableLayout> 
    </ScrollView> 

這是我的XML頁面,但該表完全彈出。
我選擇了動態添加表格佈局的標題。

void addHeader(){ 
    /** Create a TableRow dynamically **/ 
    tr = new TableRow(this); 

    /** Creating a TextView to add to the row **/ 
    label = new TextView(this); 
    label.setText("ID"); 
    label.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
       TableRow.LayoutParams.WRAP_CONTENT)); 
    label.setPadding(5, 5, 5, 5); 
    label.setBackgroundColor(Color.RED); 
    LinearLayout Ll = new LinearLayout(this); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
       TableRow.LayoutParams.WRAP_CONTENT); 
    params.setMargins(5, 5, 5, 5); 
    Ll.setPadding(10, 5, 5, 5); 
    Ll.addView(label,params); 
    tr.addView((View)Ll); // Adding textView to tablerow. 

由於添加更多代碼需要更多描述,因此只是表格的標題部分。

enter image description here

+0

您是否向您的TextView添加了'android:clickable =「true」'? – cjnash

+0

setOnClickListener將您的視圖設置爲可在內部單擊 – crgarridos

+0

如何將texview附加到表中? – crgarridos

回答

0

final TextView[] myTextViews = new TextView[N]; //創建一個空的陣列;

for (int i = 0; i < N; i++) { 
    // create a new textview 
    final TextView rowTextView = new TextView(this); 

    // set some properties of rowTextView or something 
    rowTextView.setText("This is row #" + i); 

    // add the textview to the tablelayout 
    yourtable.addView(rowTextView); 

    // save a reference to the textview for later 
    myTextViews[i] = rowTextView; 
} 
+0

它是如何解決它通過點擊我的行? –