-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from CaptainHuangsh/14-city-data
14 city data
- Loading branch information
Showing
19 changed files
with
751 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/example/owen/weathergo/common/util/DBManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.example.owen.weathergo.common.util; | ||
|
||
import android.content.Context; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.os.Environment; | ||
import android.support.annotation.Nullable; | ||
import android.util.Log; | ||
|
||
import com.example.owen.weathergo.R; | ||
import com.example.owen.weathergo.base.BaseApplication; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by owen on 2017/4/25. | ||
*/ | ||
|
||
public class DBManager { | ||
|
||
private static String TAG = DBManager.class.getSimpleName(); | ||
public static final String DB_NAME = "china_city.db"; //数据库名字 | ||
public static final String PACKAGE_NAME = "com.example.owen.weathergo"; | ||
public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + | ||
PACKAGE_NAME; //在手机里存放数据库的位置(/data/data/com.example.owen.weathergo/china_city.db) | ||
private SQLiteDatabase database; | ||
private Context context; | ||
|
||
private DBManager() { | ||
|
||
} | ||
|
||
public static DBManager getInstance() { | ||
return DBManagerHolder.sInstance; | ||
} | ||
|
||
private static final class DBManagerHolder { | ||
public static final DBManager sInstance = new DBManager(); | ||
} | ||
|
||
public SQLiteDatabase getDatabase() { | ||
return database; | ||
} | ||
|
||
|
||
public void openDatabase() { | ||
//Log.e(TAG, DB_PATH + "/" + DB_NAME); | ||
this.database = this.openDatabase(DB_PATH + "/" + DB_NAME); | ||
} | ||
|
||
@Nullable | ||
private SQLiteDatabase openDatabase(String dbfile) { | ||
|
||
try { | ||
if (!(new File(dbfile).exists())) { | ||
//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库 | ||
InputStream is = BaseApplication.getAppContext().getResources().openRawResource(R.raw.china_city); //欲导入的数据库 | ||
FileOutputStream fos = new FileOutputStream(dbfile); | ||
int BUFFER_SIZE = 400000; | ||
byte[] buffer = new byte[BUFFER_SIZE]; | ||
int count; | ||
while ((count = is.read(buffer)) > 0) { | ||
fos.write(buffer, 0, count); | ||
} | ||
fos.close(); | ||
is.close(); | ||
} | ||
return SQLiteDatabase.openOrCreateDatabase(dbfile, null); | ||
} catch (FileNotFoundException e) { | ||
Log.e("File not found",""); | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
Log.e("IO exception",""); | ||
e.printStackTrace(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public void closeDatabase() { | ||
if (this.database != null) { | ||
this.database.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/example/owen/weathergo/common/util/WeatherDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.example.owen.weathergo.common.util; | ||
|
||
import android.database.Cursor; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.os.Build; | ||
import android.support.annotation.RequiresApi; | ||
|
||
import com.example.owen.weathergo.modules.dao.City; | ||
import com.example.owen.weathergo.modules.dao.Province; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
//import okhttp3.internal.Util; | ||
|
||
public class WeatherDB { | ||
|
||
public WeatherDB() { | ||
|
||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) | ||
public static List<Province> loadProvinces(SQLiteDatabase db) { | ||
|
||
List<Province> list = new ArrayList<>(); | ||
|
||
//DMBManager类复制数据库操作已再设备上验证成功(coolpad8297 API19) | ||
Cursor cursor = db.query("T_Province", null, null, null, null, null, null); | ||
|
||
if (cursor.moveToFirst()) { | ||
do { | ||
Province province = new Province(); | ||
province.setProSort(cursor.getInt(cursor.getColumnIndex("ProSort"))); | ||
province.setProName(cursor.getString(cursor.getColumnIndex("ProName"))); | ||
list.add(province); | ||
} while (cursor.moveToNext()); | ||
} | ||
closeQuietly(cursor); | ||
return list; | ||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) | ||
public static List<City> loadCities(SQLiteDatabase db, int ProID) { | ||
List<City> list = new ArrayList<>(); | ||
Cursor cursor = db.query("T_City", null, "ProID = ?", new String[] { String.valueOf(ProID) }, null, null, null); | ||
if (cursor.moveToFirst()) { | ||
do { | ||
City city = new City(); | ||
city.setCityName(cursor.getString(cursor.getColumnIndex("CityName"))); | ||
city.setProID(ProID); | ||
city.setCitySort(cursor.getInt(cursor.getColumnIndex("CitySort"))); | ||
list.add(city); | ||
} while (cursor.moveToNext()); | ||
} | ||
closeQuietly(cursor); | ||
return list; | ||
} | ||
|
||
|
||
public static void closeQuietly(Closeable closeable) { | ||
if (null != closeable) { | ||
try { | ||
closeable.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/example/owen/weathergo/modules/dao/City.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.example.owen.weathergo.modules.dao; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Created by owen on 2017/4/25. | ||
*/ | ||
|
||
public class City implements Serializable { | ||
private String CityName; | ||
private int ProID; | ||
|
||
public String getCityName() { | ||
return CityName; | ||
} | ||
|
||
public void setCityName(String cityName) { | ||
CityName = cityName; | ||
} | ||
|
||
public int getProID() { | ||
return ProID; | ||
} | ||
|
||
public void setProID(int proID) { | ||
ProID = proID; | ||
} | ||
|
||
public int getCitySort() { | ||
return CitySort; | ||
} | ||
|
||
public void setCitySort(int citySort) { | ||
CitySort = citySort; | ||
} | ||
|
||
private int CitySort; | ||
} |
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/example/owen/weathergo/modules/dao/CityViewHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.example.owen.weathergo.modules.dao; | ||
|
||
import android.support.v7.widget.CardView; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
|
||
import com.example.owen.weathergo.R; | ||
import com.example.owen.weathergo.base.BaseViewHolder; | ||
import com.example.owen.weathergo.modules.main.adapter.CityAdapter; | ||
|
||
import butterknife.OnClick; | ||
|
||
/** | ||
* Created by owen on 2017/4/25. | ||
*/ | ||
|
||
public class CityViewHolder extends BaseViewHolder<String> implements View.OnClickListener{ | ||
|
||
TextView mItemCity; | ||
public CardView mCardView; | ||
private CityAdapter.OnRecyclerViewItemClickListener mOnItemClickListener = null; | ||
|
||
|
||
public CityViewHolder(View itemView) { | ||
super(itemView); | ||
mCardView = (CardView) itemView.findViewById(R.id.city_cardView); | ||
mItemCity = (TextView) itemView.findViewById(R.id.city_txt); | ||
mCardView.setOnClickListener((View.OnClickListener) this); | ||
|
||
} | ||
|
||
@Override | ||
public void bind(String s) { | ||
Log.i("CityViewHolderBind",""+s); | ||
// mItemCity.setText("城市"); | ||
mItemCity.setText(s); | ||
} | ||
|
||
|
||
@Override | ||
public void onClick(View v) { | ||
if(mOnItemClickListener != null){ | ||
mOnItemClickListener.onItemClick(v,(Integer) itemView.getTag()); | ||
Log.i("CityViewHolderOnclivk",""); | ||
} | ||
} | ||
|
||
|
||
public interface OnRecyclerViewItemClickListener { | ||
void onItemClick(View view, int pos); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/example/owen/weathergo/modules/dao/Province.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.owen.weathergo.modules.dao; | ||
|
||
/** | ||
* Created by owen on 2017/4/25. | ||
*/ | ||
|
||
public class Province { | ||
private String ProName; | ||
private int ProSort; | ||
|
||
public String getProName() { | ||
return ProName; | ||
} | ||
|
||
public void setProName(String proName) { | ||
ProName = proName; | ||
} | ||
|
||
public int getProSort() { | ||
return ProSort; | ||
} | ||
|
||
public void setProSort(int proSort) { | ||
ProSort = proSort; | ||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.