【问题标题】:Dynamically Added Button eventListener动态添加按钮事件监听器
【发布时间】:2012-10-10 02:41:02
【问题描述】:

我有代码,我需要获取数据的 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);
}

【问题讨论】:

    标签: android database sqlite dynamic event-listener


    【解决方案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.
         }
    }
    

    我还建议您在将视图添加到TableRow 和将TableRow 添加到TableLayout 时使用正确的LayoutParams

    将视图添加到TableRow时:

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

    TableRow 添加到TableLayout 时:

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

    如果您支持低于 2.2 的版本,请使用 FILL_PARENT 而不是 MATCH_PARENT

    【讨论】:

    • 请问您在向 TableRow 和 TableLayout 添加视图时使用正确的 LayoutParams 是什么意思?
    • 嗯,好的,谢谢 - 这可能是我在向表中添加行时遇到格式问题的原因。
    【解决方案2】:

    在你的 addPlusButtonPointsTable() 方法中添加这一行:

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

    【讨论】:

    • 这是在我将 Button 添加到 TableRow 之前?我有一种感觉,这很容易,但我不确定是否是 100%。
    • 只要在new Button()调用下方,什么时候做都没有关系。
    猜你喜欢
    • 2016-05-06
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-13
    • 2021-08-15
    相关资源
    最近更新 更多