Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - package com.example.day2_sqlite;
 - import android.database.Cursor;
 - import android.database.sqlite.SQLiteDatabase;
 - import android.support.v7.app.AppCompatActivity;
 - import android.os.Bundle;
 - import android.view.View;
 - import android.widget.EditText;
 - import android.widget.ListView;
 - import android.widget.SimpleCursorAdapter;
 - import android.widget.TextView;
 - import android.content.Context;
 - public class MainActivity extends AppCompatActivity {
 - private EditText ETNazv;
 - private EditText ETPrice;
 - private EditText ETYr;
 - private TextView tVHead;
 - DataBaseHelper DBHelper;
 - Cursor userCursor;
 - ListView lVDB;
 - SimpleCursorAdapter userAdapter;
 - SQLiteDatabase dbCars;
 - @Override
 - protected void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.activity_main);
 - ETNazv=(EditText)findViewById(R.id.EtName);
 - ETPrice=(EditText)findViewById(R.id.EtPrice);
 - ETYr=(EditText)findViewById(R.id.EtYear);
 - tVHead=(TextView)findViewById(R.id.tVHead);
 - lVDB = (ListView)findViewById(R.id.lVDB);
 - DBHelper = new DataBaseHelper(getApplicationContext());
 - }
 - public void onResume() {
 - super.onResume();
 - // открываем подключение
 - dbCars = DBHelper.getReadableDatabase();
 - //получаем данные из бд в виде курсора
 - userCursor = dbCars.rawQuery("select * from "+ DBHelper.TABLE, null);
 - // определяем, какие столбцы из курсора будут выводиться в ListView
 - String[] headers = new String[] {DBHelper.COLUMN_NAME,DBHelper.COLUMN_PRICE,
 - DBHelper.COLUMN_YEAR};
 - // создаем адаптер, передаем в него курсор
 - userAdapter = new SimpleCursorAdapter(this,
 - android.R.layout.two_line_list_item, userCursor, headers, new int[]{android.R.id.text1,
 - android.R.id.text2}, 0);
 - tVHead.setText("Найдено элементов: " + userCursor.getCount());
 - lVDB.setAdapter(userAdapter);
 - }
 - public void onDestroy(){
 - super.onDestroy();
 - dbCars.close();
 - userCursor.close();
 - }
 - public void OnClick_Open(View view) {
 - Cursor query = dbCars.rawQuery("SELECT * FROM cars;", null);
 - while(query.moveToNext()){
 - String car = query.getString(1);
 - double prc = query.getDouble(2);
 - String year= query.getString(3);
 - tVHead.append("Авто: " + car + "\n");
 - tVHead.append("Цена: " + prc + "\n");
 - tVHead.append("Год выпуска "+year+"\n");
 - }
 - query.close();
 - dbCars.close();
 - }
 - }
 - package com.example.day2_sqlite;
 - import android.content.Context;
 - import android.database.sqlite.SQLiteDatabase;
 - import android.database.sqlite.SQLiteOpenHelper;
 - /**
 - * Created by user220 on 30.03.2021.
 - */
 - public class DataBaseHelper extends SQLiteOpenHelper{
 - private static final String DATABASE_NAME = "app1.db"; //название бд
 - private static final int SCHEMA = 1; // версия базы данных
 - static final String TABLE = "cars"; // название таблицы в бд
 - // названия столбцов
 - public static final String COLUMN_ID = "id";
 - public static final String COLUMN_NAME = "name";
 - public static final String COLUMN_PRICE = "prc";
 - public static final String COLUMN_YEAR = "yrvp";
 - public DataBaseHelper(Context context) {
 - super(context, DATABASE_NAME, null, SCHEMA);
 - }
 - public void onCreate(SQLiteDatabase db) {
 - db.execSQL("CREATE TABLE "+TABLE+(COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT, " + COLUMN_PRICE + " REAL," + COLUMN_YEAR +"TEXT);"));
 - db.execSQL("INSERT INTO "+ TABLE +" (" + COLUMN_NAME + ", " + COLUMN_PRICE + ","+ COLUMN_YEAR+ ") VALUES ('Nissan X-Trail',1800000, '2020' );");
 - db.execSQL("INSERT INTO "+ TABLE +" (" + COLUMN_NAME + ", " + COLUMN_PRICE + ","+ COLUMN_YEAR+ ") VALUES ('Nissan Qashqai',1600000, '2020' );");
 - db.execSQL("INSERT INTO "+ TABLE +" (" + COLUMN_NAME + ", " + COLUMN_PRICE + ","+ COLUMN_YEAR+ ") VALUES ('Nissan Juke',1400000, '2020' );");
 - }
 - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 - db.execSQL("DROP TABLE IF EXISTS "+TABLE);
 - onCreate(db);
 - }
 - }
 - <?xml version="1.0" encoding="utf-8"?>
 - <android.support.constraint.ConstraintLayout
 - xmlns:android="http://schemas.android.com/apk/res/android"
 - xmlns:app="http://schemas.android.com/apk/res-auto"
 - xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 - android:layout_height="match_parent" tools:context="com.example.day2_sqlite.MainActivity">
 - <EditText
 - android:id="@+id/EtYear"
 - android:layout_width="350dp"
 - android:layout_height="47dp"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="8dp"
 - android:layout_marginTop="8dp"
 - android:ems="10"
 - android:inputType="textPersonName"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.333"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.433"
 - android:layout_marginStart="8dp"
 - android:layout_marginEnd="8dp" />
 - <TextView
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="Год выпуска"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.053"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.36" />
 - <TextView
 - android:id="@+id/textView2"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="Цена"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.045"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.194" />
 - <TextView
 - android:id="@+id/textView"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="Наименование"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.055"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.032" />
 - <EditText
 - android:id="@+id/EtName"
 - android:layout_width="350dp"
 - android:layout_height="47dp"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="8dp"
 - android:layout_marginTop="8dp"
 - android:ems="10"
 - android:inputType="textPersonName"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.072"
 - android:layout_marginStart="8dp"
 - android:layout_marginEnd="8dp" />
 - <EditText
 - android:id="@+id/EtPrice"
 - android:layout_width="350dp"
 - android:layout_height="47dp"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="8dp"
 - android:layout_marginTop="8dp"
 - android:ems="10"
 - android:inputType="textPersonName"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.333"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.258"
 - android:layout_marginStart="8dp"
 - android:layout_marginEnd="8dp" />
 - <Button
 - android:id="@+id/button"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="8dp"
 - android:layout_marginTop="8dp"
 - android:onClick="OnClick_Delete"
 - android:text="Удалить"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.971"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.559"
 - android:layout_marginStart="8dp"
 - android:layout_marginEnd="8dp" />
 - <Button
 - android:id="@+id/button3"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="8dp"
 - android:layout_marginTop="8dp"
 - android:onClick="OnClick_Add"
 - android:text="Добавить"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.559"
 - android:layout_marginStart="8dp"
 - android:layout_marginEnd="8dp" />
 - <Button
 - android:id="@+id/button2"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="8dp"
 - android:layout_marginTop="8dp"
 - android:onClick="OnClick_Open"
 - android:text="Открыть"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.028"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.559"
 - android:layout_marginStart="8dp"
 - android:layout_marginEnd="8dp" />
 - <TextView
 - android:id="@+id/tVHead"
 - android:layout_width="349dp"
 - android:layout_height="56dp"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="15dp"
 - android:layout_marginTop="8dp"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.833"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.701"
 - android:layout_marginStart="8dp"
 - android:layout_marginEnd="15dp" />
 - <ListView
 - android:id="@+id/lVDB"
 - android:layout_width="351dp"
 - android:layout_height="114dp"
 - android:layout_marginBottom="8dp"
 - android:layout_marginLeft="8dp"
 - android:layout_marginRight="8dp"
 - android:layout_marginTop="249dp"
 - app:layout_constraintBottom_toBottomOf="parent"
 - app:layout_constraintHorizontal_bias="0.521"
 - app:layout_constraintLeft_toLeftOf="parent"
 - app:layout_constraintRight_toRightOf="parent"
 - app:layout_constraintTop_toTopOf="parent"
 - app:layout_constraintVertical_bias="0.934"
 - tools:layout_editor_absoluteY="381dp" />
 - </android.support.constraint.ConstraintLayout>
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment