· 7 years ago · Sep 24, 2018, 05:58 PM
1public class toDastgheybFragment extends BaseFragment {
2
3
4 private List<DatabaseModel> Times = new ArrayList<DatabaseModel>();
5 DatabaseHelper databaseHelper;
6 private View mView;
7 RecyclerView mRecyclerView;
8 RecyclerView.Adapter mAdapter;
9
10 @Nullable
11 @Override
12 public View onCreateView(LayoutInflater inflater, ViewGroup container,
13Bundle savedInstanceState) {
14
15 mView = inflater.inflate(R.layout.fragment_to_dastgheyb, container,
16false);
17 databaseHelper = new DatabaseHelper(getContext());
18 Times = databaseHelper.getAllUsers();
19 mRecyclerView = mView.findViewById(R.id.myRecycler);
20 mAdapter = new DataAdapter(Times);
21 mRecyclerView.setLayoutManager(new
22LinearLayoutManager(getContext()));
23 mRecyclerView.setAdapter(mAdapter);
24 return mView;
25 }
26
27
28 @Override
29 public void onActivityCreated(Bundle savedInstanceState) {
30 super.onActivityCreated(savedInstanceState);
31 ArrayList<DatabaseModel> LineList = new ArrayList<>();
32 LineList.clear();
33 mAdapter = new DataAdapter(LineList);
34 DatabaseHelper db = new DatabaseHelper(getContext());
35 final List<DatabaseModel> m = db.getAllUsers();
36 if (m.size() > 0) {
37 for (int i = 0; i < m.size(); i++) {
38 LineList.add(m.get(i));
39 mAdapter.notifyDataSetChanged();
40 }
41 }
42 db.close();
43 }
44}
45
46public class DatabaseHelper extends SQLiteOpenHelper {
47 private static final int DATABASE_VERSION = 1;
48 private static final String DATABASE_NAME = "MetroDB";//name of the
49database
50 private static final String TABLE_NAME = "stationtime";//name for the
51table
52 static String db_path = "/data/data/ir.shirazmetro/databases/";
53 private static final String Station = "station";
54 private static final String Time = "time";
55 private static final String Line = "line";
56 private final Context context;
57 private SQLiteDatabase database;
58
59 public DatabaseHelper(Context context) {
60 super(context, DATABASE_NAME, null, DATABASE_VERSION);
61 this.context = context;
62 }
63
64 @Override
65 public void onCreate(SQLiteDatabase db) {
66
67 String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " +
68TABLE_NAME + "(" + Station + " TEXT," + Time + " TEXT," + Line + " TEXT)";
69 db.execSQL(CREATE_CONTACTS_TABLE);
70 }
71
72 @Override
73 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
74{
75 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
76 onCreate(db);
77
78 }
79
80 private boolean checkExist() {
81 SQLiteDatabase db = null;
82 try {
83 db = SQLiteDatabase.openDatabase(db_path + DATABASE_NAME, null,
84SQLiteDatabase.OPEN_READONLY);
85 } catch (SQLException e) {
86 }
87 return db != null;
88 }
89
90 private void copyDatabase() throws IOException {
91 OutputStream myOutput = new FileOutputStream(db_path +
92DATABASE_NAME);
93 byte[] buffer = new byte[1024];
94 int length;
95 InputStream myInput = context.getAssets().open(DATABASE_NAME +
96".db");
97 while ((length = myInput.read(buffer)) > 0) {
98 myOutput.write(buffer, 0, length);
99 }
100 myInput.close();
101 myOutput.flush();
102 myOutput.close();
103 }
104
105 public void importIfNotExist() throws IOException {
106 boolean dbExist = checkExist();
107 if (!dbExist) {
108 this.getReadableDatabase();
109 try {
110 copyDatabase();
111 } catch (IOException e) {
112 e.printStackTrace();
113 }
114 }
115 }
116
117 public void open() {
118 database = SQLiteDatabase.openDatabase(db_path + DATABASE_NAME, null,
119SQLiteDatabase.OPEN_READWRITE);
120 }
121
122 public List<DatabaseModel> getAllUsers() {
123 List<DatabaseModel> contactList = new ArrayList<DatabaseModel>();
124 String whatStation = C.whatStation;
125 String whatLine = C.whatLine;
126 String selectQuery = "SELECT " + Time + " FROM " + TABLE_NAME + "
127WHERE " + Station + " LIKE '%" + whatStation + "%' AND " + Line + " LIKE '%"
128+ whatLine + "%'";
129 SQLiteDatabase db = this.getReadableDatabase();
130 Cursor cursor = db.rawQuery(selectQuery, null);
131
132 cursor.moveToFirst();
133 if (cursor.getCount() > 0) {
134 do {
135 DatabaseModel m = new DatabaseModel();
136 m.setTime(cursor.getString(cursor.getColumnIndex(Time)));
137 contactList.add(m);
138 } while (cursor.moveToNext());
139 }
140
141
142 return contactList;
143 }
144
145}
146
147public class station extends BaseActivity {
148Toolbar mToolbar;
149private TabLayout tbLayout;
150private ViewPager vPager;
151RecyclerView.Adapter mAdapter;
152
153
154@Override
155protected void onCreate(Bundle savedInstanceState) {
156 super.onCreate(savedInstanceState);
157 setContentView(R.layout.activity_station);
158
159 final DatabaseHelper helper = new DatabaseHelper(this);
160 try {
161 helper.importIfNotExist();
162 } catch (IOException ioe) {
163 throw new Error("Unable to create database");
164 }
165 mToolbar = findViewById(R.id.tlbr1);
166
167 setSupportActionBar(mToolbar);
168 initView();
169 setupWithViewPager();
170 tbLayout.addOnTabSelectedListener(new
171TabLayout.OnTabSelectedListener() {
172 @Override
173 public void onTabSelected(TabLayout.Tab tab) {
174
175 }
176
177 @Override
178 public void onTabUnselected(TabLayout.Tab tab) {
179
180 }
181
182 @Override
183 public void onTabReselected(TabLayout.Tab tab) {
184
185 }
186 });
187
188 }
189
190}
191
192
193public void line1(View view) {
194 Intent line1 = new Intent(this, line1.class);
195 startActivity(line1);
196}
197
198private void setupWithViewPager() {
199 BasePagerAdapter basePagerAdapter = new BasePagerAdapter(this,
200getSupportFragmentManager());
201 vPager.setAdapter(basePagerAdapter);
202 tbLayout.setupWithViewPager(vPager);
203
204}
205
206private void initView() {
207 vPager = findViewById(R.id.view_pager);
208 tbLayout = findViewById(R.id.tab_layout);
209 backBtn = findViewById(R.id.backBtn);
210
211
212}
213
214
215@Override
216public void onClick(View view) {
217 switch (view.getId()) {
218 case R.id.backBtn:
219 line1(view);
220 return;
221 }
222
223}
224
225}