【问题标题】:ListView does not appear when filtering with Spinner使用 Spinner 过滤时不出现 ListView
【发布时间】: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;
}

有没有我遗漏的代码?请帮忙,

【问题讨论】:

    标签: android listview spinner


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 2020-12-31
      • 2020-10-06
      • 2013-12-29
      相关资源
      最近更新 更多