保存 GridView 的位置
gridView.getFirstVisiblePosition();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex("name"));
String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
Toast.makeText(ListPage.this, img_url, Toast.LENGTH_SHORT).show();
grp = gridView.getFirstVisiblePosition();
Intent intent = new Intent(getApplicationContext(), Idolpiclist.class);
intent.putExtra("name", name);
startActivity(intent);
}
});
private class Myhand extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
cursor = (Cursor) msg.obj;
System.out.println(cursor.getCount());
GridView gridView = (GridView) findViewById(R.id.List_page_gridview);
gridView.setSelection(grp+1);
gridView.setAdapter(new Myadapter());
}
}
2016年12月1日木曜日
android.os.Process.setThreadPriority(-8);
android.os.Process.setThreadPriority(-8);
private class MyThread extends Thread {
@Override
public void run() {
android.os.Process.setThreadPriority(-8);
super.run();
FlickrSQL flickrSQL = new FlickrSQL(getApplicationContext());
Message message = myhand.obtainMessage();
message.obj = flickrSQL.SelectallIdol();
myhand.sendMessage(message);
}
}
private class MyThread extends Thread {
@Override
public void run() {
android.os.Process.setThreadPriority(-8);
super.run();
FlickrSQL flickrSQL = new FlickrSQL(getApplicationContext());
Message message = myhand.obtainMessage();
message.obj = flickrSQL.SelectallIdol();
myhand.sendMessage(message);
}
}
Android getDrawable,getColor 过时的替代方法
Android getDrawable,getColor 过时的替代方法
public class ContextCompat{}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_main);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
relativeLayout.addView(linearLayout);
for (int i = 0; i < 3; i++) {
ImageView imageView = new ImageView(this);
// 过时
// imageView.setImageDrawable(getDrawable(R.drawable.android));
// 替代方法
imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.firefox));
TextView textView = new TextView(this);
textView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_orange_dark));
textView.setText(String.valueOf(i));
imageView.setId(i);
textView.setId(i);
linearLayout.addView(imageView);
linearLayout.addView(textView);
imageView.setOnClickListener(this);
textView.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
String idinfo = "";
switch (v.getId()) {
case 1:
idinfo = "Button1";
break;
case 2:
idinfo = "Button2";
break;
case 0:
idinfo = "Button1";
break;
}
Toast.makeText(this, idinfo, Toast.LENGTH_SHORT).show();
}
}
public class ContextCompat{}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_main);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
relativeLayout.addView(linearLayout);
for (int i = 0; i < 3; i++) {
ImageView imageView = new ImageView(this);
// 过时
// imageView.setImageDrawable(getDrawable(R.drawable.android));
// 替代方法
imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.firefox));
TextView textView = new TextView(this);
textView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.holo_orange_dark));
textView.setText(String.valueOf(i));
imageView.setId(i);
textView.setId(i);
linearLayout.addView(imageView);
linearLayout.addView(textView);
imageView.setOnClickListener(this);
textView.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
String idinfo = "";
switch (v.getId()) {
case 1:
idinfo = "Button1";
break;
case 2:
idinfo = "Button2";
break;
case 0:
idinfo = "Button1";
break;
}
Toast.makeText(this, idinfo, Toast.LENGTH_SHORT).show();
}
}
2016年11月30日水曜日
Java BackCall 的返回值运行在新的Thread上。
Java BackCall 的返回值运行在新的Thread上。
所以,在Android 上不能咋 BackCall上更新 主线程。
https://paiza.io/projects/dBt6EFZZnt8GdS0MDW8I-g
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// Here your code !
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Handel h = new Handel();
System.out.println("Call by BackCall thread name >>" + Thread.currentThread().getName());
h.Down(new Handel.BackCall() {
public void restout(String xx) {
System.out.println("BackCall thread name >>" + Thread.currentThread().getName());
System.out.println(xx);
}
});
}
}
class Handel {
public interface BackCall {
public void restout (String json);
}
public void Down(BackCall bc) {
new Thread(new Runnable() {
public void run() {
try {
URL u = new URL("http://www.yahoo.co.jp");
HttpURLConnection c = (HttpURLConnection)u.openConnection();
c.setConnectTimeout(3000);
c.setReadTimeout(3000);
c.setDoInput(true);
c.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040225 Firefox/0.8");
InputStream is = null;
c.setRequestMethod("GET");
String bbb = "";
if (c.getResponseCode() == 200) {
is = c.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(is));
bbb = br.readLine();
}
bc.restout(bbb);
} catch (Exception e) {
e.printStackTrace();
}
}
} ).start();
}
}
所以,在Android 上不能咋 BackCall上更新 主线程。
https://paiza.io/projects/dBt6EFZZnt8GdS0MDW8I-g
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// Here your code !
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Handel h = new Handel();
System.out.println("Call by BackCall thread name >>" + Thread.currentThread().getName());
h.Down(new Handel.BackCall() {
public void restout(String xx) {
System.out.println("BackCall thread name >>" + Thread.currentThread().getName());
System.out.println(xx);
}
});
}
}
class Handel {
public interface BackCall {
public void restout (String json);
}
public void Down(BackCall bc) {
new Thread(new Runnable() {
public void run() {
try {
URL u = new URL("http://www.yahoo.co.jp");
HttpURLConnection c = (HttpURLConnection)u.openConnection();
c.setConnectTimeout(3000);
c.setReadTimeout(3000);
c.setDoInput(true);
c.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040225 Firefox/0.8");
InputStream is = null;
c.setRequestMethod("GET");
String bbb = "";
if (c.getResponseCode() == 200) {
is = c.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(is));
bbb = br.readLine();
}
bc.restout(bbb);
} catch (Exception e) {
e.printStackTrace();
}
}
} ).start();
}
}
2016年11月29日火曜日
Android_Layout 用代码得到对象的高宽
Android_Layout 用代码得到对象的高宽
当设定 ViewGroup.LayoutParams 的设定优先。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(getApplicationContext());
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
// viewGroup.addView(button);
viewGroup.addView(button, layoutParams);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) ((Button) v).getLayoutParams();
System.out.println(rl.height);
System.out.println(rl.width);
System.out.println(((Button) v).getHeight());
System.out.println(((Button) v).getWidth());
}
});
}
当>>>>
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button, layoutParams);
结果
I/System.out: 500
I/System.out: 500
I/System.out: 500
I/System.out: 500
当>>>>
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button);
结果
I/System.out: -2
I/System.out: -2
I/System.out: 400
I/System.out: 400
当设定 ViewGroup.LayoutParams 的设定优先。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(getApplicationContext());
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
// viewGroup.addView(button);
viewGroup.addView(button, layoutParams);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) ((Button) v).getLayoutParams();
System.out.println(rl.height);
System.out.println(rl.width);
System.out.println(((Button) v).getHeight());
System.out.println(((Button) v).getWidth());
}
});
}
当>>>>
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button, layoutParams);
结果
I/System.out: 500
I/System.out: 500
I/System.out: 500
I/System.out: 500
当>>>>
button.setText("Button");
// ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(500, 500);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
button.setHeight(400);
button.setWidth(400);
viewGroup.addView(button);
结果
I/System.out: -2
I/System.out: -2
I/System.out: 400
I/System.out: 400
Android_Layout 用代码添加对象
Android LayoutParams
用代码添加对象(按键),并设置LayoutParames,
当按键Click时,变更LayoutParames 的值。
public class MainActivity extends AppCompatActivity {
/**
* https://www.youtube.com/watch?v=QSsvUbPVHOU&index=3&list=PLTstZD3AK3S9sR6uiKzmNtgnY5p4nMT3Z
*
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(getApplicationContext());
button.setText("Button");
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
// "NG>>" ViewGroup viewGroup = (ViewGroup) findViewById(R.layout.activity_main);
viewGroup.addView(button, layoutParams);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
((Button) v).setLayoutParams(layoutParams1);
// java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
// "NG>>" ViewGroup.LayoutParams layoutParams1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Button bbt = (Button) v;
// bbt.setLayoutParams(layoutParams1);
}
});
}
}
用代码添加对象(按键),并设置LayoutParames,
当按键Click时,变更LayoutParames 的值。
public class MainActivity extends AppCompatActivity {
/**
* https://www.youtube.com/watch?v=QSsvUbPVHOU&index=3&list=PLTstZD3AK3S9sR6uiKzmNtgnY5p4nMT3Z
*
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(getApplicationContext());
button.setText("Button");
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
// "NG>>" ViewGroup viewGroup = (ViewGroup) findViewById(R.layout.activity_main);
viewGroup.addView(button, layoutParams);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
((Button) v).setLayoutParams(layoutParams1);
// java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
// "NG>>" ViewGroup.LayoutParams layoutParams1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Button bbt = (Button) v;
// bbt.setLayoutParams(layoutParams1);
}
});
}
}
2016年11月27日日曜日
Android SQL
Android SQL
package com.example.e560.m1126a.ToolsClass;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Created by E560 on 2016/11/26.
*/
public class FlickrSQL {
/**
*
*/
private Context context;
private final String TAG = "FlickrSQL";
private final String JSON_PATH = "http://cdefgab.web.fc2.com/idolsname.json";
private final String DB_NAME = "IdolAPP.db";
private final int DB_VERSION = 1;
private mSQL mSQL_db;
/**
* @param context
*/
public FlickrSQL(Context context) {
this.context = context;
mSQL_db = new mSQL(context, DB_NAME, null, DB_VERSION);
}
/**
* 檢查Name是否存在。count = 1 存在,0 不存在。
*
* @param name
* @return
* @throws Exception
*/
public int check_name(String name) throws Exception {
int count = 0;
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "select name from toplist where name = ?";
Cursor cursor = db.rawQuery(sql_cmd, new String[]{name});
count = cursor.getCount();
cursor.close();
db.close();
return count;
}
/**
* 返回Name個數。
*
* @return
*/
public int db_count() {
int count = 0;
SQLiteDatabase db = mSQL_db.getReadableDatabase();
String sql_cmd = "select count(*) from toplist";
Cursor cursor = db.rawQuery(sql_cmd, null);
while (cursor.moveToNext()) {
count = Integer.parseInt(cursor.getString(cursor.getColumnIndex("count(*)")));
}
cursor.close();
Log.i(TAG, "db_idols_count: " + count);
db.close();
return count;
}
/**
* 返回所有條目。
*
* @return
*/
public Cursor Selectall() {
Cursor cursor = null;
SQLiteDatabase db = mSQL_db.getReadableDatabase();
String sql_cmd = "select * from toplist WHERE active NOT IN(?) ORDER BY up_date DESC ";
cursor = db.rawQuery(sql_cmd, new String[]{"false"});
return cursor;
}
/**
* 從JSON_PATH 更新客戶端數據庫。
* String JSON_PATH = "http://cdefgab.web.fc2.com/idolsname.json";
*
* @throws Exception
*/
public void sysdb() throws Exception {
int insertcount = 0;
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String json = FlickrHttp.GetResultstring(JSON_PATH);
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("idolnames");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
if (check_name(jsonObject1.getString("name")) == 1) {
update_active(jsonObject1.getString("name"), jsonObject1.getString("active"));
} else {
insertcount++;
insert(jsonObject1.getString("name"), jsonObject1.getString("img"));
update_active(jsonObject1.getString("name"), jsonObject1.getString("active"));
}
}
Log.i(TAG, "sysdb: new Insert " + insertcount);
db.close();
}
public void insert(String name, String img_url) {
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "insert into toplist(name,img_url) values (? ,?)";
db.execSQL(sql_cmd, new String[]{name, img_url});
db.close();
}
public void update_imgurl(String name, String img_url) {
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "update toplist set img_url = ? where name = ?";
db.execSQL(sql_cmd, new String[]{name, img_url});
db.close();
}
public void update_active(String name, String active) {
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "update toplist set active = ? where name = ?";
db.execSQL(sql_cmd, new String[]{active, name});
db.close();
}
public class mSQL extends SQLiteOpenHelper {
/**
* mSQL_db = new mSQL(context, db_pak_name, null, db_version);
* https://farm8.staticflickr.com/7566/15949681296_b6a869bdfd_q.jpg
* https://farm8.staticflickr.com/7569/15975428145_c85aae84bd_s.jpg
*/
public mSQL(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String topidoltable = "create table toplist (rowid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL,up_date TEXT default current_timestamp,img_url TEXT NOT NULL, accc INTEGER DEFAULT 0, Active TEXT DEFAULT 'ture')";
String setimgurl = "update toplist set img_url = ?";
db.execSQL(topidoltable);
db.execSQL(setimgurl, new String[]{"https://farm8.staticflickr.com/7569/15975428145_c85aae84bd_s.jpg"});
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
/**
* Created by E560 on 2016/11/26.
*/
public class test_FlickrSQL extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
new Th().start();
}
class Th extends Thread {
@Override
public void run() {
try {
FlickrSQL flickrSQL = new FlickrSQL(getApplicationContext());
// flickrSQL.sysdb();
// flickrSQL.db_count();
Cursor cursor = flickrSQL.Selectall();
while (cursor.moveToNext()) {
String rowid = cursor.getString(cursor.getColumnIndex("rowid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String up_date = cursor.getString(cursor.getColumnIndex("up_date"));
String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
String accc = cursor.getString(cursor.getColumnIndex("accc"));
String Active = cursor.getString(cursor.getColumnIndex("Active"));
System.out.println(rowid);
System.out.println(name);
System.out.println(up_date);
System.out.println(img_url);
System.out.println(accc);
System.out.println(Active);
}
// cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.example.e560.m1126a.ToolsClass;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Created by E560 on 2016/11/26.
*/
public class FlickrSQL {
/**
*
*/
private Context context;
private final String TAG = "FlickrSQL";
private final String JSON_PATH = "http://cdefgab.web.fc2.com/idolsname.json";
private final String DB_NAME = "IdolAPP.db";
private final int DB_VERSION = 1;
private mSQL mSQL_db;
/**
* @param context
*/
public FlickrSQL(Context context) {
this.context = context;
mSQL_db = new mSQL(context, DB_NAME, null, DB_VERSION);
}
/**
* 檢查Name是否存在。count = 1 存在,0 不存在。
*
* @param name
* @return
* @throws Exception
*/
public int check_name(String name) throws Exception {
int count = 0;
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "select name from toplist where name = ?";
Cursor cursor = db.rawQuery(sql_cmd, new String[]{name});
count = cursor.getCount();
cursor.close();
db.close();
return count;
}
/**
* 返回Name個數。
*
* @return
*/
public int db_count() {
int count = 0;
SQLiteDatabase db = mSQL_db.getReadableDatabase();
String sql_cmd = "select count(*) from toplist";
Cursor cursor = db.rawQuery(sql_cmd, null);
while (cursor.moveToNext()) {
count = Integer.parseInt(cursor.getString(cursor.getColumnIndex("count(*)")));
}
cursor.close();
Log.i(TAG, "db_idols_count: " + count);
db.close();
return count;
}
/**
* 返回所有條目。
*
* @return
*/
public Cursor Selectall() {
Cursor cursor = null;
SQLiteDatabase db = mSQL_db.getReadableDatabase();
String sql_cmd = "select * from toplist WHERE active NOT IN(?) ORDER BY up_date DESC ";
cursor = db.rawQuery(sql_cmd, new String[]{"false"});
return cursor;
}
/**
* 從JSON_PATH 更新客戶端數據庫。
* String JSON_PATH = "http://cdefgab.web.fc2.com/idolsname.json";
*
* @throws Exception
*/
public void sysdb() throws Exception {
int insertcount = 0;
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String json = FlickrHttp.GetResultstring(JSON_PATH);
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("idolnames");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
if (check_name(jsonObject1.getString("name")) == 1) {
update_active(jsonObject1.getString("name"), jsonObject1.getString("active"));
} else {
insertcount++;
insert(jsonObject1.getString("name"), jsonObject1.getString("img"));
update_active(jsonObject1.getString("name"), jsonObject1.getString("active"));
}
}
Log.i(TAG, "sysdb: new Insert " + insertcount);
db.close();
}
public void insert(String name, String img_url) {
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "insert into toplist(name,img_url) values (? ,?)";
db.execSQL(sql_cmd, new String[]{name, img_url});
db.close();
}
public void update_imgurl(String name, String img_url) {
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "update toplist set img_url = ? where name = ?";
db.execSQL(sql_cmd, new String[]{name, img_url});
db.close();
}
public void update_active(String name, String active) {
SQLiteDatabase db = mSQL_db.getWritableDatabase();
String sql_cmd = "update toplist set active = ? where name = ?";
db.execSQL(sql_cmd, new String[]{active, name});
db.close();
}
public class mSQL extends SQLiteOpenHelper {
/**
* mSQL_db = new mSQL(context, db_pak_name, null, db_version);
* https://farm8.staticflickr.com/7566/15949681296_b6a869bdfd_q.jpg
* https://farm8.staticflickr.com/7569/15975428145_c85aae84bd_s.jpg
*/
public mSQL(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String topidoltable = "create table toplist (rowid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL,up_date TEXT default current_timestamp,img_url TEXT NOT NULL, accc INTEGER DEFAULT 0, Active TEXT DEFAULT 'ture')";
String setimgurl = "update toplist set img_url = ?";
db.execSQL(topidoltable);
db.execSQL(setimgurl, new String[]{"https://farm8.staticflickr.com/7569/15975428145_c85aae84bd_s.jpg"});
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
/**
* Created by E560 on 2016/11/26.
*/
public class test_FlickrSQL extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
new Th().start();
}
class Th extends Thread {
@Override
public void run() {
try {
FlickrSQL flickrSQL = new FlickrSQL(getApplicationContext());
// flickrSQL.sysdb();
// flickrSQL.db_count();
Cursor cursor = flickrSQL.Selectall();
while (cursor.moveToNext()) {
String rowid = cursor.getString(cursor.getColumnIndex("rowid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String up_date = cursor.getString(cursor.getColumnIndex("up_date"));
String img_url = cursor.getString(cursor.getColumnIndex("img_url"));
String accc = cursor.getString(cursor.getColumnIndex("accc"));
String Active = cursor.getString(cursor.getColumnIndex("Active"));
System.out.println(rowid);
System.out.println(name);
System.out.println(up_date);
System.out.println(img_url);
System.out.println(accc);
System.out.println(Active);
}
// cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
登録:
投稿 (Atom)