2012-10-19 61 views
1

我有代碼,我需要從數據的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); 
} 

回答

1

您可以修改addPlusButtonPointsTable()方法也採取列的值(例如食品名稱),你想存儲在當Button點擊點擊的偏好(我認爲這是你想要什麼沒有?)是這樣的:

private void addPlusButtonPointsTable(TableRow newrow, String foodName) { 
    // ... 
    // set the data as the tag for the Button 
    plusButton.setTag(foodName); 
    plusButton.setOnClickListener(mListener); 
    // ... 
} 

這個方法將被稱爲像這樣:

addPlusButtonPointsTable(newrow, tblFoodName); 

mListener是這樣的:

OnClickListener mListener = new OnCLickListener() { 

    @Override 
    public void onClick(View v) { 
     String foodName = (String)v.getTag(); 
     // store the value. 
    } 
} 

我也建議你使用正確的LayoutParams添加欣賞到TableRowTableRowTableLayout

當添加欣賞到TableRow

newrow.addView(plusButton, new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 

加入當TableRowTableLayout

tablePoints.addView(newrow, new Tablelayout.LayoutParams(Tablelayout.LayoutParams.MATCH_PARENT, Tablelayout.LayoutParams.WRAP_CONTENT)); 

如果你支持低於2.2使用FILL_PARENT代替MATCH_PARENT版本。

+0

請問在將視圖添加到TableRow和TableLayout時,通過使用正確的LayoutParams是什麼意思? – mattdonders

+0

@mattdonders看到我編輯的答案。 – Luksprog

+0

啊好吧謝謝 - 這可能是爲什麼我添加行到我的表格時遇到格式問題。 – mattdonders

1

在你addPlusButtonPointsTable()方法添加此行:

plusButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // do what you want 
    } 
}); 
+0

這是在將按鈕添加到TableRow之前?我有一種感覺,那很容易,但我並不是100%確定它是否是這樣。 – mattdonders

+0

只要它低於'new Button()'調用時,這並不重要。 – WarrenFaith