MainActivity.java 3.33 KB
package com.example.ksi.refrigerator;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    ArrayList<Item> itemList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for(int i=0; i<3; i++)
        {
            Item temp = new Item();
            temp.name = "사과";
            temp.date = new Date();
            itemList.add(temp);
        }

    }

    public void showExpdate(View view){
        Intent intent = new Intent(this,Main4Activity.class);

        ArrayList<Item> ExpdateItem = new ArrayList();
        for(int i=0; i<itemList.size();i++)
        {
            Date today = new Date();
            long dif = today.getTime()-itemList.get(i).date.getTime();
            dif = dif/(24*60*60*1000);

            //유통기한 3일 남은것까지 보여주기
            if(dif < 4){
                ExpdateItem.add(itemList.get(i));
            }
        }

        intent.putExtra("item",ExpdateItem);

        startActivity(intent);
    }

    public void addItem(View view){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(intent.resolveActivity(getPackageManager())!=null) {
            startActivityForResult(intent, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);

        Bitmap img;
        if(requestCode == 1 && resultCode == RESULT_OK)
        {
            Bundle extras = data.getExtras();
            img = (Bitmap) extras.get("data");
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            intent.putExtra("barcode",img);
            startActivityForResult(intent, 2);
        }
        else if(requestCode == 2 && resultCode == RESULT_OK)
        {
            Toast.makeText(this,"성공!",Toast.LENGTH_LONG).show();
            Item item = (Item)data.getSerializableExtra("item2");
            itemList.add(item);

            TableRow row = new TableRow(this);
            ImageView photo = new ImageView(this);
            photo.setBackgroundColor(0xFF00FF00);
            Glide.with(this)
                    .load(item.image)
                    .override(200,200)
                    .into(photo);

            photo.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT));

            row.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.WRAP_CONTENT,
                    TableLayout.LayoutParams.WRAP_CONTENT));

            row.addView(photo);

            TableLayout table = (TableLayout)findViewById(R.id.refri);
            table.addView(row);

        }
    }


}