我有代碼,我需要從數據的ArrayList(從SQLite數據庫返回),並通過下面的代碼將其轉換爲表。我想知道的是,我會如何將clickListener添加到動態添加到表中的按鈕上?基本上,它會將該行中某列的值添加到我在別處訪問的SharedPreference變量中。動態添加按鈕eventListener
請讓我知道,如果需要更多的信息,但我認爲這是有道理的。
DatabaseHandler db = new DatabaseHandler(TabFragment3.this.getActivity());
List<FoodPoints> foodpoints = db.getAllFoodPoints();
for (FoodPoints fp : foodpoints) {
String listFood = fp.getFood();
String listPoints = Integer.toString(fp.getPoints());
String listDate = fp.getDate();
listDate = listDate.substring(0, 12);
insertRow(tablePoints, listFood, listPoints, listDate);
// String log = "ID: " + fp.getID() + ", Food: " + fp.getFood() + ", Points: " + fp.getPoints() + ", Date: " + fp.getDate();
// Log.d("FoodPoints", log);
}
private void insertRow(TableLayout tablePoints, String tblFoodName, String tblFoodPoints, String tblFoodDate) {
final TableRow newrow = new TableRow(currentActivity);
addPlusButtonPointsTable(newrow);
addTexttoRowswithValues(newrow, tblFoodName, 3);
addTexttoRowswithValues(newrow, tblFoodPoints, 17);
addTexttoRowswithValues(newrow, tblFoodDate, 17);
tablePoints.addView(newrow);
}
...
private void addPlusButtonPointsTable(TableRow newrow) {
Button plusButton = new Button(currentActivity);
//plusButton.setBackgroundColor(R.drawable.);
plusButton.setText("+");
plusButton.setMinimumWidth(1);
plusButton.setMinimumHeight(1);
plusButton.setTextSize(14);
newrow.addView(plusButton);
}
請問在將視圖添加到TableRow和TableLayout時,通過使用正確的LayoutParams是什麼意思? – mattdonders
@mattdonders看到我編輯的答案。 – Luksprog
啊好吧謝謝 - 這可能是爲什麼我添加行到我的表格時遇到格式問題。 – mattdonders