gpt4 book ai didi

java - 为列表创建过滤器-ANDROID

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:17 25 4
gpt4 key购买 nike

我在开发Android应用程序时遇到问题:

我有一个列表,它保存自定义对象:

public class Centro implements Serializable {
String id;
String nombre;
String direccion;
Boolean infantil;
Boolean primaria;
Boolean eso;
Boolean bachillerato;
Boolean fp;
Boolean universidad;
String descripcion;
Provincia province;

public Centro(String id, String nombre,
String direccion, Boolean infantil,
Boolean primaria, Boolean eso,
Boolean bachillerato, Boolean fp,
Boolean universidad, String descripcion,
Provincia province) {
this.id = id;
this.nombre = nombre;
this.direccion = direccion;
this.infantil = infantil;
this.primaria = primaria;
this.eso = eso;
this.bachillerato = bachillerato;
this.fp = fp;
this.universidad = universidad;
this.descripcion = descripcion;
this.province = province;
}
}


在我的活动中,当getBachillerato,getUniversidad和getFp的值为false时,我想将该对象添加到另一个列表中。问题是,当我制作过滤器时,我使用适配器在ListView中显示列表,它什么也不显示,就像列表中没有内容一样,就像任何对象都通过了过滤器一样。

public class FirstFragment extends Fragment implements FragmentLifecycle {

private static String url = "https://testapi-pprog2.azurewebsites.net/api/schools.php?method=getSchools";
ListView listView;
List<Centro> centroListaOne = new ArrayList<>();
private ProgressDialog pDialog;
private Context ctx;

public FirstFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new cargarCentrosLista().execute();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_one, container, false);
listView = v.findViewById(R.id.listViewOne);
return v;
}

@Override
public void onPauseFragment() {

}

@Override
public void onResumeFragment() {
new cargarCentrosLista().execute();
}

private class cargarCentrosLista extends AsyncTask<Void, Void, Void> {

List<Centro> list = new ArrayList<>();

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
JSONObject obj = new JSONObject(jsonStr);
JSONArray heroArray = obj.getJSONArray("msg");
for (int i = 0; i < heroArray.length(); i++) {
JSONObject centroObjeto = heroArray.getJSONObject(i);
Centro centro = new Centro();
centro.setNombre(centroObjeto.getString("schoolName"));
centro.setDireccion(centroObjeto.getString("schoolAddress"));
centro.setId(centroObjeto.getString("id"));
if (centroObjeto.getString("isBatxillerat") == "0") {
centro.setBachillerato(false);
} else {
centro.setBachillerato(true);
}
if (centroObjeto.getString("isEso") == "0") {
centro.setEso(false);
} else {
centro.setBachillerato(true);
}
if (centroObjeto.getString("isBatxillerat") == "0") {
centro.setBachillerato(false);
} else {
centro.setBachillerato(true);
}
if (centroObjeto.getString("isFP") == "0") {
centro.setFp(false);
} else {
centro.setFp(true);
}
if (centroObjeto.getString("isInfantil") == "0") {
centro.setInfantil(false);
} else {
centro.setInfantil(true);
}
if (centroObjeto.getString("isPrimaria") == "0") {
centro.setPrimaria(false);
} else {
centro.setPrimaria(true);
}
if (centroObjeto.getString("isUniversitat") == "0") {
centro.setUniversidad(false);
} else {
centro.setUniversidad(true);
}
switch (centroObjeto.getString("province")) {
case "barcelona":
centro.setProvince(Provincia.BARCELONA);
break;
case "girona":
centro.setProvince(Provincia.GIRONA);
break;
case "lleida":
centro.setProvince(Provincia.LLEIDA);
break;
case "tarragona":
centro.setProvince(Provincia.TARRAGONA);
break;
default:
centro.setProvince(null);
break;
}
centroListaOne.add(centro);
}
} catch (final JSONException e) {

}
} else {

}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
List<Centro> listaFiltro = new ArrayList<>();
for(Centro centro : centroListaOne){
if(centro.getBachillerato() == false
&& centro.getUniversidad() == false
&& centro.getFp() == false){
listaFiltro.add(centro);
}
}
CentroAdapter adapter = new CentroAdapter(listaFiltro, getActivity());
listView.setAdapter(adapter);
}
}
}


我的适配器:

public class CentroAdapter extends ArrayAdapter<Centro> implements Filterable {

private List<Centro> heroList;
private Context mCtx;

public CentroAdapter(List<Centro> heroList, Context mCtx) {
super(mCtx, R.layout.mockup_lista_escuela, heroList);
this.heroList = heroList;
this.mCtx = mCtx;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mCtx);

View listViewItem = inflater.inflate(R.layout.mockup_lista_escuela, null, true);
TextView nombre = listViewItem.findViewById(R.id.nombreEscuela);
TextView direcciom = listViewItem.findViewById(R.id.direccionEscuela);
ImageView imagen = listViewItem.findViewById(R.id.imagenEscuela);
Centro hero = heroList.get(position);
Picasso.get().load("https://fotos00.diarioinformacion.com/2010/09/14/318x200/2010-09-21_IMG_2010-09-14_01.31.35__M0701BENIDORM.jpg.jpg").into(imagen);
nombre.setText(hero.getNombre());
direcciom.setText(hero.getDireccion());

return listViewItem;
}


}

编辑:当我看到新列表中有多少个项目时,没有项目,基本上任何对象都通过过滤器。

最佳答案

看一看
过滤器如何与Android中的列表一起使用
https://youtu.be/MALa2ahelCA

关于java - 为列表创建过滤器-ANDROID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51249972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com