Skip to content

Commit

Permalink
Merge pull request #21 from CaptainHuangsh/14-city-data
Browse files Browse the repository at this point in the history
14 city data
  • Loading branch information
CaptainHuangsh authored Apr 28, 2017
2 parents 61da648 + c6f65bb commit a41dd53
Show file tree
Hide file tree
Showing 19 changed files with 751 additions and 49 deletions.
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ android {
versionName '1.0.1'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
/*jackOptions {
enabled true
}*/
// 适用java8属性
}
buildTypes {
release {
Expand All @@ -31,6 +35,10 @@ android {
}
productFlavors {
}
compileOptions {
targetCompatibility 1.7
sourceCompatibility 1.7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
Expand Down Expand Up @@ -68,6 +76,7 @@ dependencies {
compile 'com.jakewharton:butterknife:8.5.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:support-vector-drawable:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
debugCompile 'com.github.moduth:blockcanary-android:1.2.1'
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme0">
<activity android:name=".modules.main.activity.WeatherMain">
<activity android:name=".modules.main.activity.WeatherMain"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -48,8 +49,10 @@
//key:开发者申请的Key
<activity
android:name=".modules.main.activity.SettingsActivity"
android:label="@string/title_activity_settings"></activity>
<activity android:name=".modules.about.About"></activity>
android:label="@string/title_activity_settings" />
<activity android:name=".modules.about.About" />
<activity android:name=".modules.main.activity.ChoiceCityActivity"
android:launchMode="singleTask"></activity>
</application>

</manifest>
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class JSONUtil {
static ArrayList<DailyForecast> dfLists = new ArrayList<DailyForecast>();

public static WeatherBean getWeatherBeans(final Context context, String sCity) {
dfLists = null;
/**
* 处理从heweather网站上获取的json代码,进行解析赋值操作的静态方法
*/
Expand All @@ -49,7 +50,7 @@ public static WeatherBean getWeatherBeans(final Context context, String sCity) {
.build();
//Retrofit创建一个BlogService的代理对象
WgClient service = retrofit.create(WgClient.class);
Call<ResponseBody> call = service.mWeatherAPI("kaifeng", "b2a628bc1de942dc869fcbe524c65313");
Call<ResponseBody> call = service.mWeatherAPI(sCity, "b2a628bc1de942dc869fcbe524c65313");
String jss = "";
call.enqueue(new Callback<ResponseBody>() {
String jsonText = "";
Expand Down
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 app/src/main/java/com/example/owen/weathergo/modules/dao/City.java
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;
}
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);
}
}
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;
}



}
Loading

0 comments on commit a41dd53

Please sign in to comment.