Java 将((RelativeLayout)findViewById)添加到ArrayList<;相对寿命>;
我有以下代码:Java 将((RelativeLayout)findViewById)添加到ArrayList<;相对寿命>;,java,android,android-layout,dynamic,Java,Android,Android Layout,Dynamic,我有以下代码: public class MainActivity extends Activity implements OnClickListener { public ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>(); public ArrayList<Integer> drawableId = new ArrayList<Integer>
public class MainActivity extends Activity implements OnClickListener {
public ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>();
public ArrayList<Integer> drawableId = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
linear = (LinearLayout) findViewById(R.id.Rlayout);
scrool = (ScrollView) findViewById(R.id.scrool);
for (int i = 0; i < buttons.size(); i++) {
{
String buttonID = "Llayout" + i;
int resID = getResources().getIdentifier(buttonID, "id",
getPackageName());
//The problem is here
buttons.get(i).add((RelativeLayout) findViewById(resID));
buttons.get(i).setOnClickListener(this);
drawableId.add(getResources().getIdentifier("supa" + i, "drawable", getPackageName()));
}
}
}
@Override
public void onClick(View v) {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() == v.getId()) {
index = i;
Intent trimite = new Intent(MainActivity.this, RecipeView.class);
Bundle colet = new Bundle();
colet.putString("key", Content.RETETE[i]);
colet.putInt("keyimg", drawableId.get(i));
trimite.putExtras(colet);
startActivity(trimite);
break;
}
}
}
}
Java错误表明:add((RelativeLayout)未为RelativeLayout类型定义
add方法适用于ArrayList drawableId,在下面一行,但不适用于按钮。有人能帮我吗
以下是我的xml布局:
我用RelativeLayouts制作了按钮。每个RelativeLayouts按钮都有文本和文本旁边的图标。所有RelativeLayouts按钮都在一个线性布局中,以便它们彼此对齐
当RelativeLayouts按钮和ArrayList drawableId是简单的数组时,效果非常好,但我不想每次添加新的RelativeLayout按钮时都修改数组
有什么建议吗?没有add()
方法
buttons
是RelativeLayouts的数组列表。当您调用buttons.get(i)
时,会得到一个RelativeLayout对象。由于RelativeLayout没有add()
方法,因此会出现此错误
目前还不完全清楚您使用此代码的意图,但我假设您正在尝试将每个RelativeLayouts添加到按钮列表中
如果这是正确的,您只需要删除get(i)
调用,使该行成为
buttons.add((RelativeLayout) findViewById(resID));
试试这种方法,希望这能帮助你解决问题。
public class MainActivity extends Activity implements View.OnClickListener {
private ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>();
private ArrayList<Integer> drawableId = new ArrayList<Integer>();
private LinearLayout linear;
private ScrollView scrool;
private int index;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
linear = (LinearLayout) findViewById(R.id.Rlayout);
scrool = (ScrollView) findViewById(R.id.scrool);
for (int i = 0; i < linear.getChildCount(); i++) {
{
String buttonID = "Llayout" + i;
int resID = getResources().getIdentifier(buttonID, "id",getPackageName());
buttons.add((RelativeLayout)findViewById(resID));
buttons.get(i).setOnClickListener(this);
drawableId.add(getResources().getIdentifier("supa" + i, "drawable", getPackageName()));
}
}
}
@Override
public void onClick(View v) {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() == v.getId()) {
index = i;
Toast.makeText(MyActivity.this,String.valueOf(index),Toast.LENGTH_SHORT).show();
Intent trimite = new Intent(MainActivity.this, RecipeView.class);
Bundle colet = new Bundle();
colet.putString("key", Content.RETETE[i]);
colet.putInt("keyimg", drawableId.get(i));
trimite.putExtras(colet);
startActivity(trimite);
break;
}
}
}
}
public类MainActivity扩展活动实现View.OnClickListener{
私有ArrayList按钮=新建ArrayList();
私有ArrayList drawableId=新ArrayList();
私人线路线性布局;
私用scrool;
私有整数索引;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG\u保持屏幕打开);
线性=(线性布局)findViewById(R.id.Rlayout);
scrool=(ScrollView)findViewById(R.id.scrool);
对于(int i=0;i
正如我所说,每个RelativeLayout都是一个按钮。我正在尝试将相对布局buton添加到列表中,以便每个按钮都有一个从i到buttons的id。size()。您的方法没有为任何按钮提供id,因此我无法单击任何按钮。我已从同一个类中添加了onClick方法。我只想为每个按钮的相对布局添加id,就像我在下面用按钮做了一行一样。get(i).setOnClickListener.yes他们会这样做,并且使用您的方法错误会消失。我运行程序,但我无法单击任何按钮。这是我尝试使用的get(i).add.can你能批准它以便帮助其他人吗?我还有一个问题。在内容类中,我有一个名为RETETE的字符串,其中存储了一些Recipe。然后,我通过带有Recipe标题的按钮的索引id获取它们。我想知道我可以使用哪些其他方法来存储这些Recipe并将其格式化(如缩进、粗体、字体等)。因此,当我得到它们时,它们已经格式化。在字符串中,我无法真正格式化文本。
public class MainActivity extends Activity implements View.OnClickListener {
private ArrayList<RelativeLayout> buttons = new ArrayList<RelativeLayout>();
private ArrayList<Integer> drawableId = new ArrayList<Integer>();
private LinearLayout linear;
private ScrollView scrool;
private int index;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
linear = (LinearLayout) findViewById(R.id.Rlayout);
scrool = (ScrollView) findViewById(R.id.scrool);
for (int i = 0; i < linear.getChildCount(); i++) {
{
String buttonID = "Llayout" + i;
int resID = getResources().getIdentifier(buttonID, "id",getPackageName());
buttons.add((RelativeLayout)findViewById(resID));
buttons.get(i).setOnClickListener(this);
drawableId.add(getResources().getIdentifier("supa" + i, "drawable", getPackageName()));
}
}
}
@Override
public void onClick(View v) {
for (int i = 0; i < buttons.size(); i++) {
if (buttons.get(i).getId() == v.getId()) {
index = i;
Toast.makeText(MyActivity.this,String.valueOf(index),Toast.LENGTH_SHORT).show();
Intent trimite = new Intent(MainActivity.this, RecipeView.class);
Bundle colet = new Bundle();
colet.putString("key", Content.RETETE[i]);
colet.putInt("keyimg", drawableId.get(i));
trimite.putExtras(colet);
startActivity(trimite);
break;
}
}
}
}