首页 > 解决方案 > 使用 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", "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;
}

有没有我错过的代码?请帮忙,

我的应用

标签: androidlistviewspinner

解决方案


在这种情况下,我会检查两件事:

  1. 起点是什么?如果未设置类别 - 也许您的代码返回空列表,因此 ListView 似乎没有出现,但实际上是空的。
  2. 您没有复制粘贴布局 xml。也许这只是一个简单的错误。这是很常见的错误。

推荐阅读