从sqlite加载数据在一个片段中正常工作,而在第二个片段中不起作用

时间:2016-10-12 17:59:08

标签: android sqlite android-fragments android-asynctask android-viewpager

我在mainActivity中有asynctask并将数据保存到sqlite.But当我在viewpager frags中加载这些数据时,它在一个工作正常,而第二个片段没有发生任何事情。有人可以帮助我吗?

public class CurrentContests extends Fragment {

        private String TAG = CurrentContests.class.getSimpleName();

        RecyclerView rv;
        ProgressDialog pdialog;
        public DataAdapter adapter;
        protected static ArrayList<DataModel> data;
        private RecyclerView.LayoutManager layoutManager;
        static View v;
        private static final String SELECT_SQL = "SELECT * FROM ankit";
        public SQLiteDatabase db;
        private Cursor c;

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


            v = inflater.inflate(R.layout.currentcontests_layout, container, false);
            data = new ArrayList<>();
            rv = (RecyclerView) v.findViewById(R.id.tab1_recycler_view);
            rv.setHasFixedSize(true);
            layoutManager = new LinearLayoutManager(getActivity());
            rv.setLayoutManager(layoutManager);
            rv.setItemAnimator(new DefaultItemAnimator());
            adapter = new DataAdapter(data);
            rv.setAdapter(adapter);
           /* new GetContacts().execute();*/
            openDatabase();


            c = db.rawQuery(SELECT_SQL, null);
            //c.moveToFirst();
            //showRecords();
            db.beginTransaction();
            shownow();
            return v;
        }

        protected void openDatabase() {
            db =getActivity().openOrCreateDatabase("ankitDB", Context.MODE_PRIVATE, null);//openOrCreateDatabase("ankitDB", Context.MODE_PRIVATE, null);
        }
        private void shownow(){
            //if the cursor isnt null we will essentially iterate over rows and then columns
            //to form a table of data as per database.
            if (c != null) {

                //more to the first row
                c.moveToFirst();
                Log.e("current", "shownow");
                //iterate over rows
                for (int i = 0; i < c.getCount(); i++) {

                    String site = c.getString(1);
                    String name = c.getString(2);
                    String begin_date = c.getString(3);
                    String begin_time=c.getString(4);
                    String end_date=c.getString(5);
                    String end_time=c.getString(6);
                    String type=c.getString(7);
                    //String type=c.getString(8);

                        DataModel dm = new DataModel(site, name, begin_date, begin_time, end_date, end_time);//,i+1,R.drawable.codechef);
                        data.add(dm);


                    //move to the next row
                    c.moveToNext();
                }
                //close the cursor
                db.setTransactionSuccessful();
               db.endTransaction();
                c.close();
                db.close();
            }
            adapter.notifyDataSetChanged();
        }
    }

这是我的 Second Fragment

public class SecondContent extends Fragment {

        private String TAG = UpcommingContests.class.getSimpleName();
        RecyclerView rv;
        ProgressDialog pdialog;
        public DataAdapter adapter;
        protected static ArrayList<DataModel> data;
        private RecyclerView.LayoutManager layoutManager;
        static View v;

        private static final String SELECT_SQL = "SELECT * FROM ankit";

        public SQLiteDatabase dbup;

        private Cursor cup;
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


            v = inflater.inflate(R.layout.upcomingcontests_layout, container, false);
            data = new ArrayList<>();
            rv = (RecyclerView) v.findViewById(R.id.rv_tab2);
            rv.setHasFixedSize(true);
            layoutManager = new LinearLayoutManager(getActivity());
            rv.setLayoutManager(layoutManager);
            rv.setItemAnimator(new DefaultItemAnimator());
            adapter = new DataAdapter(data);
            rv.setAdapter(adapter);
           /* new GetContacts().execute();*/
            openDatabase();
            cup = dbup.rawQuery(SELECT_SQL, null);
            //c.moveToFirst();
            //showRecords();
            shownow();
            return v;
        }
        protected void openDatabase() {
            dbup =getActivity().openOrCreateDatabase("ankitDB", Context.MODE_PRIVATE, null);//openOrCreateDatabase("ankitDB", Context.MODE_PRIVATE, null);
        }
        private void shownow(){
            //if the cursor isnt null we will essentially iterate over rows and then columns
            //to form a table of data as per database.
            if (cup != null) {
                //more to the first row
                cup.moveToFirst();
                Log.e("upcoming","shownow");
                //iterate over rows
                for (int i = 0; i < cup.getCount(); i++) {
                    String site = cup.getString(1);
                    String name = cup.getString(2);
                    String begin_date = cup.getString(3);
                    String begin_time=cup.getString(4);
                    String end_date=cup.getString(5);
                    String end_time=cup.getString(6);
                    String type=cup.getString(7);
                    //String type=c.getString(8);
                    Log.e("upcoming",site+"");
                        DataModel dm = new DataModel(site, name, begin_date, begin_time, end_date, end_time);//,i+1,R.drawable.codechef);
                        data.add(dm);
                    //move to the next row
                    cup.moveToNext();
                }
                //close the cursor
                adapter.notifyDataSetChanged();
                cup.close();
                dbup.close();

            }
            adapter.notifyDataSetChanged();
        }
    }

1 个答案:

答案 0 :(得分:0)

问题可能出在数据库事务管理中。当事务启动时,它会阻止从任何其他位置访问数据库,直到它结束。

在您的情况下,您在onCreateView()中启动事务并仅在游标不为空时才完成。这意味着如果光标为空,数据库将保持阻塞状态。

首选使用try-finally块中的数据库事务来防止此类行为:

    db.beginTransaction();
try {
    // do your work
    db.setTransactionSuccessful();
} catch {
    //Error between database transaction. Or skip catch if not necessary 
} finally {
    db.endTransaction();
}