动态地将可点击的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功能。我在启动mulator时尝试单击我的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.
只是表格标题的一部分,因为添加更多代码需要更多描述。
答案 0 :(得分: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;
}