У меня есть активность с буксирными вкладками (с viewPager).Внутри viewPager есть фрагмент с RecyclerView.
первая вкладка для (понедельник-пятница) и вторая вкладка для (суббота-воскресенье)
Я хочу получить данные из базы данных и показать вреселлервью на каждой вкладке.но я не могу обновить свой recyclerView.Пожалуйста, помогите мне сделать это. И еще одна проблема заключается в том, что когда RecyclerView показывают, данные показывают только первую строку, но количество строк в таблице. Например:
10: 06: 00
10:06:00
10: 06: 00
10: 06: 00
10: 06: 00
10: 06: 00
вот мой код:
это мой код фрагмента:
public class toDastgheybFragment extends BaseFragment {
private List<DatabaseModel> Times = new ArrayList<DatabaseModel>();
DatabaseHelper databaseHelper;
private View mView;
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_to_dastgheyb, container,
false);
databaseHelper = new DatabaseHelper(getContext());
Times = databaseHelper.getAllUsers();
mRecyclerView = mView.findViewById(R.id.myRecycler);
mAdapter = new DataAdapter(Times);
mRecyclerView.setLayoutManager(new
LinearLayoutManager(getContext()));
mRecyclerView.setAdapter(mAdapter);
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<DatabaseModel> LineList = new ArrayList<>();
LineList.clear();
mAdapter = new DataAdapter(LineList);
DatabaseHelper db = new DatabaseHelper(getContext());
final List<DatabaseModel> m = db.getAllUsers();
if (m.size() > 0) {
for (int i = 0; i < m.size(); i++) {
LineList.add(m.get(i));
mAdapter.notifyDataSetChanged();
}
}
db.close();
}
}
и это мой код DataHelper:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MetroDB";//name of the
database
private static final String TABLE_NAME = "stationtime";//name for the
table
static String db_path = "/data/data/ir.shirazmetro/databases/";
private static final String Station = "station";
private static final String Time = "time";
private static final String Line = "line";
private final Context context;
private SQLiteDatabase database;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " +
TABLE_NAME + "(" + Station + " TEXT," + Time + " TEXT," + Line + " TEXT)";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
private boolean checkExist() {
SQLiteDatabase db = null;
try {
db = SQLiteDatabase.openDatabase(db_path + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
}
return db != null;
}
private void copyDatabase() throws IOException {
OutputStream myOutput = new FileOutputStream(db_path +
DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
InputStream myInput = context.getAssets().open(DATABASE_NAME +
".db");
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
myOutput.flush();
myOutput.close();
}
public void importIfNotExist() throws IOException {
boolean dbExist = checkExist();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDatabase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void open() {
database = SQLiteDatabase.openDatabase(db_path + DATABASE_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
}
public List<DatabaseModel> getAllUsers() {
List<DatabaseModel> contactList = new ArrayList<DatabaseModel>();
String whatStation = C.whatStation;
String whatLine = C.whatLine;
String selectQuery = "SELECT " + Time + " FROM " + TABLE_NAME + "
WHERE " + Station + " LIKE '%" + whatStation + "%' AND " + Line + " LIKE '%"
+ whatLine + "%'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
do {
DatabaseModel m = new DatabaseModel();
m.setTime(cursor.getString(cursor.getColumnIndex(Time)));
contactList.add(m);
} while (cursor.moveToNext());
}
return contactList;
}
}
и, наконец, этомой код активности:
public class station extends BaseActivity {
Toolbar mToolbar;
private TabLayout tbLayout;
private ViewPager vPager;
RecyclerView.Adapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_station);
final DatabaseHelper helper = new DatabaseHelper(this);
try {
helper.importIfNotExist();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
mToolbar = findViewById(R.id.tlbr1);
setSupportActionBar(mToolbar);
initView();
setupWithViewPager();
tbLayout.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
public void line1(View view) {
Intent line1 = new Intent(this, line1.class);
startActivity(line1);
}
private void setupWithViewPager() {
BasePagerAdapter basePagerAdapter = new BasePagerAdapter(this,
getSupportFragmentManager());
vPager.setAdapter(basePagerAdapter);
tbLayout.setupWithViewPager(vPager);
}
private void initView() {
vPager = findViewById(R.id.view_pager);
tbLayout = findViewById(R.id.tab_layout);
backBtn = findViewById(R.id.backBtn);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.backBtn:
line1(view);
return;
}
}
}