android - 在 fragment TabHost 中刷新 ListView

标签 android listview android-fragments android-tabhost fragment-tab-host

我有 MainActivity,它包含 fragment 。我的一个 fragment (参与者)检查数据库中是否有任何内容。如果否,则显示带有添加数据消息的 fragment (EmptyParticipantsList)。如果是,它将显示带有 TabHost 的 fragment 和 2 个选项卡,其中一个包含带有数据库条目的 ListView 的 fragment (ParticipantsList)。有一个 FAB 按钮可以添加更多记录。添加另一条记录后如何刷新ListView?我不确定,因为 TabHost 不能与我在应用程序中使用的 FragmentManager 一起使用。

//TODO refresh ListView 是我需要放代码的地方。

编辑 6.5.2017 我修改代码并添加“myCursorAdapter.notifyDataSetChanged();”在 SQLite 中添加/编辑项目后,但 List 仍未自行刷新。有帮助吗?

参与者.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);

参与者列表.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);
    }
}

最佳答案

你应该调用 notifyDataSetChanged 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();

关于android - 在 fragment TabHost 中刷新 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43117867/

相关文章:

android - Android 5 Lollipop 中的通知栏图标变为白色

java - 为什么是 Libgdx Gdx.gl10.glLineWidth(width);不对更改投影属性使用react

android - 删除doOnTextChanged上的文本会删除我以编程方式设置的编辑文本的特殊格式

java - 如何通过单击 ListView 项并将值发送到对话框来显示来创建自定义对话框?

android - 如何在 android 上的对话框中管理返回堆栈

安卓 NFC : 'd' prepended on NDEF payload

android - 剪辑列表选择器

listview - 对 ListView<> 中的项目进行排序

python - 如何更改 ttk.Combobox ListView 中的背景颜色?

java - 如何在一个 Activity 中切换两个 fragment ?