ProductListActivity.java
Dosyayı İndir
package com.godoro.androidsqlite;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class ProductListActivity extends ListActivity {
private InventoryProductRepository repository;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
repository =new InventoryProductRepository(this);
// İlk çalıştırmada sınama için açılabilir
repository.insert("Cep Telefonu", 345);
repository.insert("Televizyon", 670);
repository.insert("Buzdolabı", 995);
list();
Button insertButton=(Button)findViewById(R.id.insertButton);
insertButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
insert();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
repository.close();
}
private void list(){
Cursor cursor= repository.list();
String[] from={"_id","productName","salesPrice"};
int[] to={R.id.productIdView,R.id.productNameView,R.id.salesPriceView};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.product_row, cursor, from, to);
setListAdapter(adapter);
}
public void insert() {
Intent intent = new Intent(ProductListActivity.this, ProductDetailActivity.class);
startActivityForResult(intent, 999);
}
private void update(long productId){
if(productId<=0){
return ;
}
Intent intent = new Intent(ProductListActivity.this, ProductDetailActivity.class);
intent.putExtra("productId", productId);
startActivityForResult(intent, 998);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String message=String.format("Seçilmiş orun %d kimlik: %d",position,id);
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
update(id);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && (requestCode == 999 || requestCode == 998)) {
if (data.hasExtra("changed")) {
boolean changed=data.getExtras().getBoolean("changed");
if(changed){
list();
}
}
}
}
}
Dosyayı İndir