首次设置:
final SharedPreferences preferences = getSharedPreferences("sql",0);
final SharedPreferences.Editor editor = preferences.edit();
final int first = preferences.getInt("first", 0);
if (first == 0) {
ContentValues values = new ContentValues();
ContentValues values1 = new ContentValues();
ContentValues values2 = new ContentValues();
ContentValues values3 = new ContentValues();
ContentValues values4 = new ContentValues();
values.put("username","admin");
values.put("password","123");
sql.insert("user",values);
values1.put("gg","公告:");
sql.insert("gg",values1);
values2.put("lm","栏目");
sql.insert("lm",values2);
values3.put("title","title");
values3.put("art","art");
values3.put("lm","栏目");
sql.insert("meg",values3);
values4.put("pl","pl");
values4.put("title","title");
sql.insert("pl",values4);
editor.putInt("first",1);
editor.commit();
}
listview的使用:
final CharSequence[] list = sql.read("lm","lm");
CharSequence[] listgg = sql.read("gg","gg");
listView.setAdapter(new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, listgg));
动态添加按钮或者textview:
for(int i =0; i<list.length;i++){
settx(list[i].toString());
list1 =sql.list("title","meg","lm",list[i].toString());
for(int j =0; j<list1.length;j++){setbt(list1[j].toString());}
}
public void setbt(final String title){
LinearLayout ll = (LinearLayout)findViewById(R.id.view);
final TextView bt = new TextView(this);
bt.setText(title);
bt.setGravity(Gravity.LEFT);
bt.setTextSize(22);
ll. addView ( bt );
bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.button2));
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("sql",0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("title",title);
editor.commit();
startActivity(new Intent(MainActivity.this, art.class));
}
});
}
public void settx(String lm){
LinearLayout ll = (LinearLayout)findViewById(R.id.view);
TextView tx = new TextView(this);
tx.setText(lm);
tx.setTextSize(26);
//tx.getBackground().setAlpha(10);
tx.setGravity(Gravity.CENTER);
tx.setBackgroundDrawable(getResources().getDrawable(R.drawable.button1));
ll. addView(tx);
}
这个是百度出来的scrollview嵌套listview高度不确定的修正:
fixListViewHeight(listview);
public void fixListViewHeight(ListView listView) {
// 如果没有设置数据适配器,则ListView没有子项,返回。
ListAdapter listAdapter = listView.getAdapter();
int totalHeight = 0;
if (listAdapter == null) {
return;
}
for (int index = 0, len = listAdapter.getCount(); index < len; index++) {
View listViewItem = listAdapter.getView(index , null, listView);
// 计算子项View 的宽高
listViewItem.measure(0, 0);
// 计算所有子项的高度和
totalHeight += listViewItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
// listView.getDividerHeight()获取子项间分隔符的高度
// params.height设置ListView完全显示需要的高度
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
连接sqlite的使用,刚学写不怎么好
public class sql extends SQLiteOpenHelper{
public sql(Context context) {
super(context, "news.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table user(username varchar(20) not null , password varchar(10) not null);";
String sql1 = "create table gg(gg varchar(200) not null);";
String sql2 = "create table lm(lm varchar(20) not null);";
String sql3 = "create table meg(title varchar(200) not null , art varchar(200) not null , lm varchar(20) not null);";
String sql4 = "create table pl(pl varchar(200) not null , title varchar(200) not null);";
db.execSQL(sql);
db.execSQL(sql1);
db.execSQL(sql2);
db.execSQL(sql3);
db.execSQL(sql4);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + "user";
String sql1 = "DROP TABLE IF EXISTS " + "gg";
String sql2 = "DROP TABLE IF EXISTS " + "lm";
String sql3 = "DROP TABLE IF EXISTS " + "meg";
String sql4 = "DROP TABLE IF EXISTS " + "pl";
db.execSQL(sql);
db.execSQL(sql1);
db.execSQL(sql2);
db.execSQL(sql3);
db.execSQL(sql4);
onCreate(db);
}
public void insert(String table ,ContentValues values){
SQLiteDatabase db = this.getWritableDatabase();
db.insert(table, null, values);
}
public void delete(String table,String lm , String name){
SQLiteDatabase db = this.getWritableDatabase();
String where = lm+" =? ";
String[] args ={name};
db.delete(table,where,args);
}
public void updata(String table,String lm,String name,ContentValues values){
SQLiteDatabase db = this.getWritableDatabase();
String where = lm+" =? ";
String[] args ={name};
db.update(table,values,where,args);
}
public CharSequence[] read(String table , String path){
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.query(table,new String[]{path},null,null,null,null,null);
c.moveToFirst();
CharSequence[] list =new CharSequence[c.getCount()];
for (int i=0;i<list.length;i++){
list[i]=c.getString(0);
c.moveToNext();
}
return list;
}
public String check(String table ,String username , String user){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select password from " +table+" where "+username+" like ?",new String[]{"%"+user+"%"});
cursor.moveToFirst();
if (cursor.getCount()==0){return "flase";}
else{
String password = cursor.getString(0);
cursor.close();
return password;}
}
public CharSequence[] list(String sle , String table,String lm, String user){
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("select "+sle+" from " +table+" where "+lm+" like ?",new String[]{"%"+user+"%"});
c.moveToFirst();
CharSequence[] list =new CharSequence[c.getCount()];
for (int i=0;i<list.length;i++){
list[i]=c.getString(0);
c.moveToNext();
}
return list;
}
}