[英]Is there a way to change the filename I am calling, so that I don't need all these if/else statements?
I am currently developing a java XML based app using Android Studio, am currently messing around with image and list views, I'd like to change the image based on the user's selection and have got the app to work as intended, however, I do not like how many if statements it uses, I have looked all over the web for a way to change the R.drawable.x where x is a different filename depending on users selection, however, haven't come across anything as of yet, please find below my complete java and XML code.我目前正在使用 Android Studio 开发一个基于 java XML 的应用程序,目前正在处理图像和列表视图,我想根据用户的选择更改图像并让应用程序按预期工作,但是,我这样做不像它使用了多少 if 语句,我在 web 中查找了一种更改 R.drawable.x 的方法,其中 x 是一个不同的文件名,具体取决于用户的选择,但是,到目前为止,还没有遇到任何问题,请在下面找到我完整的 java 和 XML 代码。
I would like to know what the best way to do this would be?我想知道最好的方法是什么? Thank you in advance!
先感谢您!
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] fruits = {"Apple", "Watermelon", "Grapes", "Banana"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView mListView = (ListView) findViewById(R.id.fruitslist);
final ImageView mImageView = (ImageView) findViewById(R.id.imageView);
ArrayAdapter<String> aAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fruits);
mListView.setAdapter(aAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
String fruit = fruits[position];
Toast.makeText(MainActivity.this, "Selected :" + " " + fruit + ", " + position, Toast.LENGTH_SHORT).show();
if (position == 0) {
mImageView.setImageResource(R.drawable.apple);
} else if (position == 1) {
mImageView.setImageResource(R.drawable.watermelon);
} else if (position == 2) {
mImageView.setImageResource(R.drawable.grapes);
} else {
mImageView.setImageResource(R.drawable.banana);
}
mImageView.setVisibility(View.VISIBLE);
}
});
}
}
activity_main.xml activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/fruitslist"
android:layout_width="match_parent"
android:layout_height="251dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="390dp"
android:visibility="invisible"
app:srcCompat="@drawable/apple" />
</LinearLayout>
Note that I have set the image to apple as default, this was simply so I had a placeholder to see where the image view was located graphically, it's set to invisible on start and not made visible until the user's selection has been made.请注意,我已将图像默认设置为苹果,这只是为了让我有一个占位符来查看图像视图的图形位置,它在开始时设置为不可见,并且在用户做出选择之前不可见。
a lot of work for you for this purpose为此,你需要做很多工作
first of all create some custom class
holding your fruit objects with names and image resource首先创建一些自定义的
class
保存带有名称和图像资源的水果对象
public class Fruit{
public String name;
public int imageRes;
public Fruit(String name, int imageRes){
this.name = name;
this.imageRes = imageRes;
}
public String toString(){
return name;
}
}
next declare your fruits
array like this接下来像这样声明你的
fruits
数组
private Fruit[] fruits = {
new Fruit("Apple", R.drawable.apple),
... rest of fruits
now your adapter need to handle Fruit
class (adapter is generic), so declare it like below现在你的适配器需要处理
Fruit
class(适配器是通用的),所以声明如下
ArrayAdapter<Fruit> aAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, fruits);
Fruit
isn't usual String
so ArrayAdapter
will use toString
method to get text to print (every object have toString
method, String
also, returns itself) Fruit
不是通常的String
,所以ArrayAdapter
将使用toString
方法获取要打印的文本(每个 object 都有toString
方法, String
也返回自身)
now in your onItemClick
method just get proper object by position现在在你的
onItemClick
方法中,只需通过 position 获得正确的 object
Fruit pickedFruit = fruits[position];
mImageView.setImageResource(picketFruit.imageRes);
mImageView.setVisibility(View.VISIBLE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.