first
This commit is contained in:
80
main/java/com/hty/browser/DBHelper.java
Normal file
80
main/java/com/hty/browser/DBHelper.java
Normal file
@@ -0,0 +1,80 @@
|
||||
package com.hty.browser;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
public class DBHelper extends SQLiteOpenHelper {
|
||||
|
||||
public static final String DATABASE_NAME = MainActivity.dir + File.separator + "webfav.db";
|
||||
private final static int VERSION = 1;
|
||||
String TableName = "webfav";
|
||||
private SQLiteDatabase db;
|
||||
private static DBHelper mInstance = null;
|
||||
|
||||
public DBHelper(Context context) {
|
||||
super(context, DATABASE_NAME, null, VERSION);
|
||||
}
|
||||
|
||||
public static synchronized DBHelper getInstance(Context context) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new DBHelper(context);
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
this.db = db;
|
||||
db.execSQL("CREATE TABLE webfav (_id INTEGER PRIMARY KEY ,website TEXT,title TEXT)");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// db.execSQL("DROP TABLE IF EXISTS battery");
|
||||
// onCreate(db);
|
||||
switch (newVersion) {
|
||||
case 6:
|
||||
db.execSQL("alter table battery rename to battery_temp");
|
||||
db.execSQL("CREATE TABLE battery (_id INTEGER PRIMARY KEY ,time TEXT,level INTEGER, voltage INTEGER, current INTEGER , temperature INTEGER, cpu INTEGER)");
|
||||
db.execSQL("insert into battery select *,'' from battery_temp");
|
||||
db.execSQL("drop table battery_temp");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void insert(ContentValues values) {
|
||||
db = getWritableDatabase();
|
||||
db.insert(TableName, null, values);
|
||||
db.close();
|
||||
}
|
||||
|
||||
public Cursor query(String url) {
|
||||
db = getWritableDatabase();
|
||||
Cursor c = null;
|
||||
if (url.equalsIgnoreCase("")) {
|
||||
c = db.query(TableName, null, null, null, null, null, "_id desc");
|
||||
} else {
|
||||
c = db.query(TableName, null, "website=?", new String[] { url }, null, null, "_id desc");
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
public void del(int id) {
|
||||
if (db == null)
|
||||
db = getWritableDatabase();
|
||||
db.delete(TableName, "_id=?", new String[] { String.valueOf(id) });
|
||||
// Log.e("id", id + "");
|
||||
// db.ExecuteNonQuery(CommandType.Text, "VACUUM");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (db != null)
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
93
main/java/com/hty/browser/FavoriteActivity.java
Normal file
93
main/java/com/hty/browser/FavoriteActivity.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package com.hty.browser;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Bundle;
|
||||
import android.view.ContextMenu;
|
||||
import android.view.ContextMenu.ContextMenuInfo;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.View.OnCreateContextMenuListener;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.AdapterContextMenuInfo;
|
||||
import android.widget.AdapterView.OnItemClickListener;
|
||||
import android.widget.ListView;
|
||||
import android.widget.SimpleCursorAdapter;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class FavoriteActivity extends Activity {
|
||||
SimpleCursorAdapter adapter;
|
||||
ListView listView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_favorite);
|
||||
DBHelper helper = new DBHelper(this);
|
||||
Cursor c = helper.query("");
|
||||
String[] from = { "_id", "title", "website" };
|
||||
int[] to = { R.id.id, R.id.title, R.id.website };
|
||||
adapter = new SimpleCursorAdapter(this, R.layout.favorite_row, c, from, to, 0);
|
||||
listView = (ListView) this.findViewById(R.id.listView1);
|
||||
listView.setAdapter(adapter);
|
||||
listView.setDivider(new ColorDrawable(Color.GREEN));
|
||||
listView.setDividerHeight(2);
|
||||
listView.setOnItemClickListener(new OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
|
||||
String url = ((TextView) arg1.findViewById(R.id.website)).getText().toString();
|
||||
Intent intent = new Intent(FavoriteActivity.this, MainActivity.class);
|
||||
intent.putExtra("url", url);
|
||||
setResult(RESULT_OK, intent);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
||||
listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
|
||||
@Override
|
||||
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
|
||||
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
|
||||
String title = ((TextView) info.targetView.findViewById(R.id.title)).getText().toString();
|
||||
menu.setHeaderTitle(title);
|
||||
menu.add(0, 0, 0, "复制链接");
|
||||
menu.add(0, 1, 1, "删除");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onContextItemSelected(MenuItem item) {
|
||||
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
|
||||
switch (item.getItemId()) {
|
||||
case 0:
|
||||
ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
|
||||
String url = ((TextView) menuInfo.targetView.findViewById(R.id.website)).getText().toString();
|
||||
cm.setPrimaryClip(ClipData.newPlainText("link", url));
|
||||
Toast.makeText(getApplicationContext(), "链接已复制", Toast.LENGTH_SHORT).show();
|
||||
break;
|
||||
case 1:
|
||||
int id = Integer.parseInt(((TextView) menuInfo.targetView.findViewById(R.id.id)).getText()
|
||||
.toString());
|
||||
DBHelper helper = new DBHelper(getApplicationContext());
|
||||
helper.del(id);
|
||||
Cursor c = helper.query("");
|
||||
String[] from = { "_id", "title", "website" };
|
||||
int[] to = { R.id.id, R.id.title, R.id.website };
|
||||
adapter = new SimpleCursorAdapter(this, R.layout.favorite_row, c, from, to, 0);
|
||||
listView.setAdapter(adapter);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void favback(View v) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
1031
main/java/com/hty/browser/MainActivity.java
Normal file
1031
main/java/com/hty/browser/MainActivity.java
Normal file
File diff suppressed because it is too large
Load Diff
37
main/java/com/hty/browser/SettingsActivity.java
Normal file
37
main/java/com/hty/browser/SettingsActivity.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.hty.browser;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.os.Bundle;
|
||||
import android.preference.EditTextPreference;
|
||||
import android.preference.PreferenceActivity;
|
||||
|
||||
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{
|
||||
|
||||
private EditTextPreference ETP_homepage;
|
||||
SharedPreferences sharedPreferences;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.preference);
|
||||
ETP_homepage = (EditTextPreference) findPreference("homepage");
|
||||
sharedPreferences = getPreferenceScreen().getSharedPreferences();
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
// Activity启动时,修改列表项目值
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
ETP_homepage.setSummary(sharedPreferences.getString("homepage","http://www.baidu.com"));
|
||||
}
|
||||
|
||||
@Override
|
||||
// 编辑后确定,修改列表项目值
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if(key.equals("homepage")){
|
||||
ETP_homepage.setSummary(sharedPreferences.getString(key,""));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user