我使用以下片段来显示注释列表,但是当我从同一活动中的另一个片段进入此处时,它会将db中相同的值再次放入列表中。 我怎样才能避免这种情况发生?
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.frisodenijs.allinoneorganiser.dummy.DummyContent;
import java.util.ArrayList;
import java.util.List;
/**
* A fragment representing a list of Items.
* <p/>
* Large screen devices (such as tablets) are supported by replacing the ListView
* with a GridView.
* <p/>
* Activities containing this fragment MUST implement the {@link OnFragmentInteractionListener}
* interface.
*/
public class NotesFragment extends Fragment implements AbsListView.OnItemClickListener {
private List notesList;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/**
* The fragment's ListView/GridView.
*/
private AbsListView mListView;
/**
* The Adapter which will be used to populate the ListView/GridView with
* Views.
*/
private ListAdapter mAdapter;
// TODO: Rename and change types of parameters
public static NotesFragment newInstance(String param1, String param2) {
NotesFragment fragment = new NotesFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public NotesFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
notesList = new ArrayList();
DummyContent.getInstance(getActivity()).noteDummies();
// TODO: Change Adapter to display your content
mAdapter = new NotesAdapter(getActivity(), notesList);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes, container, false);
// Set the adapter
mListView = (AbsListView) view.findViewById(android.R.id.list);
((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
// Set OnItemClickListener so we can be notified on item clicks
mListView.setOnItemClickListener(this);
return view;
}
@Override
public void onResume() {
super.onResume();
DBAdapter db = new DBAdapter(getActivity());
db.open();
Cursor c = db.getAllNotes();
if (c.moveToLast()) {
do {
notesList.add(new Note(Long.parseLong(c.getString(0)), c.getString(1)));
}while (c.moveToPrevious());
}
db.close();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Note note = (Note) this.notesList.get(position);
//Toast.makeText(getActivity(), note.getText() + "Clicked!", Toast.LENGTH_SHORT).show();
/*
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_notes_fragment, new NotesEditFragment());
transaction.addToBackStack(null);
transaction.commit();
*/
mListener.goToNotesEditFragment(note);
//if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
// mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
// }
}
/**
* The default content for this Fragment has a TextView that is shown when
* the list is empty. If you would like to change the text, call this method
* to supply the text it should use.
*/
public void setEmptyText(CharSequence emptyText) {
View emptyView = mListView.getEmptyView();
if (emptyView instanceof TextView) {
((TextView) emptyView).setText(emptyText);
}
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
public void goToNotesEditFragment(Note note);
}
}
答案 0 :(得分:0)
您可以将所需的所有值从一个片段传递到另一个片段,这样您就不必再次将它们从db中传出。
请注意:不鼓励两个片段之间的直接通信,您应该创建一个回调给您的活动的电话,并从那里将数据传递给另一个片段。 (你可以使用
setArguments()
)