【问题标题】:Refresh ListView in fragment TabHost刷新片段 TabHost 中的 ListView
【发布时间】:2017-03-30 12:28:04
【问题描述】:

我有 MainActivity,它包含片段。我的一个片段(参与者)检查数据库中是否有任何内容。如果否,则显示片段(EmptyParticipantsList)以及添加数据的消息。如果是,它会显示带有 TabHost 和 2 个选项卡的片段,其中一个包含带有 ListView 和数据库条目的片段(ParticipantsList)。有一个 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);

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

}

【问题讨论】:

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


    【解决方案1】:

    您应该在 setAdapter

    之后致电 notifyDataSetChanged

    通知附加的观察者基础数据已被 发生变化,任何反映数据集的视图都应自行刷新。

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

    【讨论】:

    • 谢谢,在我添加 populateListView();我的评论在哪里 //TODO 刷新 ListView 它正在工作。
    • @Raddino 很高兴听到。 #Abid 谢谢
    • 我试图在应用程序的不同位置重用代码,但它不起作用。我读了几篇文章,我确定调用 populateListView();添加/编辑项目以刷新 ListView 后是个坏主意,正确的是使用 notifyDataSetChanged 但不确定在哪里正确使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多