The next day Android development

IDE: Android Studio 3.5 RC 2

Development language: Java

SDK version: Android 9.0 API 28

builde.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"


    defaultConfig {
        applicationId "com.aaa.aListView"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

List 1. ListView for data display

com.aaa.domain/Dog.java

Package Penalty for com.aaa.domain; 

/ ** 
 * Author: Kevin ON 2019/8/8 20:19 
 * / 
public  class Dog {
     Private Integer dogId;       // dog numbering 
    Private String dogName;      // dog's name 
    Private String dogSkin ;      // dog's skin 
    Private the Float dogPrice;      // dogs price 

    public dog () { 
    } 

    public dog (dogName String, String dogSkin, the Float dogPrice) {
         the this .dogName = dogName;
         the this .dogSkin = dogSkin;
         the this.dogPrice = dogPrice;
    }

    public Dog(Integer dogId, String dogName, String dogSkin, Float dogPrice) {
        this.dogId = dogId;
        this.dogName = dogName;
        this.dogSkin = dogSkin;
        this.dogPrice = dogPrice;
    }

    public Integer getDogId() {
        return dogId;
    }

    public void setDogId(Integer dogId) {
        this.dogId = dogId;
    }

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public String getDogSkin() {
        return dogSkin;
    }

    public void setDogSkin(String dogSkin) {
        this.dogSkin = dogSkin;
    }

    public Float getDogPrice() {
        return dogPrice;
    }

    public void setDogPrice(Float dogPrice) {
        this.dogPrice = dogPrice;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "dogId=" + dogId +
                ", dogName='" + dogName + '\'' +
                ", dogSkin='" + dogSkin + '\'' +
                ", dogPrice=" + dogPrice +
                '}';
    }
}

com.aaa.service/OpenDatabaseService.java

Package Penalty for com.aaa.service; 

Import android.content.Context;
 Import android.database.sqlite.SQLiteDatabase;
 Import android.database.sqlite.SQLiteOpenHelper; 

/ ** 
 * Author: Kevin ON 2019/8/8 17:28 
 * / 
public  class OpenDatabaseService the extends the SQLiteOpenHelper {
     public OpenDatabaseService (the context context) {
         // parameters: context object database file using the default cursor database version 
        Super (context, "kevin.db", null ,. 1 ); 
    } 
    // 2019 August 8 day 17:30:17 this method is called when the database file is first created 
    @Override
     public  voidthe onCreate (the SQLiteDatabase DB) {
         // the SQLite database, in addition to the primary key of the table. Other field values regardless of type, VARCHAR (20) of these things can be omitted 
        String sql = "CREATE TABLE dog ( dogId integer PRIMARY KEY AUTOINCREMENT, dogName varchar (20), dogSkin varchar (20), dogPrice float (20)) ";         // create statement list of 
        db.execSQL (SQL); 
    } 

    @Override    // 2019 Nian 8 Yue 8 Ri 17:38:32 this method is called when the database version number changes of 
    public  void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) { 
    } 
}

com.aaa.service/DogService.java

package com.aaa.service;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.aaa.domain.Dog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:kevin on 2019/8/8 20:53
 */
public class DogService {
    private SQLiteDatabase dbOption;

    public DogService(SQLiteDatabase dbOption) {
        this.dbOption = dbOption;
    }

    // 2019年8月8日20:53:53 插入数据
    public void insert(Dog dog){
        String sql = "Dog the INSERT the INTO (dogName, dogSkin, dogPrice) values (,,???)" ; 
        DbOption.execSQL (SQL, new new Object [] {dog.getDogName (), dog.getDogSkin (), dog.getDogPrice () });
         // dbOption.close ();    // may or may not be shut off 
    }
     // August 8, 2019 21:18:58 delete the data 
    public  void the delete ( int the above mentioned id) { 
        String SQL = "dELETE the FROM Dog dogId = the WHERE "? ; 
        dbOption.execSQL (SQL, new new Object [] {the above mentioned id}); 
    } 
    // 2019 Nian 8 Yue 8 Ri 21:20:39 change the data 
    public  void Update (Dog Dog) { 
        String SQL= "UPDATE dog SET dogName = ?,dogSkin = ?,dogPrice = ? WHERE dogId = ?";
        dbOption.execSQL(sql,new Object[]{dog.getDogName(),dog.getDogSkin(),dog.getDogPrice(),dog.getDogId()});
    }
    // 2019年8月8日21:23:26 查询数据
    public Dog query(int id){
        String sql = "SELECT * FROM dog WHERE dogId = ?";
        Cursor cursor = dbOption.rawQuery(sql, new String[]{String.valueOf(id)});
        if(cursor.moveToFirst()){
            int dogId = cursor.getInt(cursor.getColumnIndex("dogId"));
            String dogName = cursor.getString(cursor.getColumnIndex("dogName"));
            String dogSkin = cursor.getString(cursor.getColumnIndex("dogSkin"));
            float dogPrice = cursor.getFloat(cursor.getColumnIndex("dogPrice"));
            return new Dog(dogId,dogName,dogSkin,dogPrice);
        }
        cursor.close();
        return null;
    }
    // 2019年8月8日21:25:16 获取数据库记录的总条目数
    public long getCount(){
        String sql = "SELECT COUNT(*) FROM dog";
        Cursor cursor = dbOption.rawQuery(sql,null);
        cursor.moveToFirst();
        long result = cursor.getLong(0);
        return result;
    }

    // 2019年8月8日21:39:01 分页查询
    public List<Dog> getPaginationData(int start,int pageSize){
        List<Dog> dogs = new ArrayList<>();
        String sql = "SELECT * FROM dog ORDER BY dogId ASC LIMIT ?,?";
        Cursor cursor = dbOption.rawQuery(sql,new String[]{String.valueOf(start),String.valueOf(pageSize)});
        while (cursor.moveToNext()){
            int dogId = cursor.getInt(cursor.getColumnIndex("dogId"));
            String dogName = cursor.getString(cursor.getColumnIndex("dogName"));
            String dogSkin = cursor.getString(cursor.getColumnIndex("dogSkin"));
            float dogPrice = cursor.getFloat(cursor.getColumnIndex("dogPrice"));
            dogs.add(new Dog(dogId,dogName,dogSkin,dogPrice));
        }
        return dogs;
    }
    // 2019年8月9日08:52:15    获取分页数据的第二种方式
    public Cursor getCursorPagination(int start,int pageSize){
        List<Dog> dogs = new ArrayList<>();
        String sql = "SELECT dogId as _id,dogName,dogSkin,dogPrice FROM dog ORDER BY dogId ASC LIMIT ?,?";
        Cursor cursor = dbOption.rawQuery(sql,new String[]{String.valueOf(start),String.valueOf(pageSize)});
        return cursor;
    }
}

com.aaa.aListView/Main.java

package com.aaa.aListView;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

import com.aaa.domain.Dog;
import com.aaa.service.DogService;
import com.aaa.service.OpenDatabaseService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main extends AppCompatActivity {
    private ListView listView;
    private SQLiteDatabase dbOption;
    private DogService dogService;
    private Dog dog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = this.findViewById(R.id.list_item);
        dbOption = new OpenDatabaseService(Main.this).getWritableDatabase();    // 得到数据库操作对象
        = dogService new new DogService (dboption);                                   // get the object dogService 
        Dog = new new Dog ();
         // The insertData ();     // insert dummy data 40
         //   showData1 ();       // bind SimpleAdapter data adapter
         // showData2 (); 
        showData3 ();         // custom adapter 
    }
     // August 9, 2019 08:39:38 dummy data is inserted, to facilitate later query 
    public  void The insertData () {
         for ( int I = 0; I <40; ++ I ) { 
            dog.setDogId (I); 
            dog.setDogName ("阿拉斯加犬"+i+"号");
            dog.setDogSkin("绿色"+i+"号");
            dog.setDogPrice((float)i+100);
            dogService.insert(dog);
        }
    }
    // 2019年8月9日08:48:48    使用SimpleAdapter绑定数据
    public void showData1(){
        List<Dog> dogs = dogService.getPaginationData(5, 20);
        List<HashMap<String,Object>> data = new ArrayList<HashMap<String, Object>>();
        for(Dog dog:dogs){
            HashMap<String,Object> map = new newThe HashMap <String, Object> (); 
            map.put ( "dogId" , dog.getDogId ()); 
            map.put ( "dogName" , dog.getDogName ()); 
            map.put ( "dogSkin" , dog.getDogSkin ()); 
            map.put ( "dogPrice" , dog.getDogPrice ()); 
            data.add (Map); 
        } 
        // through the adapter, the display data entry to the dogs set the ListView 
        String [] keys = { " dogId "," dogName "," dogSkin "," dogPrice "};        // value of the data set may correspond dogId bind to this control R.id.dogId 
        int [] = views {R.id.dogId, R & lt .id.dogName, R.id.dogSkin, R.id.dogPrice};
        SimpleAdapter simpleAdapter =new new SimpleAdapter (Main. the this , the Data, R.layout.dogs, Keys, views); 
        listView.setAdapter (simpleAdapter); 
    } 
        // 2019 Nian 8 Yue 9 Ri 09:16:54 binding data via cursor object 
    public  void showData2 () { 
        the Cursor Cursor = dogService.getCursorPagination (. 5, 20 is ); 
        String [] Keys = { "the _id", "dogName", "dogSkin", "dogPrice"};        // value of the data set may correspond tied dogId this control is given to R.id.dogId 
        int [] = views {R.id.dogId, R.id.dogName, R.id.dogSkin, R.id.dogPrice}; 
        the SimpleCursorAdapter SimpleCursorAdapter = new new . the SimpleCursorAdapter (the Main the this,R.layout.dogs,cursor,keys,views,0);
        listView.setAdapter(simpleCursorAdapter);
    }
   
}

Custom adapter to achieve binding data

com.aaa.adapter/MyAdapter.java

 

package com.aaa.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aaa.aListView.R;
import com.aaa.domain.Dog;

import java.util.List;

/**
 * 作者:kevin on 2019/8/9 09:30
 */
public class MyAdapter extends BaseAdapter {
    private List<Dog> dogs ;    // 定义需要绑定的数据
    private int Resource;        // data entry interface binding 
    Private LayoutInflater inflater so that; // the layout of the filler can be generated with a View object xml file a 
    public MyAdapter (the Context context, List <Dog> Dogs, int Resource) {
         the this .dogs = Dogs;
         the this .resource = Resource; 
        inflater so that = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override    // for obtaining the total number of records 
    public  int getCount () {
         return dogs.size (); // data The total number 
    }

    @Override    // an index corresponding to an element 
    public Object the getItem ( int position) {
         return dogs.get (position); 
    } 

    @Override 
    public  Long getItemId ( int position) {
         return position; 
    } 

    @override    // position entry request of the tie predetermined data in the set of index values 
    public View the getView ( int position, View View, the ViewGroup parent) {
        IF (View == null ) { 
            View = Inflater.inflate (Resource, null ); 
       } 
       the TextView dogId= view.findViewById(R.id.dogId);
        TextView dogName = view.findViewById(R.id.dogName);
        TextView dogSkin = view.findViewById(R.id.dogSkin);
        TextView dogPrice = view.findViewById(R.id.dogPrice);
        Dog dog = dogs.get(position);
        dogId.setText(dog.getDogId());
        dogName.setText(dog.getDogName());
        dogSkin.setText(dog.getDogSkin());
        dogPrice.setText(dog.getDogPrice().toString());
       return view;
    }
}

 

Main.java Add Method

    // 9 August 2019 09:29:29 by custom adapter binding data 
    public  void showData3 () { 
        List <Dog> Dogs dogService.getPaginationData = (. 5, 20 is ); 
        MyAdapter myAdapter = new new . MyAdapter (the Main the this , Dogs, R.layout.dogs); 
        listView.setAdapter (myAdapter); 
    }

 

Guess you like

Origin www.cnblogs.com/kevinOnes1/p/11324818.html