简体   繁体   English

动态添加和删除映射按钮到 TableLayout

[英]Dynamically adding and removing mapped buttons to a TableLayout

I'm struggling to find a good way of adding and removing clickable buttons in a TableLayout.我正在努力寻找一种在 TableLayout 中添加和删除可点击按钮的好方法。

So I currently have a HashMap that contains an integer and an object.所以我目前有一个包含整数和对象的 HashMap。 It updates as the user needs it too.它也会随着用户的需要而更新。 When the user presses the "add project" button, I would like it to go through the motions of adding a project to the HashMap and then update my TableLayout.当用户按下“添加项目”按钮时,我希望它完成将项目添加到 HashMap 的动作,然后更新我的 TableLayout。

I'd like to limit each row to 2 buttons.我想将每一行限制为 2 个按钮。 I noticed that before I can update the TableLayout (or any layout) with the new project added in, I have to remove the previous iteration.我注意到在我可以使用添加的新项目更新 TableLayout(或任何布局)之前,我必须删除以前的迭代。

I've tried numerous different ways to add and remove the buttons in a row but none of them seem to have worked.我尝试了许多不同的方法来连续添加和删除按钮,但似乎都没有奏效。

One example of what I did is:

int i = mProjectMap.size();
for(Map.Entry<Integer, Counter> entry : mProjectMap.entrySet()) { // This already has one entry before reaching this loop
    if(i % 2 == 0 || mLayoutProjects.getChildCount() == 0) {
        mTableRow = new TableRow(mMainContext);
        mLayoutProjects.addView(mTableRow);
    }

    mTableRow.addView(entry.getValue());
};

As for removing the views I've tried:
mLayoutProjects.removeAllViews();

and:
mLayoutProjects.removeViewsInLayout();

And many more.

What should happen is as follows:应该发生的情况如下:

1) User clicks "Add Project" button. 1) 用户点击“添加项目”按钮。 2) Project is populated with relevant information. 2) 项目填充有相关信息。 (done) 3) Project is added to mProjectMap (done) 4) mLayoutProjects has all containing views removed. (完成) 3) 项目被添加到 mProjectMap (完成) 4) mLayoutProjects 已删除所有包含视图。 5) if mLayoutProjects.getChildCount() equals 0 OR i % 2 equals 0 then: Create new row and add it to mLayoutProjects. 5) 如果 mLayoutProjects.getChildCount() 等于 0 或 i % 2 等于 0,则:创建新行并将其添加到 mLayoutProjects。 6) Add a project button to the row. 6) 在行中添加一个项目按钮。

Instead, When I press the "Add Project" button, that loop seems to add everything on the first iteration, but nothing shows on the screen in terms of buttons (I have a project counter which increments once).相反,当我按下“添加项目”按钮时,该循环似乎在第一次迭代中添加了所有内容,但屏幕上没有任何按钮显示(我有一个递增一次的项目计数器)。 I then press the button again and the app crashes.然后我再次按下按钮,应用程序崩溃了。

Update:更新:

I have fixed the issue with a different way.我已经用不同的方式解决了这个问题。 For those who stumble on this I shall include the solution for completeness:对于那些偶然发现的人,我将提供完整的解决方案:

So I changed everything to a LinearLayout and in the "addProjectButton" function I had the following:因此,我将所有内容都更改为 LinearLayout,并在“addProjectButton”函数中执行以下操作:

int i = mProjectMap.size();
for(Map.Entry<Integer, Counter> entry : mProjectMap.entrySet()) {
    if((entry.getKey() - 1) % 2 == 0 || mLayoutRow == null) {
        mLayoutRow = new LinearLayout(mMainContext);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        );
        mLayoutRow.setOrientation(LinearLayout.HORIZONTAL);
        mLayoutRow.setGravity(Gravity.CENTER);
        mLayoutRow.setLayoutParams(lp);
        mLayoutProjects.addView(mLayoutRow);
    }

    mLayoutRow.addView(entry.getValue());
}

Then, in my "removeProjectButton" function:然后,在我的“removeProjectButton”函数中:

for(Map.Entry<Integer, Counter> entry : mProjectMap.entrySet()) {
    if(entry.getValue().getParent() != null) {
        ((ViewGroup) entry.getValue().getParent().removeView(entry.getValue());
    }
}

mLayoutRow = null;

This seems to run perfectly fine without any problems... Though it could end up being too good to be true but only time will tell.这似乎运行得非常好,没有任何问题......虽然它最终可能好得令人难以置信,但只有时间会证明一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM