如何解决使用 Spinner 过滤时不出现 ListView
我创建了一个关于水生植物的 ListView,但是当我创建一个 Spinner 来过滤数据时(我用我创建的 categoryID 过滤),ListView 列表没有出现,即使它在我放置 Spinner 之前就出现了。
>我的代码有问题吗?
这是我的 MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
Spinner spinner;
ArrayAdapter<PlantAdapter> adapter;
String[] categories = {"All","Low Co2","Medium Co2","High Co2"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,categories));
listView = findViewById(R.id.listView);
ArrayList<Plant> arrayList = new ArrayList<>();
arrayList.add(new Plant(R.drawable.cabomba_aquatica,"Cabomba Aquatica","Low",1));
arrayList.add(new Plant(R.drawable.anubiasa_sp,"Anubias Sp",1));
arrayList.add(new Plant(R.drawable.java_moss,"Taxiphyllum Barbieri (Java Moss)",1));
arrayList.add(new Plant(R.drawable.hair_grass,"Dwarf Hairgrass","Medium",2));
arrayList.add(new Plant(R.drawable.crinum_calamistratum,"Crinum Calamistratum","High",3));
arrayList.add(new Plant(R.drawable.echinodorus_iguazu_2009,"Echinodorus Iguazu 2009",3));
PlantAdapter plantAdapter = new PlantAdapter(this,R.layout.list_row,arrayList);
listView.setAdapter(plantAdapter);
spinner.setonItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView,View view,int position,long itemID) {
if (position>= 0 && position < categories.length) {
getSelectedCategoryData(position);
} else {
Toast.makeText(MainActivity.this,"Selected Category Does Not Exist!",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onnothingSelected(AdapterView<?> adapterView) {
}
});
}
private void getSelectedCategoryData(int categoryID) {
ArrayList<Plant> arrayList = new ArrayList<>();
if (categoryID == 0){
arrayList = new ArrayList<Plant>();
} else {
for (Plant plant : arrayList) {
if (plant.getCategoryID() == categoryID){
arrayList.add(plant);
}
}
arrayList = new ArrayList<Plant>();
}
listView.setAdapter(adapter);
}
以下是Plant.java代码
public class Plant {
int Image;
String Name;
String Co2_requirement;
String Light_requirement;
private int CategoryID;
public Plant(int image,String name,String co2_requirement,String light_requirement,int categoryID) {
Image = image;
Name = name;
Co2_requirement = co2_requirement;
Light_requirement = light_requirement;
CategoryID = categoryID;
}
public int getimage() {
return Image;
}
public void setimage(int image) {
Image = image;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getCo2_requirement() {
return Co2_requirement;
}
public void setCo2_requirement(String co2_requirement) {
Co2_requirement = co2_requirement;
}
public String getLight_requirement() {
return Light_requirement;
}
public void setLight_requirement(String light_requirement) {
Light_requirement = light_requirement;
}
public int getCategoryID() {
return CategoryID;
}
public void setCategoryID(int categoryID) {
CategoryID = categoryID;
}
这是 PlantAdapter.java 的代码
public class PlantAdapter extends ArrayAdapter<Plant> {
private Context mContext;
private int mResource;
public PlantAdapter(@NonNull Context context,int resource,@NonNull ArrayList<Plant> objects) {
super(context,resource,objects);
this.mContext = context;
this.mResource = resource;
}
@SuppressLint("ViewHolder")
@NonNull
@Override
public View getView(int position,@Nullable View convertView,@NonNull ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(mResource,parent,false);
ImageView imageView = convertView.findViewById(R.id.image);
TextView txtName = convertView.findViewById(R.id.txtName);
TextView txtco2 = convertView.findViewById(R.id.txtco2);
TextView txtlight = convertView.findViewById(R.id.txtlight);
imageView.setimageResource(getItem(position).getimage());
txtName.setText(getItem(position).getName());
txtco2.setText(getItem(position).getCo2_requirement());
txtlight.setText(getItem(position).getLight_requirement());
return convertView;
}
有没有我遗漏的代码?请帮忙,
解决方法
在这种情况下,我会检查两件事:
- 起点是什么?如果未设置类别 - 可能您的代码返回空列表,因此 ListView 似乎没有出现,但实际上是空的。
- 您没有复制粘贴布局 xml。也许这只是简单的错误。这是很常见的错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。