基本上,我一直在尝试获取我单击的行的索引号,这是一个代码生成的Table,因此我无法将ID放入Xml,而且我不确定是否将SetTag放在哪里我在一些问题中看到的。也许有一种更简单的方法。 (OnClick函数正在我创建的TableLayout的每个TableRow内使用TextView) 这是试图做到这一点的代码:
View.OnClickListener myAddHandler = new View.OnClickListener() {
public void onClick(View v) {
TableRow row = (TableRow) v.getParent();
int index=mTableLayout.indexOfChild(row);
addedcode = data[index].productCode;
addedcant = data[index].invoiceNumber;
Intent switchIntent = new Intent(AddManager.this,NavManager.class);
startActivity(switchIntent);
}
};
这是生成表格的代码:
public void loadData(InvoiceData[] data) {
int leftRowMargin=0;
int topRowMargin=0;
int rightRowMargin=0;
int bottomRowMargin = 0;
int textSize = 0, smallTextSize =0, mediumTextSize = 0;
textSize = (int) getResources().getDimension(R.dimen.font_size_verysmall);
smallTextSize = (int) getResources().getDimension(R.dimen.font_size_small);
mediumTextSize = (int) getResources().getDimension(R.dimen.font_size_medium);
DecimalFormat decimalFormat = new DecimalFormat("0.00");
int rows = data.length;
getSupportActionBar().setTitle("Productos (" + String.valueOf(rows) + ")");
TextView textSpacer = null;
mTableLayout.removeAllViews();
// -1 means heading row
for(int i = -1; i < rows; i ++) {
InvoiceData row = null;
if (i > -1)
row = data[i];
else {
textSpacer = new TextView(this);
textSpacer.setText("");
}
// data columns
final TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv.setGravity(Gravity.LEFT);
tv.setPadding(5, 15, 0, 15);
if (i == -1) {
tv.setText("Cant");
tv.setBackgroundColor(Color.parseColor("#f0f0f0"));
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv.setBackgroundColor(Color.parseColor("#f8f8f8"));
tv.setText(String.valueOf(row.invoiceNumber));
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
final TextView tv2 = new TextView(this);
if (i == -1) {
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.MATCH_PARENT));
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
tv2.setGravity(Gravity.LEFT
);
tv2.setPadding(5, 15, 0, 15);
if (i == -1) {
tv2.setText("Agregar");
tv2.setBackgroundColor(Color.parseColor("#f7f7f7"));
}else {
tv2.setBackgroundColor(Color.parseColor("#ffffff"));
tv2.setTextColor(Color.parseColor("#000000"));
tv2.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
tv2.setCompoundDrawablesWithIntrinsicBounds(0, 0,0,R.drawable.cart);
tv2.setOnClickListener(myAddHandler);
}
final LinearLayout layCustomer = new LinearLayout(this);
layCustomer.setOrientation(LinearLayout.VERTICAL);
layCustomer.setPadding(0, 10, 0, 10);
layCustomer.setBackgroundColor(Color.parseColor("#f8f8f8"));
final TextView tv3 = new TextView(this);
if (i == -1) {
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv3.setPadding(5, 5, 0, 5);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv3.setPadding(5, 0, 0, 5);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
tv3.setGravity(Gravity.TOP);
if (i == -1) {
tv3.setText("Productos");
tv3.setBackgroundColor(Color.parseColor("#f0f0f0"));
} else {
tv3.setBackgroundColor(Color.parseColor("#f8f8f8"));
tv3.setTextColor(Color.parseColor("#000000"));
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
tv3.setText(row.productName);
}
layCustomer.addView(tv3);
if (i > -1) {
final TextView tv3b = new TextView(this);
tv3b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv3b.setGravity(Gravity.RIGHT);
tv3b.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
tv3b.setPadding(5, 1, 0, 5);
tv3b.setTextColor(Color.parseColor("#aaaaaa"));
tv3b.setBackgroundColor(Color.parseColor("#f8f8f8"));
tv3b.setText(row.productCode);
layCustomer.addView(tv3b);
}
final LinearLayout layAmounts = new LinearLayout(this);
layAmounts.setOrientation(LinearLayout.VERTICAL);
layAmounts.setGravity(Gravity.LEFT);
layAmounts.setPadding(0, 10, 0, 10);
layAmounts.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
final TextView tv4 = new TextView(this);
if (i == -1) {
tv4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv4.setPadding(5, 5, 1, 5);
layAmounts.setBackgroundColor(Color.parseColor("#f7f7f7"));
} else {
tv4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4.setPadding(5, 0, 1, 5);
layAmounts.setBackgroundColor(Color.parseColor("#ffffff"));
}
tv4.setGravity(Gravity.LEFT);
if (i == -1) {
tv4.setText("Precio");
tv4.setBackgroundColor(Color.parseColor("#f7f7f7"));
tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv4.setBackgroundColor(Color.parseColor("#ffffff"));
tv4.setTextColor(Color.parseColor("#000000"));
tv4.setText(row.precio);
tv4.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}
layAmounts.addView(tv4);
if (i > -1) {
final TextView tv4b = new TextView(this);
tv4b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tv4b.setGravity(Gravity.LEFT);
tv4b.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
tv4b.setPadding(2, 2, 1, 5);
tv4b.setTextColor(Color.parseColor("#00afff"));
tv4b.setBackgroundColor(Color.parseColor("#ffffff"));
}
// add table row
final TableRow tr = new TableRow(this);
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
tr.setPadding(0,0,0,0);
tr.setLayoutParams(trParams);
int p=0;
tr.setTag(p);
tr.addView(tv);
tr.addView(layCustomer);
tr.addView(layAmounts);
tr.addView(tv2);
p++;
mTableLayout.addView(tr, trParams);
if (i > -1) {
final TableRow trSep = new TableRow(this);
TableLayout.LayoutParams trParamsSep = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
trParamsSep.setMargins(leftRowMargin, topRowMargin, rightRowMargin, bottomRowMargin);
trSep.setLayoutParams(trParamsSep);
TextView tvSep = new TextView(this);
TableRow.LayoutParams tvSepLay = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
tvSepLay.span = 4;
tvSep.setLayoutParams(tvSepLay);
tvSep.setBackgroundColor(Color.parseColor("#d9d9d9"));
tvSep.setHeight(1);
trSep.addView(tvSep);
mTableLayout.addView(trSep, trParamsSep);
}
}
}
您不断看到的数据是一个包含五个元素的数组:id,发票编号,产品名称,产品代码和价格。我真的需要我在这里待了两天的帮助:(
答案 0 :(得分:1)
传递给处理程序的View v
方法的onClick()
参数是被单击的视图。您可以结合使用setTag()
和getTag()
来存储一些额外的信息。
在loadData()
内部,您可以在其中分配点击侦听器:
tv2.setOnClickListener(myAddHandler);
您可以将tv2
的标签设置为具有索引:
tv2.setTag(i);
tv2.setOnClickListener(myAddHandler);
现在,在点击侦听器内部,您可以使用getTag()
检索索引:
public void onClick(View v) {
int index = (int) v.getTag();
// ...
}