【发布时间】:2021-02-06 19:58:23
【问题描述】:
我创建了一个关于水生植物的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", "Low", 1));
arrayList.add(new Plant(R.drawable.anubiasa_sp, "Anubias Sp", "Low", "Low", 1));
arrayList.add(new Plant(R.drawable.java_moss, "Taxiphyllum Barbieri (Java Moss)", "Low", "Low", 1));
arrayList.add(new Plant(R.drawable.hair_grass, "Dwarf Hairgrass", "Medium", "Medium", 2));
arrayList.add(new Plant(R.drawable.crinum_calamistratum, "Crinum Calamistratum", "High", "High", 3));
arrayList.add(new Plant(R.drawable.echinodorus_iguazu_2009, "Echinodorus Iguazu 2009", "High", "High", 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;
}
有没有我遗漏的代码?请帮忙,
【问题讨论】: