简体   繁体   中英

Trying to update my custom list adapter of listView using notifyDataSetChanged but not working

How can I update my listView adapter properly when I add a new file? The listView is feed by a an array. I have read a lot and tried several things but nothing works outs. Am I missing something? notifyDataSetChanged is not working for some reason.

activity.java

on create:

ListView list2;    
CustomList listAdapter;
String[] ficheros;

final File carpeta = new File("/storage/emulated/0/Android/data/cc.openframeworks.androidMultiOFActivitiesExample/files/xml");


    list2=(ListView)findViewById(R.id.listview);
    list2.setVisibility(GONE);
    list2.setCacheColorHint(Color.TRANSPARENT);

    ficheros = list.toArray(new String[list.size()]);
    List<String> list = new ArrayList<String>();
    listAdapter =  new CustomList(OFActivityA.this, ficheros,fecha);
    list2.setAdapter(listAdapter);
    listarFicherosPorCarpeta(carpeta);

@Override
protected void onResume() {
   super.onResume();


public void listarFicherosPorCarpeta(final File carpeta) {
   for (final File ficheroEntrada: carpeta.listFiles()) {
         if (ficheroEntrada.isDirectory()) {
               listarFicherosPorCarpeta(ficheroEntrada);
              } else {
                 System.out.println(ficheroEntrada.getName());
                 list.add(ficheroEntrada.getName());
              }
            }
      listAdapter.notifyDataSetChanged();
           }

CustomList.java

public class CustomList extends ArrayAdapter<String>{

    private final Activity context;
    private final String[] ficheros;
    private final String[] fecha;

    public CustomList(Activity context, String[] ficheros, String[] fecha) {
        super(context, R.layout.rowlayout, ficheros);
        this.context = context;
        this.ficheros = ficheros;
        this.fecha = fecha;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.rowlayout, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.listtext);
        TextView txtFecha = (TextView) rowView.findViewById(R.id.fechatxt);
        txtTitle.setTextColor(Color.parseColor("#015D99"));
        txtTitle.setHeight(123);
        txtFecha.setText(fecha[position]);
        txtTitle.setText(ficheros[position]);

        return rowView;
    }

notifyDataSetChanged() will work if the content in dataset has changed. You are calling it in onResume() which might get called before your loop in function has finished. Try changing your function call and add notifyDataSetChanged() once your for loop completes.

public void listarFicherosPorCarpeta(final File carpeta) {
   for (final File ficheroEntrada: carpeta.listFiles()) {
         if (ficheroEntrada.isDirectory()) {
               listarFicherosPorCarpeta(ficheroEntrada);
              } else {
                 System.out.println(ficheroEntrada.getName());
                 list.add(ficheroEntrada.getName());
         }
   }
   listAdapter.notifyDataSetChanged(); //call it here after you have updated your data.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM