ProductDetailActivity.java


Dosyayı İndir
package com.godoro.androidsqlite;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class ProductDetailActivity extends Activity {

    private InventoryProductRepository repository;
    private long id;
    private TextView idValue;
    private EditText nameValue;
    private EditText priceValue;

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

        repository=new InventoryProductRepository(this);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            id = extras.containsKey("productId")?extras.getLong("productId"):0;
        }

        idValue=(TextView)findViewById(R.id.idValue);
        nameValue=(EditText)findViewById(R.id.nameValue);
        priceValue=(EditText)findViewById(R.id.priceValue);

        showData();

        Button saveButton=(Button)findViewById(R.id.saveButton);
        saveButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                save();
            }
        });

        Button deleteButton=(Button)findViewById(R.id.deleteButton);
        deleteButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                delete();
            }
        });
        deleteButton.setEnabled(id!=0);

        Button closeButton=(Button)findViewById(R.id.closeButton);
        closeButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view) {
                close();
            }
        });
    }

    private void showData(){
        if(id!=0){
            idValue.setText(Long.toString(id));
            Cursor cursor=repository.find(id);
            String name=cursor.getString(cursor.getColumnIndexOrThrow("productName"));
            nameValue.setText(name);
            double price=cursor.getDouble(cursor.getColumnIndexOrThrow("salesPrice"));
            priceValue.setText(Double.toString(price));
        }
    }
    private void save(){
        String name=nameValue.getText().toString();
        double price=Double.parseDouble(priceValue.getText().toString());

        if(id==0){
            id=repository.insert(name, price);
        }else{
            repository.update(id,name, price);
        }
        String message=String.format("Kaydoldu kimlik: %d", id);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.putExtra("changed", true);
        setResult(RESULT_OK, intent);
        finish();
    }
    private void delete(){
        if(id<=0){
            return ;
        }
        repository.delete(id);
        String message=String.format("Silindi kimlik: %d",id);
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.putExtra("changed", true);
        setResult(RESULT_OK, intent);
        finish();
    }
    private void close(){

        finish();
    }

}

				
Dosyayı İndir

Bu Sayfayı Paylaş:




Bu Sayfayı Paylaş:

İletişim Bilgileri

Takip Et

Her Hakkı Saklıdır. Bu sitede yayınlanan tüm bilgi ve fikirlerin kullanımından fibiler.com sorumlu değildir. Bu sitede üretilmiş , derlenmiş içerikleri, fibiler.com'u kaynak göstermek koşuluyla kendi sitenizde kullanılabilirsiniz. Ancak telif hakkı olan içeriklerin hakları sahiplerine aittir