Android 动态添加的按钮eventListener
我有一些代码,需要获取数据的ArrayList(从SQLite数据库返回)并通过下面的代码将其转换为表。我想知道的是,我将如何向动态添加到表中的按钮添加clickListener?基本上,它会将行中一列的值添加到我在别处访问的SharedReference变量中 如果需要更多信息,请告诉我,但我认为这是有道理的Android 动态添加的按钮eventListener,android,database,sqlite,dynamic,event-listener,Android,Database,Sqlite,Dynamic,Event Listener,我有一些代码,需要获取数据的ArrayList(从SQLite数据库返回)并通过下面的代码将其转换为表。我想知道的是,我将如何向动态添加到表中的按钮添加clickListener?基本上,它会将行中一列的值添加到我在别处访问的SharedReference变量中 如果需要更多信息,请告诉我,但我认为这是有道理的 DatabaseHandler db = new DatabaseHandler(TabFragment3.this.getActivity()); List<FoodPoints
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);
}
DatabaseHandler db=newdatabasehandler(TabFragment3.this.getActivity());
List foodpoints=db.getAllFoodPoints();
对于(FoodPoints fp:FoodPoints){
字符串listFood=fp.getFood();
字符串listPoints=Integer.toString(fp.getPoints());
字符串listDate=fp.getDate();
listDate=listDate.substring(0,12);
insertRow(tablePoints、listFood、listPoints、listDate);
//字符串log=“ID:”+fp.getID()+”,食物:“+fp.getFood()+”,点:“+fp.getPoints()+”,日期:“+fp.getDate()”;
//Log.d(“食物点”,Log);
}
私有void insertRow(TableLayout tablePoints、String tblFoodName、String tblFoodPoints、String tblFoodDate){
final TableRow newrow=新TableRow(当前活动);
addPlusButtonPointsTable(新行);
addTexttoRowswithValues(新行、tblFoodName、3);
addTexttoRowswithValues(新行、tblFoodPoints、17);
addTexttoRowswithValues(新行、tblFoodDate、17);
tablePoints.addView(newrow);
}
...
私有void addPlusButtonPointsTable(TableRow newrow){
按钮plusButton=新按钮(currentActivity);
//plusButton.setBackgroundColor(R.可拉深);
plusButton.setText(“+”);
plusButton.setMinimumWidth(1);
plusButton.设置最小高度(1);
plusButton.setTextSize(14);
addView(plusButton);
}
在您的addPlusButtonPointsTable()方法中添加以下行:
plusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do what you want
}
});
您可以修改addPlusButtonPointsTable()
方法,以便在单击按钮时(我想这就是您想要的吗?)也获取要存储在首选项中的列的值(例如食品名称),如下所示:
private void addPlusButtonPointsTable(TableRow newrow,String foodName){
// ...
//将数据设置为按钮的标记
plusButton.setTag(foodName);
plusButton.setOnClickListener(mListener);
// ...
}
此方法的调用方式如下:
addPlusButtonPointsTable(newrow,tblFoodName);
mListener
是这样的:
OnClickListener mListener=new OnClickListener(){
@凌驾
公共void onClick(视图v){
String foodName=(String)v.getTag();
//存储值。
}
}
我还建议您在将视图添加到TableRow
和TableRow
时使用适当的LayoutParams
将视图添加到表格行时
:
newrow.addView(plusButton,newtablerow.LayoutParams(TableRow.LayoutParams.MATCH_父,TableRow.LayoutParams.WRAP_内容));
将表格行
添加到表格布局
时:
tablePoints.addView(newrow,newtablelayout.LayoutParams(Tablelayout.LayoutParams.MATCH_父,Tablelayout.LayoutParams.WRAP_内容));
如果您支持2.2以下的版本,请使用FILL\u PARENT
而不是MATCH\u PARENT
,这是在我将按钮添加到表格行之前?我有一种感觉,这很简单,但我不能100%确定它是否是。当你这样做的时候,只要它在new按钮()下面就行了
call。请问您在向TableRow和TableLayout添加视图时使用正确的LayoutParams是什么意思?啊,好的,谢谢您-这可能是我在向表添加行时遇到格式问题的原因。