android查询SQLite中数据显示在textview中

    xiaoxiao2021-04-15  296

    效果图

    SQLite设置

    main_activity.xml

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="学号:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="密码:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="姓名:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="年级:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="专业:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="email:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="班级:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="辅导员:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="120dp" android:layout_height="wrap_content" android:text="指导老师:" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp"/> <TextView android:id="@+id/textView9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingTop="10dp" android:textColor="#2828ff" android:textSize="20sp" /> </LinearLayout>

    DBHelper

    public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) { super(context, "mysise.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql = "create table student_info(_id integer primary key autoincrement,school_num VARCHAR, password VARCHAR(64),name VARCHAR(64),grade VARCHAR(64),professional VARCHAR(64),email VARCHAR(64),classes VARCHAR(64),counselor VARCHAR(64),teacher VARCHAR(64))"; //要使用游标适配器,SQLite表格必须包含一栏“_id” db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub }

    }

    MainActivity.java

    import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.SimpleCursorAdapter; import android.widget.TextView;

    public class MainActivity extends Activity {

    private DBHelper dbHelper; private SQLiteDatabase database; private SimpleCursorAdapter adapter;// 简单的游标适配器 private TextView textView1,textView2,textView3,textView4,textView5,textView6,textView7,textView8,textView9; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = findViewById(R.id.textView1); textView2 = findViewById(R.id.textView2); textView3 = findViewById(R.id.textView3); textView4 = findViewById(R.id.textView4); textView5 = findViewById(R.id.textView5); textView6 = findViewById(R.id.textView6); textView7 = findViewById(R.id.textView7); textView8 = findViewById(R.id.textView8); textView9 = findViewById(R.id.textView9); dbHelper = new DBHelper(this); database = dbHelper.getReadableDatabase(); Cursor c = database.query("student_info", null, "_id=2", null, null, null, null); c.moveToFirst(); textView1.setText(c.getString(c.getColumnIndex("school_num"))); textView2.setText(c.getString(c.getColumnIndex("password"))); textView3.setText(c.getString(c.getColumnIndex("name"))); textView4.setText(c.getString(c.getColumnIndex("grade"))); textView5.setText(c.getString(c.getColumnIndex("professional"))); textView6.setText(c.getString(c.getColumnIndex("email"))); textView7.setText(c.getString(c.getColumnIndex("classes"))); textView8.setText(c.getString(c.getColumnIndex("counselor"))); textView9.setText(c.getString(c.getColumnIndex("teacher"))); c.close(); }

    }


    最新回复(0)