当前位置: 首页 > 知识库问答 >
问题:

刷新片段TabHost中的ListView

薛墨一
2023-03-14

我有Mainactive,它保存片段。我的一个片段(参与者)检查数据库中是否有任何内容。如果没有,则显示带有消息以添加数据的片段(空参与者列表)。如果是,它显示带有2个选项卡的TabHost片段,其中一个包含带有数据库条目的ListView片段(参与者列表)。有一个FAB按钮可以添加更多记录。添加另一条记录后如何刷新ListView?我不确定,因为TabHost不适用于我在应用程序中使用的FragmentManager。

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

编辑6.5.2017我修改了代码并添加了“myCursorAdapter”。notifyDataSetChanged();'在SQLite中添加/编辑项目后,但列表仍无法自行刷新。有什么帮助吗?

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);

参与者名单。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个答案

刘泰
2023-03-14

您应该在setAdapter之后调用通知数据设置更改

通知附加的观察者基础数据已更改,反映数据集的任何视图都应刷新自身。

participantList.setAdapter(myCursorAdapter);
myCursorAdapter.notifyDataSetChanged(); // this
myDB.close();
 类似资料:
  • 问题内容: 我搜索了许多看起来像这样的问题,但没有找到我的答案。 我有一个活动,该活动具有3个可通过操作栏访问的选项卡。我通过添加3个片段来实现这一点,这些片段使我自定义视图扩展了视图类。 在数据库更改的那一刻,我尝试通过调用invalidate()/ postinvalidate()刷新选项卡中的视图,但这不起作用。正如我考虑的其他许多选项一样,调用片段的onCreateView也是如此。 但是

  • 我遇到了一个如何在对话框片段中更新片段的问题。 当我单击过滤器菜单按钮时,会显示一个新的对话框片段,其中包括一个无线电组。 我想在单击ok按钮时更新包含位置列表的片段。 它是PlaceActive的代码,其中包含PlaceFraank: 公共类PlaceActive扩展AppCompatActive{ } 以下是PlaceFragment类的代码: 公共类PlaceFragment扩展了片段{ }

  • 我试着在发帖前在里面搜索,但我不能理解如何刷新片段中托管的ViewPager的内容。你能帮帮我吗? 谢谢你抽出时间 } 这是pageviewer中的imageview的类

  • 2)如果困境是真的,为什么事情要这样设置?谷歌计划反对TabHost和基于标签的多活动方法吗?多活动方法有什么不愉快的地方吗? 3)如果继续支持这两种方法,它们的优缺点是什么?如果我使用ActionBar+片段,我会遇到任何困难吗?例如,当我想让我的一个选项卡屏幕滑过/弹出一个额外的屏幕/片段时,我会不会在ActionBar中切换到/从选项卡中切换到选项卡?

  • 我有一个由片段填充的ViewPager。特别是,我有一个片段,它显示了Addresses对象的列表和一个允许创建新地址的列表。添加新地址的过程如下: 因此,当添加新地址时,我回到AddressListFragment,它是ViewPager中的活动地址。我需要更新该片段,以便更新列表与新添加的地址时,我回来它!我需要在那个片段上重写一个特定的方法吗? 提前道谢!

  • 在我的android应用程序中,两个月以来我一直无法解决这个问题,即使在尝试了堆栈溢出中发现的许多类似解决方案之后。我正在用ArrayAdapter填充listview。长时间按下listview的某个项目时,会出现一个对话框,要求确认。确认后,所选项目将从数组列表中删除,但在listview中,最后一个项目似乎已删除。当您关闭片段并返回到它时,您会看到正确的项目,但不会在关闭警报对话框后立即看到