gpt4 book ai didi

java - 使用从微调器中选择的项目填充 imageview

转载 作者:行者123 更新时间:2023-12-02 12:58:51 26 4
gpt4 key购买 nike

我希望能够在从微调器中选择一个项目时填充 ImageView !我尝试实现 onitemselectedlistener 但无法使代码正常工作,因此寻求帮助!

public class MainActivity extends AppCompatActivity {


Context context = this;
MediaPlayer mp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ArrayList<ItemData> list = new ArrayList<>();
list.add(new ItemData("Blue", R.drawable.blue));
list.add(new ItemData("Green", R.drawable.green));
list.add(new ItemData("Orange", R.drawable.orange));
list.add(new ItemData("Pink", R.drawable.pink));
list.add(new ItemData("Yellow", R.drawable.yel));

Spinner sp = (Spinner) findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this,
R.layout.spinner_layout, R.id.txt, list);
sp.setAdapter(adapter);

mp = MediaPlayer.create(context, R.raw.sound);


ImageView myView = (ImageView) findViewById(R.id.mainimage);


myView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, R.raw.sound);
}
mp.start();
} catch (Exception e) {
e.printStackTrace();
}

ImageView myView = (ImageView) findViewById(R.id.mainimage);
Animation a = new RotateAnimation(0.0f, 360.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
a.setInterpolator(new LinearInterpolator());
a.setRepeatCount(-1);
a.setDuration(500);
a.setFillAfter(true);
a.setRepeatCount(7);
myView.startAnimation(a);

}
});
}

}

public class ItemData {

String text;
Integer imageId;
public ItemData(String text, Integer imageId){
this.text=text;
this.imageId=imageId;
}

public String getText(){
return text;
}

public Integer getImageId(){
return imageId;
}

}

最佳答案

如果您想在单击某个项目时显示颜色..

    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
ItemData data = sp.getSelectedItem();
int res = data.getImageId(); //change Integer type to int if error exists
myView.setImageDrawable(getResources().getDrawable(res));
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

关于java - 使用从微调器中选择的项目填充 imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44355689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com