用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

sql list

2016-12-06 作者: 新世界举报

[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>



网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...