ListView aktuallisiert sich nicht

Lucaaa

Bekanntes Mitglied
Hallo!
Meine ListView aktuallisiert sich nach dem löchen eines Items nicht
Warum?

Java:
package com.ludevstudio.schoolmanager;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.ludevstudio.schoolmanager.Elements.Schedule;
import com.ludevstudio.schoolmanager.data.SchedulesSrc;
import com.ludevstudio.schoolmanager.dialogs.SchedulesOptions;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import static android.widget.Toast.makeText;


public class SchedulesFragment extends Fragment {
    // Buttons
    FloatingActionButton btnAddSchedule;

    // Schedule add variables

SchedulesFragment thisFragment = this;

ArrayList<Schedule> schedules = new ArrayList<Schedule>();

   ListView listSchedules;


ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.fragment_schedules, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initUI();

    }

    private void initUI() {
        btnAddSchedule = (FloatingActionButton) getView().findViewById(R.id.btn_add_schedules);
        btnAddSchedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Snackbar sBar = new DialogAddSchedule(thisFragment).get();
               sBar.show();
            }
        });

        // load all schedules from database and visualize it in a listview
        loadSchedules();
        listSchedules = (ListView) getView().findViewById(R.id.schedules_list);
      adapter =new ListAdapter(this.getActivity(), loadScheduleNames());
          listSchedules.setAdapter(adapter);


    }

    public ArrayList<String> loadScheduleNames() {
        ArrayList<String> list =new ArrayList<String>();

        for(Schedule s : schedules) {
            list.add(s.getName());
        }
        return list;
    }


    public class ListAdapter extends BaseAdapter {
        private Context context;
        private ArrayList<String> list =new ArrayList<String>();


        public ListAdapter(Context context, ArrayList<String> list) {
            this.context = context;
            this.list= list;
        }

        @Override
        public int getCount() {
           return list.size();

        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 1L;
        }

        @Override
        public View getView(final int i, View convertView, ViewGroup viewGroup) {
          View view = convertView;
        // create view
          if(view==null) {
              LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              view = inflater.inflate(R.layout.schedules_list_item, null);
          }
          // init Elements
            TextView twTitle = (TextView) view.findViewById(R.id.schedules_list_item_text);
          twTitle.setText(list.get(i));

          Button btnMenu = (Button) view.findViewById(R.id.schedules_list_item_button);
          btnMenu.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  SchedulesOptions dialog =new SchedulesOptions(getActivity(), schedules, schedules.get(i), adapter, listSchedules);
                  dialog.get().show();
              }
          });

          return  view;
        }
    }

    private void loadSchedules() {
        SchedulesSrc src =new SchedulesSrc(getActivity());
        src.open();
        schedules = src.getAll();
        src.close();
    }
}

Java:
package com.ludevstudio.schoolmanager.dialogs;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ludevstudio.schoolmanager.Elements.Schedule;
import com.ludevstudio.schoolmanager.MainActivity;
import com.ludevstudio.schoolmanager.R;
import com.ludevstudio.schoolmanager.SchedulesFragment;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class SchedulesOptions {
    Context context;

    Button btnClose;
    CardView btnEdit, btnClone, btnDelete;

ArrayList<Schedule> schedules;
Schedule schedule;
    SchedulesFragment.ListAdapter adapter;
    ListView listView;

    public SchedulesOptions(Context context, ArrayList<Schedule> schedules, Schedule schedule,
                            SchedulesFragment.ListAdapter adapter, ListView listView) {
        this.context = context;
        this.schedule = schedule;
        this.schedules = schedules;
        this.adapter = adapter;
        this.listView = listView;
    }

    public Snackbar get() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = inflater.inflate(R.layout.dialog_schedules_options, null);
        View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
       View contextView =   inflater.inflate(R.layout.fragment_schedules, null);
   //    View rootView = contextView.findViewById(R.id.schedules_roor);

       final Snackbar sBar = Snackbar.make(rootView, "hello World", Snackbar.LENGTH_INDEFINITE);
        Snackbar.SnackbarLayout sBarView = (Snackbar.SnackbarLayout) sBar.getView();
        TextView sTw = (TextView) sBarView.findViewById(android.support.design.R.id.snackbar_text);
        sTw.setVisibility(View.INVISIBLE);
        sBarView.setPadding(0,0,0,0);
        sBarView.addView(dialogView);

        initUI(sBar, dialogView);
        return sBar;
    }

    private void initUI(final Snackbar s, View v) {
        btnClose = (Button) v.findViewById(R.id.schedulesoptions_btn_close);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                s.dismiss();
            }
        });

        btnEdit = (CardView) v.findViewById(R.id.schedulesoptions_btn_edit);
        btnClone = (CardView) v.findViewById(R.id.schedulesoptions_btn_clone);
        btnDelete = (CardView) v.findViewById(R.id.schedulesoptions_btn_delete);

    btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            schedules.remove(schedule);
            adapter.notifyDataSetChanged();
            listView.invalidateViews();
            listView.invalidate();
            Toast.makeText(context, "gelöscht  "+schedules.size(), Toast.LENGTH_LONG).show();
        }
    });

    }

}
 

mihe7

Top Contributor
Wie kriege ich den denn den adapter dazu, aus einem Schedule Objekt den Namen zu lesen?
Genauso wie Du das in loadScheduleNames machst.

Java:
private ArrayList<Schedule> list =new ArrayList<Schedule>();
...
@Override
public Object getItem(int i) {
    return list.get(i).getName();
}

Da deine ListAdapter-Klasse noch nicht mal static ist, könntest Du sogar direkt auf schedules zugreifen.
 

mihe7

Top Contributor
@Lucaaa das habe ich schon gesehen, allerdings bin ich mit Android nicht so vertraut, als dass ich da eine Aussage treffen möchte. Es wird sich schon noch einer melden, der davon weit mehr Ahnung hat.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
J ListView Item individuell einfärben Android & Cross-Platform Mobile Apps 17
I Android ListView, Werte aktualisieren ohne die Liste komplett neu zu laden Android & Cross-Platform Mobile Apps 5
W ListView OnItemClicklistener setzen mit Ausgabe Android & Cross-Platform Mobile Apps 35
K Null-Pointer-Exception in ListView - wird über Datenbank gefüllt Android & Cross-Platform Mobile Apps 1
I Android ListView (Custom) soll auf Hardwaretasten nicht reagieren. Android & Cross-Platform Mobile Apps 10
W ListView und Arrays... Android & Cross-Platform Mobile Apps 68
W Android Wieso kann ich keine ListView mehr zum Layout hinzufügen? Android & Cross-Platform Mobile Apps 1
W Android Kann keine ListView mehr in der MainActivtiy anzeigen, obwohl noch sehr viel Platz frei ist Android & Cross-Platform Mobile Apps 1
N Probleme mit custom dynamic ListView Android & Cross-Platform Mobile Apps 15
L Android ListView kollabiert in Scrollview Android & Cross-Platform Mobile Apps 9
A ImageButton in ListView Item bei klick ändern Android & Cross-Platform Mobile Apps 3
J Android Suche in einer ListView Android & Cross-Platform Mobile Apps 3
H Android ArrayList <-> ArrayAdapter <-> ListView Android & Cross-Platform Mobile Apps 10
L Android ListView swipe zum löschen Android & Cross-Platform Mobile Apps 1
B Android ListView set custom check Image and delete Android & Cross-Platform Mobile Apps 0
M Android ListView wird nicht dargestellt Android & Cross-Platform Mobile Apps 2
Maresuke Android Android ListView Textfarbe und Texthintergrund ändern? Android & Cross-Platform Mobile Apps 5
A Android Problem mit ListView und OnItemClickListener.. Android & Cross-Platform Mobile Apps 10
S Listview Einträge aus "xml" Datei Android & Cross-Platform Mobile Apps 1
S Android Studio MySql Daten in Listview mit sub Item Android & Cross-Platform Mobile Apps 11
S Textdatei in ListView einlesen Tutorial gesucht!? Android & Cross-Platform Mobile Apps 3
kaoZ Tutorial .xml Layouting für z.B ListView elemente Android & Cross-Platform Mobile Apps 7
M Android ListView und Checkbox Android & Cross-Platform Mobile Apps 6
L TableRows in ListView darstellen Android & Cross-Platform Mobile Apps 2
M ListView mit ListAdapter füllen Android & Cross-Platform Mobile Apps 5
U Android ListView Frage Android & Cross-Platform Mobile Apps 6
L Android SearchBox für Custom Listview Android & Cross-Platform Mobile Apps 5
H Android ListView Images aus dem Internet via Thread Android & Cross-Platform Mobile Apps 3
T Android: ListView-Adapter: Adapter wird ständig aufgerufen Android & Cross-Platform Mobile Apps 2
H Android SAX|ListView NullPointerException Android & Cross-Platform Mobile Apps 2
A Probleme mit ListView / ArrayAdapter Android & Cross-Platform Mobile Apps 3
OSchriever Navigation drawer Strings ändern sich nicht Android & Cross-Platform Mobile Apps 0
Noahscript Android Socket.connect() wiederholt sich mehrmals? Android & Cross-Platform Mobile Apps 4
S Neue Activity lässt sich nicht starten Android & Cross-Platform Mobile Apps 28
K Sounds hören irgendwann auf sich abzuspielen Android & Cross-Platform Mobile Apps 3
H Buttons färben sich mit, beim Hintergrund ändern Android & Cross-Platform Mobile Apps 3
K Grafik Tablerow, Button erstreckt sich in der gesamten Breite trotz Beschrenkung durch (max)width Android & Cross-Platform Mobile Apps 2
B TableLayout verhält sich nicht wie eine Tabelle Android & Cross-Platform Mobile Apps 3
F Lohnt sich diese Youtubeserie um AndroidProgrammierumg zu lernen? Android & Cross-Platform Mobile Apps 4
G Android Finale Variable ändert sich NIE? Android & Cross-Platform Mobile Apps 2
S Android Android Eclipse Plugin lässt sich nicht installieren Android & Cross-Platform Mobile Apps 4
U Fire button und Command.BACK, 1 kommen sich in die quere Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben