Hallo!
Meine ListView aktuallisiert sich nach dem löchen eines Items nicht
Warum?
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();
}
});
}
}