我想知道如何根据在微调器上选择的内容填充ListView。
我有一个用strings.xml中的字符串数组填充的微调器,如下所示:
<string-array name="genres">
<item>Action</item>
<item>Adventure</item>
<item>Animation</item>
<item>Comedy</item>
<item>Crime</item>
<item>Drama</item>
<item>Horror</item>
<item>Science Fiction</item>
<item>Thriller</item>
<item>Western</item>
</string-array>
我创建了其他数组,并且根据所选的电影类型,我想用这些数组填充listview:
<string-array name="actionMovie">
<item>Die Hard</item>
<item>The Dark Knight</item>
</string-array>
<string-array name="adventureMovie">
<item>The Goonies</item>
<item>Back to the Future</item>
</string-array>
我不确定如何处理这个问题。以下是我在主要活动中所拥有的一些片段。
Spinner spinGenre;
ListView lstViewMovies;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinGenre = (Spinner)findViewById(R.id.spinnerGenre);
lstViewMovies = (ListView) findViewById(R.id.listViewMovies);
我使用setOnItemSelectedListener来检索在此微调器中选择的值。仅仅是为了测试,我使用toast消息显示在微调器中选择了什么值,但是它运行不正常。这就是我的onItemSelected方法目前的样子,有人能指出我正确的方向使toast工作(出于测试目的),甚至告诉我如何让ListView用正确的字符串数组填充?
spinGenre.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position).toString();
if (item.toString() == "Action") {
Toast.makeText(MainActivity.this, "Action Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Adventure") {
Toast.makeText(MainActivity.this, "Adventure Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Animation") {
Toast.makeText(MainActivity.this, "Aanimation Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Comedy") {
Toast.makeText(MainActivity.this, "Comedy Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Crime") {
Toast.makeText(MainActivity.this, "Crime Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Drama") {
Toast.makeText(MainActivity.this, "Drama Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Horror") {
Toast.makeText(MainActivity.this, "Horror Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Science Fiction") {
Toast.makeText(MainActivity.this, "Science Fiction Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Thriller") {
Toast.makeText(MainActivity.this, "Thriller Genre Selected!",
Toast.LENGTH_SHORT).show();
} else if (item.toString() == "Western") {
Toast.makeText(MainActivity.this, "Western Genre Selected!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please select another genre!",
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
您可以在微调器上使用UITableView?
方法来获取选择了哪个位置的回调。在位置的基础上,您可以填充listview。
答案 1 :(得分:0)
我解决了自己的问题,只是想我会在这里发帖帮助任何人。
我的字符串数组与我的问题(位于strings.xml中)保持一致。
在我的onCreate方法中,我添加了以下内容: 呼吁听众:
spinGenre.setOnItemSelectedListener(this);
lstViewMovies.setOnItemClickListener(this);
然后我使用strings.xml文件中的字符串填充字符串数组:
String [] action;
String [] adventure;
String [] animation;
String [] comedy;
String [] crime;
String [] drama;
String [] horror;
String [] scifi;
String [] thriller;
String [] western;
action = getResources().getStringArray(R.array.actionMovie);
adventure = getResources().getStringArray(R.array.adventureMovie);
animation = getResources().getStringArray(R.array.animationMovie);
comedy = getResources().getStringArray(R.array.comedyMovie);
crime = getResources().getStringArray(R.array.crimeMovie);
drama = getResources().getStringArray(R.array.dramaMovie);
horror = getResources().getStringArray(R.array.horrorMovie);
scifi = getResources().getStringArray(R.array.scienceFictionMovie);
thriller = getResources().getStringArray(R.array.thrillerMovie);
western = getResources().getStringArray(R.array.westernMovie);
这是我onCreate的结束。我的新onItemSelected方法看起来像这样,它将使用strings.xml文件中的所需数组填充listview。
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
if (item.compareToIgnoreCase("Action") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, action);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Adventure") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, adventure);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Animation") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, animation);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Comedy") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, comedy);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Crime") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, crime);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Drama") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, drama);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Horror") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, horror);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Science Fiction") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, scifi);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Thriller") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, thriller);
lstViewMovies.setAdapter(adapter);
} else if (item.compareToIgnoreCase("Western") == 0) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, western);
lstViewMovies.setAdapter(adapter);
} else {
Toast.makeText(MainActivity.this, "Please select another genre!",
Toast.LENGTH_SHORT).show();
}
}
//auto generated method with onItemSelected method - unused
public void onNothingSelected(AdapterView<?> parent) {
}
这就像我需要它一样!它可能不是最佳选择,但作为一名新程序员,这对我有用。如果您有更好的方法,请告诉我。我想了解更多!