ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [android] android 데이터 관리 +CustomerManagement 최종구현
    app/android 2022. 1. 17. 19:19
    반응형

     

     

     

     

     

     

     

    안드로이드 최종구현

     

     

     

    https://hwantech.tistory.com/104

     

    [android] android 커스텀 리스트뷰 (custom listView)

    <실습시간> 앞에 배운 리스트뷰를 이용하여 xml로 커스텀 리스트뷰를 만들어보장~~~ Student 클래스 이름 소속학과 학생번호 사진 클래스 구현방법 class StudentC (var name : String, var dept : String, var st..

    hwantech.tistory.com

     

     

    이용해서 데이터 관리까지하는 리스트뷰~~~~

     

     

     

     

    CustomerManagement 로 구현해보았당

     

     

     

     

    ---------------------------------<중ㅇ!!!>---------------------------------------

     

     

     

    메인화면에서 두 xml 화면으로 나눠서 들어가기 때문에

    manifests에 이 소스를 적용시켜야한당

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="aca.hankook.CustomerManagement">
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".JasehiActivity"></activity>
        <activity android:name=".CustomerlistActivity" />
        <activity android:name=".RegisterActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
    </manifest>

     

     

    -----------------------------------------------------------------------------------------------------

     

     

    구현시작!!!

     

    activity_main.xml

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
    
        <ImageView
            android:padding="40dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/main" />
    
        <Button
            android:padding="20dp"
            android:id="@+id/button1"
            android:text="고객등록"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:padding="20dp"
            android:id="@+id/button2"
            android:text="고객현황"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>

     

    customerlist.xml

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".CustomerlistActivity">
    
        <TextView
            android:background="#8D8D8D"
            android:text="고객등록리스트"
            android:textColor="#ffffff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
    </LinearLayout>

     

    jasehi.xml

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".JasehiActivity">
    
        <TextView
            android:id="@+id/jName"
            android:text="이름"
            android:textSize="20sp"
            android:textColor="#3F51B5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    
        <TextView
            android:id="@+id/jId"
            android:text="고객번호"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
        <TextView
            android:text="이메일"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
        <TextView
            android:text="고객등급"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
    </LinearLayout>

     

     

    register.xml

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:background="#8D8D8D"
            android:text="고객등록"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <TextView
                android:text="성명"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />
    
            <EditText
                android:id="@+id/edtName"
                android:hint="이름을 입력하시오"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:text="성별"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />
    
            <RadioGroup
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
                <RadioButton
                    android:id="@+id/rb1"
                    android:text="남"
                    android:checked="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
    
                <RadioButton
                    android:id="@+id/rb2"
                    android:text="여"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
    
    
            </RadioGroup>
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:text="휴대폰"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />
    
            <EditText
                android:id="@+id/edPhone"
                android:hint="휴대폰 번호를 입력하세요"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
    
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:text="이메일"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />
    
            <EditText
                android:id="@+id/edEmail"
                android:hint="이메일을 입력하시오"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:text="지역"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />
    
            <EditText
                android:id="@+id/edCity"
                android:hint="시/도를 입력하세요"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
        </LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:text="고객등급"
                android:layout_width="60dp"
                android:layout_height="wrap_content" />
    
            <Spinner
                android:id="@+id/edgrade"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
        </LinearLayout>
    
    
    
        <Button
            android:id="@+id/rButton1"
            android:text="등록"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
    
    </LinearLayout>

     

     

    rowitem.xml

     

    <?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">
    
        <ImageView
            android:id="@+id/itemImage"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/s1"/>
    
        <LinearLayout
            android:layout_weight="8"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <TextView
                android:id="@+id/itemNo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="고객번호" />
    
            <TextView
                android:id="@+id/itemName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="이름/성별"
                android:textSize="20sp"
                android:textColor="#ff0000" />
    
            <TextView
                android:id="@+id/itemPhone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:textColor="#007BFF"
                android:text="연락처"/>
    
            <TextView
                android:id="@+id/itemCity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="지역" />
    
    
    
    
    
        </LinearLayout>
    
    </LinearLayout>

     

     

    CustomerC.kt

     

    package aca.hankook.CustomerManagement
    
    class CustomerC (var custId : Int, var name : String, var sex : String, var phone : String,
                        var city: String) {
    
    }

     

    CustomerlistActivity.kt

     

    package aca.hankook.CustomerManagement
    
    import android.content.Intent
    import android.os.Bundle
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import kotlinx.android.synthetic.main.customerlist.*
    
    class CustomerlistActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.customerlist)
    
            var custList : ArrayList<CustomerC> = arrayListOf()
            var myHelper = MyDBHelper(this)
    
            var sqlDB = myHelper.readableDatabase
            var cursor = sqlDB.rawQuery("select * from customerTBL", null)
            while(cursor.moveToNext()){
                custList.add(CustomerC(cursor.getInt(0), cursor.getString(1),
                    cursor.getString(2), cursor.getString(3), cursor.getString(4)))
            }
            cursor.close()
            sqlDB.close()
    
            var adapter1 = ListViewAdapter(custList)
            listView1.adapter = adapter1
            listView1.setOnItemClickListener { adapterView, view, i, l ->
                Toast.makeText(applicationContext, "선택" + custList.get(i).name, Toast.LENGTH_LONG)
                    .show()
    
                var intent1 = Intent(applicationContext, JasehiActivity::class.java)
                intent1.putExtra("name", custList.get(i).name)
                intent1.putExtra("id", custList.get(i).custId)
                startActivity(intent1)
            }
        }
    }

     

    JasehiActivity.kt

     

    package aca.hankook.CustomerManagement
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import kotlinx.android.synthetic.main.jasehi.*
    
    class JasehiActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.jasehi)
    
            var intent1 = intent
    
            var name = intent1.getStringExtra("name")
            var id = intent1.getStringExtra("Id")
    
            jName.text = name
            jId.text = id
        }
    }
    

     

    ListViewAdapter.kt

     

    package aca.hankook.CustomerManagement
    
    import android.view.View
    import android.view.ViewGroup
    import android.widget.BaseAdapter
    import kotlinx.android.synthetic.main.rowitem.view.*
    
    class ListViewAdapter(var custList: ArrayList<CustomerC>) : BaseAdapter() {
        override fun getItem(p0: Int): Any {
            return custList[p0]
        }
    
        override fun getItemId(p0: Int): Long {
            return p0.toLong()
        }
    
        override fun getCount(): Int {
            return custList.size
        }
    
        override fun getView(position: Int, view: View?, parent: ViewGroup?): View {
            var rowItemView = view
            if (rowItemView == null) {
                rowItemView = View.inflate(parent?.context, R.layout.rowitem, null)
            }
            val cust1 = custList[position]
            var sajinResid = parent!!.resources.getIdentifier("aca.hankook.CustomerManagement:" + "drawable/" + "s" + cust1.custId.toString(),
                null, null)
            rowItemView!!.itemImage.setImageResource(sajinResid)
            rowItemView.itemNo.text = "고객번호 : " + cust1.custId
            rowItemView.itemName.text = "이름 : " + cust1.name + "/" + cust1.sex
            rowItemView.itemPhone.text = "연락처 : " + cust1.phone
            rowItemView.itemCity.text = "지역 : " + cust1.city
    
            return rowItemView
        }
    }

     

    MainActivity.kt

     

    package aca.hankook.CustomerManagement
    
    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import kotlinx.android.synthetic.main.activity_main.*
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            button1.setOnClickListener {
                var intent1 = Intent(applicationContext, RegisterActivity::class.java)
                startActivity(intent1)
            }
            button2.setOnClickListener {
                var intent1 = Intent(applicationContext, CustomerlistActivity::class.java)
                startActivity(intent1)
            }
        }
    }
    

     

    MyDBHelper.kt

     

    package aca.hankook.CustomerManagement
    
    import android.content.Context
    import android.database.sqlite.SQLiteDatabase
    import android.database.sqlite.SQLiteOpenHelper
    
    class MyDBHelper(var context: Context) : SQLiteOpenHelper(context, "customerDB", null, 1) {
        override fun onCreate(p0: SQLiteDatabase?) {
            var tableCreSql = "create table customerTBL( custId integer primary key autoincrement, name varchar(20), sex varchar(2), phone varchar(20), city varchar(20) );"
            p0!!.execSQL(tableCreSql)
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('홍길동', '남', '010-1111-1111', '서울')")
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('한지민', '여', '010-2222-2222', '경기')")
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('남주혁', '남', '010-3333-3333', '인천')")
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('조정석', '남', '010-4444-4444', '부산')")
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('유연석', '남', '010-5555-5555', '대구')")
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('정경호', '남', '010-6666-6666', '대전')")
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('김대명', '남', '010-7777-7777', '용인')")
            p0.execSQL("insert into customerTBL(name,sex,phone,city) values('전미도', '여', '010-8888-8888', '강원')")
    
        }
    
        override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
            p0!!.execSQL("drop table if exists customerTBL")
            onCreate(p0)
        }
    
    }

     

    RegisterActivity.kt

     

    package aca.hankook.CustomerManagement
    
    import android.os.Bundle
    import android.view.View
    import android.widget.AdapterView
    import android.widget.ArrayAdapter
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import kotlinx.android.synthetic.main.register.*
    
    class RegisterActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.register)
    
            rButton1.setOnClickListener {
                var name = edtName.text.toString()
                var sex = ""
                if (rb1.isChecked) {
                    sex = "남"
                } else {
                    sex = "여"
                }
    
    
             var grade = arrayOf("vip","gold","silver","new")
               var sAdapter1 = ArrayAdapter<String>(applicationContext, android.R.layout.simple_spinner_item, grade)
                edgrade.adapter = sAdapter1
    
    
    
                var phone = edPhone.text.toString()
                var city = edCity.text.toString()
                var myHelper = MyDBHelper(this)
                var sqlDB = myHelper.writableDatabase
                var insertSql =
                    "insert into customerTBL(name, sex, phone, city) values ('$name', '$sex', '$phone', '$city')"
                sqlDB.execSQL(insertSql)
    
                Toast.makeText(applicationContext, "inser ok" + name, Toast.LENGTH_SHORT).show()
                sqlDB.close()
            }
        }
    }

     

     

     

    실행결과>>>>>

     

     

    1)main 화면

     

     

    2) 고객등록

     

     

     

     

    3) 고객현황

     

     

     

     

    5) DB추가 화면

     

     

     

     

     

    6) jh 정보 화면 구현

     

     

     

     

     

     

    이상 안드로이드 였슴다~~~~

     

     

     

     

    반응형

    댓글

Designed by Tistory.