gpt4 book ai didi

android - 如何在自定义listfragment中显示来自sqlite的数据

转载 作者:行者123 更新时间:2023-11-29 01:30:02 25 4
gpt4 key购买 nike

我想显示sqlite数据库的数据,我会显示给listfragment,但是到现在都显示不出来

类 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);

}
}

数据库数据源.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();
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();
}
}

简单的不显示

最佳答案

这是示例代码。

首先为一个列表行创建一个布局。

例子:list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
>
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />

<TextView
android:id="@+id/merk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="5dp" />

<TextView
android:id="@+id/harga"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/merk"
android:layout_marginTop="5dp"/>

</RelativeLayout>

创建这个 list_row.xml 布局后,创建一个适配器类。

创建 CustomListAdapter.java

公共(public)类 CustomListAdapter 扩展 BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private ArrayList<Barang> barangList;

public CustomListAdapter(Activity activity, ArrayList<Barang> barangList) {
this.activity = activity;
this.barangList = barangList;
}

/*
get count of the barangList
*/

@Override
public int getCount() {
return barangList.size();
}

@Override
public Object getItem(int location) {
return barangList.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

/*
inflate the items in the list view
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row, null);
}


/*
creating objects to access the views
*/
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView merk = (TextView) convertView.findViewById(R.id.merk);
TextView harga = (TextView) convertView.findViewById(R.id.harga);



// getting barang data for the row

Barang barang = barangList.get(position);

name.setText(barang.getNama_barang());

merk.setText(barang.getMerk_barang());

harga.setText(barang.getHarga_barang());

return convertView;
}}

现在在您的 MasterBarang.java 中,将以下代码放入您的 onCreate 方法中。

values = dataSource.getAllBarang();
CustomListAdapter adapter;
adapter = new CustomListAdapter(getActivity(), values);
setListAdapter(adapter);

现在运行应用程序..干杯!

关于android - 如何在自定义listfragment中显示来自sqlite的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com