· 7 years ago · Sep 17, 2018, 10:00 PM
1public class station extends BaseActivity {
2Toolbar mToolbar;
3private TabLayout tbLayout;
4private ViewPager vPager;
5private ButtonCell backBtn;
6Boolean btnOneOn = false;
7Boolean btnTwoOn = false;
8Boolean btnThreeOn = false;
9Boolean btnfourOn = false;
10ButtonCell Button2;
11ButtonCell Button1;
12ButtonCell Button3;
13ButtonCell Button4;
14
15private List<DatabaseModel> LineList = new ArrayList<>();
16private RecyclerView recyclerView;
17private DataAdapter mAdapter;
18
19@Override
20protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_station);
23
24 mToolbar = findViewById(R.id.tlbr1);
25 setSupportActionBar(mToolbar);
26
27 DatabaseHelper helpher;
28 helpher=new DatabaseHelper(station.this);
29 helpher.insertIntoDB("dastgheyb", "06:10:00", "B");
30 helpher.close();
31 initView();
32 setupWithViewPager();
33 tbLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
34 @Override
35 public void onTabSelected(TabLayout.Tab tab) {
36
37 }
38
39 @Override
40 public void onTabUnselected(TabLayout.Tab tab) {
41
42 }
43
44 @Override
45 public void onTabReselected(TabLayout.Tab tab) {
46
47 }
48 });
49
50 }
51
52}
53
54
55
56}
57
58public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder> {
59
60 private List<DatabaseModel> LineList;
61 public Context mContext;
62
63
64 public DataAdapter(Context mContext, List<DatabaseModel> line1List) {
65 this.LineList = line1List;
66 this.mContext = mContext;
67 }
68
69 public class MyViewHolder extends RecyclerView.ViewHolder {
70
71 TextViewCell Time;
72
73 public MyViewHolder(View view) {
74 super(view);
75 //initialize textViews and buttons
76 Time = view.findViewById(R.id.textView);
77 }
78 }
79
80
81 @NonNull
82 @Override
83 public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
84 //set item layout using layout inflater
85 View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_to_dastgheyb, parent, false);
86 return new MyViewHolder(itemView);
87 }
88
89 @Override
90 public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
91 final DatabaseModel list = LineList.get(position);
92 holder.Time.setText(list.getTime());
93 }
94
95 @Override
96 public int getItemCount() {
97 return LineList.size();
98 }
99
100}
101
102And this is my fragment.java:
103
104 public class toDastgheybFragment extends BaseFragment {
105 private Activity mActivity = null;
106 private View mView;
107 private RecyclerFragmentListener mFragmentListener = null;
108
109 // RecyclerViewã¨Adapter
110 private RecyclerView mRecyclerView;
111 DataAdapter mAdapter ;
112
113 public interface RecyclerFragmentListener {
114 void onRecyclerEvent();
115 }
116
117 @Nullable
118 @Override
119 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
120 mView = inflater.inflate(R.layout.fragment_to_dastgheyb, container, false);
121 mRecyclerView = mView.findViewById(R.id.myRecycler);
122 mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
123 return mView;
124 }
125
126
127 @Override
128 public void onActivityCreated(Bundle savedInstanceState) {
129 super.onActivityCreated(savedInstanceState);
130
131 Cursor cursor;
132 List<DatabaseModel> LineList = new ArrayList<>();
133 LineList.clear();
134 DatabaseHelper db = new DatabaseHelper(getContext());
135 //data from database is returned to m
136 final List<DatabaseModel> m = db.getAllUsers();
137 Log.w("SIZe", "" + m.size());
138 //if m contains data
139 if (m.size() > 0) {
140 //loop through contents
141 for (int i = 0; i < m.size(); i++) {
142 Log.w("NAME", m.get(i).getTime());
143 //add data to list used in adapter
144 LineList.add(m.get(i));
145 //notify data change
146 mAdapter.notifyDataSetChanged();
147 }
148 }
149
150
151
152 db.close();
153
154
155
156 mAdapter = new DataAdapter(mActivity, LineList);
157 mRecyclerView.setAdapter(mAdapter);
158}
159
160
161 public void onRecyclerClicked(View v, int position) {
162
163 }
164
165}
166
167public class DatabaseHelper extends SQLiteOpenHelper {
168 private static final int DATABASE_VERSION = 1;
169 private static final String DATABASE_NAME = "MetroDB";//name of the database
170 private static final String TABLE_NAME = "stationtime";//name for the table
171
172 private static final String Station = "station";
173 private static final String Time = "time";
174 private static final String Line = "line";
175
176 public DatabaseHelper(Context context) {
177 super(context, DATABASE_NAME, null, DATABASE_VERSION);
178 }
179
180 @Override
181 public void onCreate(SQLiteDatabase db) {
182
183 //Query to create table in databse
184 String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + Station + " TEXT," + Time + " TEXT," + Line + " TEXT);";
185 db.execSQL(CREATE_CONTACTS_TABLE);
186 }
187
188 //Executes once a database change is occurred
189 @Override
190 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
191 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
192 onCreate(db);
193 }
194
195 //method to add row to table
196 void addToLine1(DatabaseModel m) {
197 SQLiteDatabase db = this.getWritableDatabase();
198 ContentValues values = new ContentValues();
199
200 values.put(Station, m.getStation());
201
202 values.put(Time, m.getTime());
203
204 values.put(Line, m.getLine());
205
206
207 db.insert(TABLE_NAME, null, values);
208 db.close();
209 }
210
211
212 public void insertIntoDB(String station,String time,String line) {
213
214 // 1. get reference to writable DB
215 SQLiteDatabase db1 = this.getWritableDatabase();
216
217 // 2. create ContentValues to add key "column"/value
218 ContentValues values = new ContentValues();
219 values.put("station", station);
220 values.put("time", time);
221 values.put("line", line);
222
223 // 3. insert
224 db1.insert(TABLE_NAME, null, values);
225 // 4. close
226 db1.close();
227 }
228
229 //method to list all details from table
230 public List<DatabaseModel> getAllUsers() {
231 List<DatabaseModel> contactList = new ArrayList<DatabaseModel>();
232 String selectQuery = "SELECT " + Time + " FROM " + TABLE_NAME + ";";//retrieve data from the database
233 SQLiteDatabase db = this.getReadableDatabase();
234 Cursor cursor = db.rawQuery(selectQuery, null);
235
236 cursor.moveToFirst();
237 if (cursor.getCount()>0) {
238 do {
239 DatabaseModel m = new DatabaseModel();
240// m.setStation(cursor.getString(0));
241 m.setTime(cursor.getString(1));
242 // m.setLine(cursor.getString(2));
243 contactList.add(m);
244 } while (cursor.moveToNext());
245 }
246
247
248
249 return contactList;
250 }
251
252 }
253
254public class DatabaseModel {
255 String station,time,line;
256
257 public DatabaseModel()
258 {
259 }
260
261 public DatabaseModel(String station, String time, String line) {
262 this.station = station;
263 this.time = time;
264 this.line = line;
265 }
266
267
268 public String getStation() {
269 return station;
270 }
271
272 public void setStation(String station) {
273 this.station = station;
274 }
275
276 public String getTime() {
277 return time;
278 }
279
280 public void setTime(String time) {
281 this.time = time;
282 }
283
284 public String getLine() {
285 return line;
286 }
287
288 public void setLine(String line) {
289 this.line = line;
290 }
291
292}