我有代码,我需要获取数据的 ArrayList(从 SQLite 数据库返回)并通过下面的代码将其转换为表。我想知道的是如何将 clickListener 添加到我动态添加到表中的按钮中?基本上,它会将行中某一列的值添加到我在其他地方访问的 SharedPreference 变量中。
如果需要更多信息,请告诉我,但我认为这是有道理的。
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);
}