<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/data_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Daftar Barang"
android:textSize="20dp" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
public class TampilDataActivity extends AppCompatActivity {
ListView listView;
private DBSource dataSource;
private ArrayList<Barang> values;
private Button editButton;
private Button delButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tampil_data);
listView = findViewById(R.id.listview);
dataSource = new DBSource(this);
dataSource.open();
// ambil semua data barang
values = dataSource.getAllBarang();
// masukkan data barang ke array adapter
ArrayAdapter<Barang> adapter = new ArrayAdapter<Barang>(this,
android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Barang barang = (Barang) parent.getAdapter().getItem(position);
switchToGetData(barang.getId());
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long l) {
final Dialog dialog = new Dialog(TampilDataActivity.this);
dialog.setContentView(R.layout.dialog_view);
dialog.setTitle("Pilih Aksi");
dialog.show();
final Barang b = (Barang) parent.getAdapter().getItem(position);
editButton = (Button) dialog.findViewById(R.id.button_edit_data);
delButton = (Button) dialog.findViewById(R.id.button_delete_data);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switchToEdit(b.getId());
dialog.dismiss();
}
});
delButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// Delete barang
dataSource.deleteBarang(b.getId());
dialog.dismiss();
finish();
startActivity(getIntent());
}
}
);
return true;
}
});
}
public void switchToEdit(long id) {
Barang b = dataSource.getBarang(id);
Intent i = new Intent(this, UbahDataActivity.class);
Bundle bun = new Bundle();
bun.putLong("id", b.getId());
bun.putString("nama", b.getNama_barang());
bun.putString("merk", b.getMerk_barang());
bun.putString("harga", b.getHarga_barang());
i.putExtras(bun);
finale();
startActivity(i);
}
public void finale() {
TampilDataActivity.this.finish();
dataSource.close();
}
@Override
protected void onResume() {
dataSource.open();
super.onResume();
}
@Override
protected void onPause() {
dataSource.close();
super.onPause();
}
public void switchToGetData(long id) {
Barang b = dataSource.getBarang(id);
Intent i = new Intent(this, DetailActivity.class);
Bundle bun = new Bundle();
bun.putLong("id", b.getId());
bun.putString("nama", b.getNama_barang());
bun.putString("merk", b.getMerk_barang());
bun.putString("harga", b.getHarga_barang());
i.putExtras(bun);
dataSource.close();
startActivity(i);
}
}