[android]代码库
package vedios1com.android.com.preferenceactivity;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sql sq=new sql(this);
SQLiteDatabase sqlRead= sq.getReadableDatabase();
Cursor c=sqlRead.query("user",null,null,null,null,null,null);
adapter=new SimpleCursorAdapter(this,R.layout.list_cell,c,new String[]{"name","sex"},new int[]{R.id.text1,R.id.text});
setListAdapter(adapter);
// 写入数据
// SQLiteDatabase sqLiteDatabase=sq.getWritableDatabase();
// ContentValues cv=new ContentValues();
// cv.put("name","张");
// cv.put("sex","17");
// sqLiteDatabase.insert("user",null,cv);
// cv.put("name","li");
// cv.put("sex","16");
// sqLiteDatabase.insert("user",null,cv);
// sqLiteDatabase.close();
// 读取数据
// SQLiteDatabase dbread=sq.getReadableDatabase();
// Cursor c=dbread.query("user",null,null,null,null,null,null);//不指定任何条件
// while (c.moveToNext()){
// String name=c.getString(c.getColumnIndex("name"));
// String age=c.getString(c.getColumnIndex("sex"));
// Toast.makeText(getApplicationContext(),name+age,Toast.LENGTH_LONG).show();
// }
}
}
package vedios1com.android.com.preferenceactivity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.preference.PreferenceActivity;
/**
* Created by Administrator on 2016/12/5 0005.
*/
public class sql extends SQLiteOpenHelper {
public sql(Context context) {
super(context, "db", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"name TEXT DEFAULT \"\"," +
"sex TEXT DEFAULT \"\")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大的"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="小的"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:id="@android:id/list"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
by: 发表于:2017-10-18 09:19:55 顶(0) | 踩(0) 回复
??
回复评论