让它工作起来有点艰难,这对我来说是一个理解的部分......
如果我有一个包含文本“[item] is [color]”的微调器项目,并且在选择它时,我希望它填充一个...... tablerow 或类似的东西(或只是一个相对布局)......按钮,会有两个按钮,[item] 和 [color],一个在另一个上。
public void onItemSelected(AdapterView<?> parentview, View arg1, int position, long id)
{
final TableLayout t1 = (TableLayout)findViewById(R.id.button_items);
final TableRow tr = new TableRow(t1.getContext());
ArrayList<String> words = Create_ArrayList(R.raw.titles);
// Create_ArrayList 只是解析已知的单词,如 [item] 和 [color] 项目,并将它们放入一个数组中......以便稍后进行枚举。
String sentence = (String) spin.getSelectedItem();
if(sentence.contains("[item]"))
{
String line = words.get(1);
ArrayList<String> x = getParts(line);
//arraylist 此时应该只是 [item] 和 [color] ...
Toast.makeText(getBaseContext(), Integer.toString(x.size()), Toast.LENGTH_SHORT).show();
for(int i = 0; i<x.size(); i++)
{
Toast.makeText(getBaseContext(), x.get(i), Toast.LENGTH_LONG).show();
Button btn = new Button(tr.getContext());
btn.setText(x.get(i));
btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tr.addView(btn);
t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}
}
但我不断...
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
并且按钮不显示......应用程序只是崩溃......进入一座山。
非常感谢您的帮助...谢谢大家!