mattdonders
mattdonders

Reputation: 1356

Dynamically Added Button eventListener

I have code where I need to take an ArrayList of data (returned from a SQLite database) and convert it to a table via the code below. What I was wondering is how I would go about adding a clickListener to the button I add dynamically to the table? Basically it would add the value from one of the columns in the row into a SharedPreference variable that I access elsewhere.

Please let me know if more information is needed, but I think that makes sense.

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);
}

Upvotes: 1

Views: 124

Answers (2)

user
user

Reputation: 87064

You could modify the addPlusButtonPointsTable() method to also take the value of the column(for example the food name) that you would like to store in the preferences when the Button is clicked clicked(I think this is what you want no?) like this:

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

This method will be called like this:

addPlusButtonPointsTable(newrow, tblFoodName);

The mListener is like this:

OnClickListener mListener = new OnCLickListener() {

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

I would also recommend that you use the proper LayoutParams when adding the views to the TableRow and the TableRow to the TableLayout.

When adding the views to the TableRow:

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

When adding the TableRow to the TableLayout:

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

If you support versions below 2.2 use FILL_PARENT instead of MATCH_PARENT.

Upvotes: 1

WarrenFaith
WarrenFaith

Reputation: 57702

In your addPlusButtonPointsTable() method add this line:

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

Upvotes: 1

Related Questions