我有一个我正在处理的小应用程序,它显示了一个怪物列表。你可以点击每个怪物来查看关于那个怪物的更多信息。将数据从一个活动动态添加到另一个活动
该列表位于我的xml布局文件中的TableLayout中。
但是看着代码,很容易看到事物如何失控,我添加的怪物越多。
我想知道,如果有更好的方法来确定哪个怪物被点击以及如何获得从怪物视图列表传递到怪物信息视图所需的信息。
这是我的代码到目前为止。它工作正常,但我知道这不是很好,因为我必须为每个添加到列表中的新怪物添加一个新的case语句。
public void AddNewView(View view) {
//get selected button view
switch(view.getId())
{
//get textview & imageview monsters from xml
//get monster picture and monster info from xml
case R.id.showMonsterButton1:
iv = (ImageView) findViewById(R.id.monster1_icon);
tv = (TextView) findViewById(R.id.monster1_info);
ShowMonsterInfoView(tv, iv);
break;
case R.id.showMonsterButton2:
iv = (ImageView) findViewById(R.id.monster2_icon);
tv = (TextView) findViewById(R.id.monster2_info);
ShowMonsterInfoView(tv, iv);
break;
case R.id.showMonsterButton3:
iv = (ImageView) findViewById(R.id.monster3_icon);
tv = (TextView) findViewById(R.id.monster3_info);
ShowMonsterInfoView(tv, iv);
break;
case R.id.showMonsterButton4:
iv = (ImageView) findViewById(R.id.monster4_icon);
tv = (TextView) findViewById(R.id.monster4_info);
ShowMonsterInfoView(tv, iv);
break;
}
}
public void ShowMonsterInfoView(TextView tv, ImageView iv) {
Intent intent = new Intent(this, DisplayMonsterInfo.class);
String text_tag = tv.getTag().toString();
String image_tag = iv.getTag().toString();
Bundle extras = new Bundle();
extras.putString("name", text_tag);
extras.putString("avatar", image_tag);
intent.putExtras(extras);
startActivity(intent);
}
谢谢你让我知道我离开了那些信息。我编辑了原始问题以包含它。怪物列表位于TableLayout中的XML文件中。 – SkyeBoniwell