2017-07-27 93 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

它是如何解决它通过点击我的行? –