jeudi 30 juillet 2015

how to display data from sqlite in custom listfragment

I want to display data from sqlite database and I will show to listfragment, but until now have not been able to be displayed

Class Barang.java

public class Barang {

private long id;
private String nama_barang;
private String merk_barang;
private String harga_barang;

public Barang()
{

}

/**
 * @return the id
 */
public long getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(long id) {
    this.id = id;
}

/**
 * @return the nama_barang
 */
public String getNama_barang() {
    return nama_barang;
}

/**
 * @param nama_barang the nama_barang to set
 */
public void setNama_barang(String nama_barang) {
    this.nama_barang = nama_barang;
}

/**
 * @return the merk_barang
 */
public String getMerk_barang() {
    return merk_barang;
}

/**
 * @param merk_barang the merk_barang to set
 */
public void setMerk_barang(String merk_barang) {
    this.merk_barang = merk_barang;
}

/**
 * @return the harga_barang
 */
public String getHarga_barang() {
    return harga_barang;
}

/**
 * @param harga_barang the harga_barang to set
 */
public void setHarga_barang(String harga_barang) {
    this.harga_barang = harga_barang;
}

@Override
public String toString()
{
    return id +" "+ nama_barang +" "+ merk_barang + " "+ harga_barang;
}
}

DBHelper.java

public class DBHelper extends SQLiteOpenHelper{

public static final String TABLE_NAME = "data_inventori";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "nama_barang";
public static final String COLUMN_MERK = "merk_barang";
public static final String COLUMN_HARGA = "harga_barang";
private static final String db_name ="inventori.db";
private static final int db_version=1;

  private static final String db_create = "create table "
      + TABLE_NAME + "(" 
      + COLUMN_ID +" integer primary key autoincrement, " 
      + COLUMN_NAME+ " varchar(50) not null, "
      + COLUMN_MERK+ " varchar(50) not null, "
      + COLUMN_HARGA+ " varchar(50) not null);";

public DBHelper(Context context) {
    super(context, db_name, null, db_version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(db_create);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    Log.w(DBHelper.class.getName(),"Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);

}
}

DBDataSource.java

public class DBDataSource {

private SQLiteDatabase database;

private DBHelper dbHelper;

private String[] allColumns = { DBHelper.COLUMN_ID,
          DBHelper.COLUMN_NAME, DBHelper.COLUMN_MERK,DBHelper.COLUMN_HARGA};

public DBDataSource(Context context)
{
    dbHelper = new DBHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public Barang createBarang(String nama, String merk, String harga) {

    ContentValues values = new ContentValues();
    values.put(DBHelper.COLUMN_NAME, nama);
    values.put(DBHelper.COLUMN_MERK, merk);
    values.put(DBHelper.COLUMN_HARGA, harga);

    long insertId = database.insert(DBHelper.TABLE_NAME, null,
        values);

    Cursor cursor = database.query(DBHelper.TABLE_NAME,
        allColumns, DBHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);

    cursor.moveToFirst();

    Barang newBarang = cursorToBarang(cursor);

    cursor.close();

    return newBarang;
  }

private Barang cursorToBarang(Cursor cursor)
{

    Barang barang = new Barang();

"+cursor.getString(1)+","+cursor.getString(2));

    barang.setId(cursor.getLong(0));
    barang.setNama_barang(cursor.getString(1));
    barang.setMerk_barang(cursor.getString(2));
    barang.setHarga_barang(cursor.getString(3));

    return barang;
}

public ArrayList<Barang> getAllBarang() {
    ArrayList<Barang> daftarBarang = new ArrayList<Barang>();


    Cursor cursor = database.query(DBHelper.TABLE_NAME,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
      Barang barang = cursorToBarang(cursor);
      daftarBarang.add(barang);
      cursor.moveToNext();
    }

    cursor.close();
    return daftarBarang;
  }

public Barang getBarang(long id)
  {
      Barang barang = new Barang(); 

      Cursor cursor = database.query(DBHelper.TABLE_NAME, allColumns, "_id ="+id, null, null, null, null);

      cursor.moveToFirst();

      barang = cursorToBarang(cursor);

      cursor.close();

      return barang;
  }


public void updateBarang(Barang b)
{

    String strFilter = "_id=" + b.getId();

    ContentValues args = new ContentValues();

    args.put(DBHelper.COLUMN_NAME, b.getNama_barang());
    args.put(DBHelper.COLUMN_MERK, b.getMerk_barang());
    args.put(DBHelper.COLUMN_HARGA, b.getHarga_barang() );

    database.update(DBHelper.TABLE_NAME, args, strFilter, null);
}

public void deleteBarang(long id)
{
    String strFilter = "_id=" + id;
    database.delete(DBHelper.TABLE_NAME, strFilter, null);
}
}

MasterBarang.java

public class MasterBarang extends ListFragment implements OnItemLongClickListener {

private DBDataSource dataSource;
private ImageButton bTambah;
private ArrayList<Barang> values;
private Button editButton;
private Button delButton;
private AlertDialog.Builder alertDialogBuilder;
public MasterBarang(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_masterbarang, container, false);
    bTambah = (ImageButton) rootView.findViewById(R.id.button_tambah);

    bTambah.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        Intent intent = new Intent(getActivity(), CreateData.class);
        startActivity(intent);
        getActivity().finish();
      }

    }); 

    ListView lv = (ListView) rootView.findViewById(android.R.id.list);
    lv.setOnItemLongClickListener(this);

    return rootView;
}

public void OnCreate(Bundle savedInstanceStat){


    dataSource = new DBDataSource(getActivity());

    dataSource.open();

    values = dataSource.getAllBarang();

    ArrayAdapter<Barang> adapter = new ArrayAdapter<Barang>(getActivity(),
    android.R.layout.simple_list_item_1, values);

          setListAdapter(adapter);


}

@Override
public boolean onItemLongClick(final AdapterView<?> adapter, View v, int pos,
        final long id) {

    final Barang b = (Barang) getListAdapter().getItem(pos);
     alertDialogBuilder.setTitle("Peringatan");
     alertDialogBuilder
             .setMessage("Pilih Aksi")
             .setCancelable(false)
             .setPositiveButton("Ubah",
                     new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                 int id) {
                             switchToEdit(b.getId());
                             dialog.dismiss();
                         }
                     })
             .setNegativeButton("Hapus",
                     new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                 int id) {
                           dataSource.deleteBarang(b.getId());
                           dialog.dismiss();
                           getActivity().finish();
                           startActivity(getActivity().getIntent());
                         }
                     }).create().show();


 return false;

}

public void switchToEdit(long id)
{
    Barang b = dataSource.getBarang(id);
    Intent i = new Intent(getActivity(), EditData.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()
{
    MasterBarang.this.getActivity().finish();
    dataSource.close();
}
@Override
public void onResume() {
    dataSource.open();
    super.onResume();
}

@Override
public void onPause() {
    dataSource.close();
    super.onPause();
}
}

Aucun commentaire:

Enregistrer un commentaire