se duplican las tablas
Publicado por srtaQuimica (1 intervención) el 11/04/2018 10:45:37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main=this;
databasesDir = this.getApplicationInfo().dataDir + "/databases/";
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, null, 1);
SQLiteDatabase db = admin.getReadableDatabase();
db.close();
if (Build.VERSION.SDK_INT >= 23){
if (checkPermission()){
DownloadFileFromURL d = new DownloadFileFromURL(this);
d.execute();
} else {
requestPermission();
}
}
else
{
DownloadFileFromURL d = new DownloadFileFromURL(this);
d.execute();
}
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tabs);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
if (toolbar != null) {
setSupportActionBar(toolbar);
}
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(
tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
List = new String[4];
List[0] = getString(R.string.mapa);
List[1] = getString(R.string.farmacias);
List[2] = getString(R.string.favoritos);
List[3] = getString(R.string.Mas);
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
return (result == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(MainActivity.this, "Write External Storage permission allows us to do store images. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (permissions.length == 1 &&
permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
DownloadFileFromURL d = new DownloadFileFromURL(this);
d.execute();
}
} else {
finish();
}
}
}
public void copyDatabase(){
OutputStream os = null;
FileInputStream is = null;
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+File.separator+"farmacias.db");
if(file.exists()){
try{
is = new FileInputStream(file);
os = new FileOutputStream(databasesDir + "FARMACIAS");
copyFile(os,is);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(os != null){
os.close();
}
if(is != null){
is.close();
}
}catch(IOException e){
}
}
}
}
private void copyFile(OutputStream os, InputStream is) throws IOException{
byte[] buffer = new byte[1024];
int length;
while((length =is.read(buffer))>0){
os.write(buffer,0,length);
}
os.flush();
}
private class MyAdapter extends FragmentPagerAdapter {
MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new FragmentMapa();
case 1 : return new FragmentFarmacias();
case 2 : return new FragmentFavoritos();
case 3 : return new FragmentMas();
}
PagerAdapter adapter = null;
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return null;
}
@Override
public int getCount() {
return int_items;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
TabLayout.Tab FragmentMapa = tabLayout.newTab();
FragmentMapa.setText(List[0]);
FragmentMapa.setIcon(android.R.drawable.ic_menu_mylocation);
tabLayout.addTab(FragmentMapa);
break;
case 1 :
TabLayout.Tab FragmentFarmacias = tabLayout.newTab();
FragmentFarmacias.setText(List[1]);
FragmentFarmacias.setIcon(R.drawable.cofbi);
tabLayout.addTab(FragmentFarmacias);
break;
case 2 :
TabLayout.Tab FragmentFavoritos = tabLayout.newTab();
FragmentFavoritos.setText(List[2]);
FragmentFavoritos.setIcon(android.R.drawable.btn_star_big_off);
tabLayout.addTab(FragmentFavoritos);
break;
case 3:
TabLayout.Tab FragmentMas = tabLayout.newTab();
FragmentMas.setText(List[3]);
FragmentMas.setIcon(android.R.drawable.ic_menu_agenda);
tabLayout.addTab(FragmentMas);
break;
}
return null;
}
}
}
El utlimo switch duplica las tabbar
Valora esta pregunta
0