在android中,如何避免光标将对象的副本添加到列表中?
我使用下面的片段来显示注释列表,但是当我从同一活动中的另一个片段进入这里时,它会将来自db的相同值再次放入列表中。 我怎样才能避免这种情况发生在android中,如何避免光标将对象的副本添加到列表中?,android,cursor,Android,Cursor,我使用下面的片段来显示注释列表,但是当我从同一活动中的另一个片段进入这里时,它会将来自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.
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);
}
}
导入android.app.Activity;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AbsListView;
导入android.widget.AdapterView;
导入android.widget.ListAdapter;
导入android.widget.TextView;
导入com.frisodenijs.allinoneOrganizer.dummy.DummyContent;
导入java.util.ArrayList;
导入java.util.List;
/**
*表示项目列表的片段。
*
*通过替换ListView支持大屏幕设备(如平板电脑)
*有一个网格视图。
*
*包含此片段的活动必须实现{@link OnFragmentInteractionListener}
*接口。
*/
公共类NotesFragment扩展片段实现AbsListView.OnItemClickListener{
私人名单;
//TODO:重命名参数参数,选择匹配的名称
//片段初始化参数,例如ARG_ITEM_NUMBER
私有静态最终字符串ARG_PARAM1=“PARAM1”;
私有静态最终字符串ARG_PARAM2=“PARAM2”;
//TODO:重命名和更改参数类型
私有字符串mParam1;
私有字符串mParam2;
私有OnFragmentInteractionListener mListener;
/**
*片段的ListView/GridView。
*/
私有AbsListView-mListView;
/**
*用于填充ListView/GridView的适配器
*观点。
*/
私有列表适配器mAdapter;
//TODO:重命名和更改参数类型
公共静态NotesFragment newInstance(字符串参数1,字符串参数2){
NotesFragment片段=新的NotesFragment();
Bundle args=新Bundle();
args.putString(ARG_PARAM1,PARAM1);
args.putString(ARG_PARAM2,PARAM2);
fragment.setArguments(args);
返回片段;
}
/**
*片段管理器实例化
*碎片(如屏幕方向改变时)。
*/
公共注释片段(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
notesList=新的ArrayList();
getInstance(getActivity()).noteDummies();
//TODO:更改适配器以显示您的内容
mAdapter=newnotesdapter(getActivity(),notesList);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment_注释,容器,错误);
//设置适配器
mListView=(AbsListView)view.findviewbyd(android.R.id.list);
((AdapterView)mListView).setAdapter(mAdapter);
//设置McClickListener,以便在单击项目时收到通知
mListView.setOnItemClickListener(此);
返回视图;
}
@凌驾
恢复时公开作废(){
super.onResume();
DBAdapter db=newdbadapter(getActivity());
db.open();
游标c=db.getAllNotes();
if(c.moveToLast()){
做{
添加(新注释(Long.parseLong(c.getString(0)),c.getString(1)));
}而(c.moveToPrevious());
}
db.close();
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
mListener=(OnFragmentInteractionListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
mListener=null;
}
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Note=(注意)this.notesList.get(位置);
//Toast.makeText(getActivity(),note.getText()+“Clicked!”,Toast.LENGTH\u SHORT.show();
/*
FragmentTransaction=getActivity().getSupportFragmentManager().beginTransaction();
replace(R.id.container_notes_fragment,new NotesEditFragment());
transaction.addToBackStack(空);
commit();
*/
mListener.goToNotesEditFragment(注);
//if(null!=mListener){
//通知活动回调接口(活动,如果
//片段被附加到一个)上,表示已选择项目。
//onFragmentInteraction(DummyContent.ITEMS.get(position.id));
// }
}
/**
*此片段的默认内容有一个文本视图,当
*列表为空。如果要更改文本,请调用此方法
*提供它应该使用的文本。
*/
public void setEmptyText(字符序列emptyText){
View-emptyView=mListView.getEmptyView();
如果(清空文本视图的视图实例){
((TextView)emptyView.setText(emptyText);
}
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*
*有关更多信息,请参阅Android培训课程。
*/
FragmentInteractionListener上的公共接口{
公共无效goToNotesEditFragment(注);
}
}
您可以将所需的所有值从一个片段传递到另一个片段,这样您就不必再次从数据库中获取它们
请注意:
setArguments()