Refresh ListView in fragment TabHost

362 Views Asked by At

I have MainActivity, which holds fragment. One of my fragments (Participants) checks if there's anything in database. If no, shows fragment(EmptyParticipantsList) with message to add data. If yes, it shows fragment with TabHost with 2 tabs, one of them holds fragment (ParticipantsList) with ListView with database entries. There's a FAB button to add more records. How can I refresh ListView after adding another record? I'm not sure since TabHost is not working with FragmentManager which I'm using in app.

//TODO refresh ListView is the place where I need to put my code.

EDIT 6.5.2017 I modify the code and add 'myCursorAdapter.notifyDataSetChanged();' after adding/editting items in SQLite but List still not refresh it self. Any help?

Participants.java

  myDB =  new DBAdapter(getActivity());
    myDB.open();
    Class S;
    if (myDB.isEmptyParticipants()) {
        S = EmptyParticipantsList.class;
    } else {
        S = ParticipantsList.class;
    }
    myDB.close();

    mTabHost = (FragmentTabHost) root.findViewById(R.id.tabHost);
    mTabHost.setup(getContext(), getChildFragmentManager(), android.R.id.tabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("List of participants"),
    S, bundle1);
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Event information"),
            EventInfo.class, bundle1);

ParticipantsList.java

 public class ParticipantsList extends DialogFragment {

DBAdapter myDB;
ListView participantList;
long id_eventu;
EditText participantName;
EditText participantSurname;
SimpleCursorAdapter myCursorAdapter;

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View root = inflater.inflate(R.layout.fragment_participants_list, container, false);
    myDB =  new DBAdapter(getContext());
    participantList = (ListView) root.findViewById(R.id.list_participants);
    myDB.open();
    FloatingActionButton fab1 = (FloatingActionButton) root.findViewById(R.id.fab_participant);
    Bundle bundle = getArguments();
    if (bundle != null) {
        id_eventu = bundle.getLong("key");
    }
    myDB.open();
    Cursor cursor = myDB.getAllRowsParticipant(id_eventu);
    String[] fromParticipantNames = new String[] {DBAdapter.PARTICIPANTS_NAME, DBAdapter.PARTICIPANTS_SURNAME};
    int[] toViewIDs = new int[] {R.id.name_of_participant, R.id.surname_of_participant};
    myCursorAdapter = new SimpleCursorAdapter(getActivity(),R.layout.row_participant, cursor, fromParticipantNames, toViewIDs,0 );
    participantList.setAdapter(myCursorAdapter);
    myCursorAdapter.notifyDataSetChanged();
    myDB.close();

    registerForContextMenu(participantList);

    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final AlertDialog.Builder addParticipantDialog = new AlertDialog.Builder(getContext());
            addParticipantDialog.setTitle("Add new Participant");
            final View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.dialog_add_participant, (ViewGroup) getView(), false);
            addParticipantDialog.setView(viewInflated);
            participantName = (EditText) viewInflated.findViewById(R.id.add_participant_name);
            participantSurname = (EditText) viewInflated.findViewById(R.id.add_participant_surname);
            addParticipantDialog.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //TODO zde se načtou data z polí a uloží do databáze
                    String name = participantName.getText().toString();
                    String surname = participantSurname.getText().toString();
                    myDB.open();
                    myDB.insertRowParticipant(name,surname, id_eventu);
                    Toast.makeText(getActivity(),"Participant added", Toast.LENGTH_LONG).show();
                    myDB.close();
                    myCursorAdapter.notifyDataSetChanged();

                }
            });
            addParticipantDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
            addParticipantDialog.show();
        }
    });

    return root;
}


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.event_popup, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    final long id = info.id;
    switch(item.getItemId()) {
        case R.id.edit_event_popup:
            final android.app.AlertDialog.Builder addEventDialog = new android.app.AlertDialog.Builder(getContext());
            addEventDialog.setTitle("Edit participant");
            final View viewInflated = LayoutInflater.from(getContext()).inflate(R.layout.dialog_add_participant, (ViewGroup) getView(), false);
            addEventDialog.setView(viewInflated);
            participantName = (EditText) viewInflated.findViewById(R.id.add_participant_name);
            participantSurname = (EditText) viewInflated.findViewById(R.id.add_participant_surname);
            myDB.open();
            Cursor c = myDB.db.rawQuery("SELECT * FROM participants WHERE _id=="+id, null);
            c.close();
            c.moveToFirst();
            String name_par = c.getString(c.getColumnIndex("name"));
            String surname_par = c.getString(c.getColumnIndex("surname"));
            participantName.setText(name_par); //tady se musí načíst data z db
            participantSurname.setText(surname_par);
            addEventDialog.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //TODO zde se načtou data z polí a uloží do databáze
                    String str = participantName.getText().toString();
                    String str1 = participantSurname.getText().toString();
                    ContentValues cv = new ContentValues();
                    cv.put("name",str);
                    cv.put("surname",str1);
                    myDB.db.update("participants", cv, "_id="+id, null);
                    Toast.makeText(getActivity(),"participant changed", Toast.LENGTH_LONG).show();
                    myDB.close();
                    myCursorAdapter.notifyDataSetChanged();
                    //TODO refresh listview
                }
            });
            addEventDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
            addEventDialog.show();
            return true;
        case R.id.delete_event_popup:
            myDB.open();
            myDB.deleteRowParticipant(id);
            Toast.makeText(getActivity(),"participant deleted", Toast.LENGTH_LONG).show();
            myCursorAdapter.notifyDataSetChanged();
            //TODO když je to poslední záznam, tak nahodit empty frag
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

}

1

There are 1 best solutions below

3
IntelliJ Amiya On

You should call notifyDataSetChanged after setAdapter

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

participantList.setAdapter(myCursorAdapter);
myCursorAdapter.notifyDataSetChanged(); // this
myDB.close();