activity_product.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ProductActivity"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@drawable/ic_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10sp"
android:layout_marginRight="10sp"
android:backgroundTint="#304BB8"/>
</RelativeLayout>
item_product.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10sp"
android:background="#43134D"
android:layout_margin="20sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="상품명"
android:textSize="20sp"
android:id="@+id/txtname"
android:textColor="#FFC107"
/>
<TextView
android:layout_below="@id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="상품가격"
android:id="@+id/txtprice"
android:textSize="15sp"
android:textColor="#FF5722"/>
<ImageView
android:id="@+id/btndel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_delete"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
ProductVO.java
package com.example.ex12;
public class ProductVO {
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
ProductDB.java
package com.example.ex12;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class ProductDB extends SQLiteOpenHelper {
public ProductDB(@Nullable Context context) {
super(context, "product.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table product(id integer primary key autoincrement,name text,price integer)");
db.execSQL("insert into product(name,price) values('갤럭시 s7',1000000)");
db.execSQL("insert into product(name,price) values('LG View2',1500000)");
db.execSQL("insert into product(name,price) values('롤리팝',10000)");
db.execSQL("insert into product(name,price) values('쿠키폰',50000)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
ProAdapter.java
package com.example.ex12;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class ProAdapter extends RecyclerView.Adapter<ProAdapter.ViewHolder> {
ArrayList<ProductVO> array;
Context context;
public ProAdapter(ArrayList<ProductVO> array, Context context) {
this.array = array;
this.context = context;
}
@NonNull
@Override
public ProAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_product,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ProAdapter.ViewHolder holder, final int position) {
final int id=array.get(position).getId();
holder.txtname.setText(array.get(position).getName());
holder.txtprice.setText(array.get(position).getPrice()+"원");
holder.btndel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder box=new AlertDialog.Builder(context);
box.setMessage(id+"을(를)삭제하시겠습니까?");
box.setNegativeButton("닫기",null);
box.setPositiveButton("예", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ProductDB helper=new ProductDB(context);
SQLiteDatabase db=helper.getWritableDatabase();
String sql="delete from product where id=" +id;
db.execSQL(sql);
array.remove(position);
notifyDataSetChanged();//어댑터 Refresh
}
});
box.show();
}
});
}
@Override
public int getItemCount() {
return array.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView txtname,txtprice;
ImageView btndel;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtname=itemView.findViewById(R.id.txtname);
txtprice=itemView.findViewById(R.id.txtprice);
btndel=itemView.findViewById(R.id.btndel);
}
}
}
ProductActivity.java
package com.example.ex12;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.util.ArrayList;
public class ProductActivity extends AppCompatActivity {
ProductDB helper;
SQLiteDatabase db;
ArrayList<ProductVO> array;
Cursor cursor;
RecyclerView list;
ProAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
getSupportActionBar().setTitle("상품목록");
helper=new ProductDB(this);
db=helper.getReadableDatabase();
array=new ArrayList<ProductVO>();
cursor=db.rawQuery("select * from product",null);
while(cursor.moveToNext()){
ProductVO vo=new ProductVO();
vo.setId(cursor.getInt(0));
vo.setName(cursor.getString(1));
vo.setPrice(cursor.getInt(2));
array.add(vo);
}
list=findViewById(R.id.list);
LinearLayoutManager manager=new LinearLayoutManager(this);
list.setLayoutManager(manager);
adapter=new ProAdapter(array,this);
list.setAdapter(adapter);
ImageView btnadd=findViewById(R.id.btnadd);
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder box=new AlertDialog.Builder(ProductActivity.this);
box.setTitle("상품정보입력");
final LinearLayout view=(LinearLayout)getLayoutInflater().inflate(R.layout.input_product,null);
box.setView(view);
box.setNegativeButton("취소",null);
box.setPositiveButton("저장", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText edtname=view.findViewById(R.id.edtname);
String strname=edtname.getText().toString();
EditText edtprice=view.findViewById(R.id.edtprice);
String strprice=edtprice.getText().toString();
String sql="insert into product(name,price) values(";
sql +="'"+strname+"',";
sql +="'"+strprice+"')";
db.execSQL(sql);
Toast.makeText(ProductActivity.this,"저장되었습니다.",
Toast.LENGTH_SHORT).show();
ProductVO vo=new ProductVO();
vo.setName(strname);
vo.setPrice(Integer.parseInt(strprice));
array.add(vo);
adapter.notifyDataSetChanged();
//getlist();
}
});
box.show();
}
});
}
/*
public void getlist() {
array=new ArrayList<ProductVO>();
cursor = db.rawQuery("select * from product", null);
while (cursor.moveToNext()) {
ProductVO vo = new ProductVO();
vo.setId(cursor.getInt(0));
vo.setName(cursor.getString(1));
vo.setPrice(cursor.getInt(2));
array.add(vo);
}
adapter=new ProAdapter(array,this);
list.setAdapter(adapter);
//activity class안에다 적용
}
*/
}
input_product.xml
<?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="wrap_content"
android:padding="10sp"
android:orientation="vertical"
android:layout_margin="50sp">
<EditText
android:id="@+id/edtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="상품이름"
android:textSize="20sp"/>
<EditText
android:id="@+id/edtprice"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="상품가격"
android:textSize="15sp"/>
</LinearLayout>
'Android' 카테고리의 다른 글
네이버 API 뉴스 검색 (0) | 2019.11.14 |
---|---|
주소록 만들기 (0) | 2019.11.14 |
recyclerview 사용하기 (0) | 2019.11.13 |
Firebase를 이용한 이메일 로그인 (0) | 2019.11.12 |
검색할 수 있는 메모장 만들기 (1) | 2019.11.12 |