簡體   English   中英

嘗試使用 notifyDataSetChanged 更新我的 listView 自定義列表適配器但不工作

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

添加新文件時如何正確更新 listView 適配器? listView 由數組提供。 我讀了很多書並嘗試了幾件事,但沒有任何效果。 我錯過了什么嗎? notifyDataSetChanged 由於某種原因無法正常工作。

活動.java

創建時:

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()將起作用。 您在onResume()中調用它可能會在 function 中的循環完成之前被調用。 嘗試更改您的 function 調用並在 for 循環完成后添加notifyDataSetChanged()

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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM