在我的情况下,我有DbHandler.java和ActivityKategori.java。
我的问题是在DbHandler.java,我有查询条件
SELECT * FROM TBL_**** WHERE COLUMN1 = 'VARIABEL_FROM_ACT_KATEGORI' "
,变量值来自bundle:D
我已经做了很多改变但没有解决。
这是完整的代码
ActivityKategori.java
此活动是从bundle获取数据并将其值发送到DBHandler以获取sql CONDITION 。
我知道有人会建议使用 SharedPreference ,但我对此感到困惑。
现在,我想学习使用 Bundle或Intents
进行传递package ptacs.ekatalog.com.e_katalogproduk.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import ptacs.ekatalog.com.e_katalogproduk.R;
import ptacs.ekatalog.com.e_katalogproduk.adapter.KategoriAdapter;
import ptacs.ekatalog.com.e_katalogproduk.helper.Constant;
import ptacs.ekatalog.com.e_katalogproduk.helper.DBHandler;
import ptacs.ekatalog.com.e_katalogproduk.helper.RecyclerItemClickListener;
import ptacs.ekatalog.com.e_katalogproduk.model.Produk;
public class ActivityKategori extends AppCompatActivity {
private SwipeRefreshLayout swLayout2;
private LinearLayout llayout2;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private KategoriAdapter adapter;
private DBHandler dbHandler;
private List<Produk> kategoriList = new ArrayList<>();
private TextView tv1;
String mMerkProduk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kategori);
initRecyclerView();
cekDataRecyclerView();
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getExtras();
mMerkProduk = bundle.getString(Constant.BUNDLE_MERK_PRODUK); //MERK kategori
//Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle(mMerkProduk);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
dbHandler = new DBHandler(this);
}
//dbHandler.getKategoryProduk(mMerkProduk);
}
private void initRecyclerView(){
recyclerView = (RecyclerView) findViewById(R.id.rv_kategori);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
dbHandler = new DBHandler(ActivityKategori.this);
kategoriList = dbHandler.getKategoryProduk(mMerkProduk); //GET VALUE STRING
//kategoriList = dbHandler.getKategoryProduk(); //OLD CODE to GET OBJECT
adapter = new KategoriAdapter(kategoriList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void cekDataRecyclerView() {
if (adapter.getItemCount() == 0) {
recyclerView.setVisibility(View.GONE);
} else {
recyclerView.setVisibility(View.VISIBLE);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// TODO Handle item click
Bundle bundle = new Bundle();
//COMMIT MAS INDRA CS
bundle.putString(Constant.BUNDLE_JENIS_PRODUK, adapter.getItem(position).getJenis_produk());
Intent intent = new Intent(ActivityKategori.this, ActivityList.class);
intent.putExtras(bundle);
startActivity(intent);
}
})
);
}
swLayout2 = (SwipeRefreshLayout) findViewById(R.id.sw_layout2);
llayout2 = (LinearLayout) findViewById(R.id.ll_Layout);
//Mengeset warna yang berputar
swLayout2.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary);
//Setting Listener yang akan dijalankan saat layar difresh
swLayout2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshItem();
}
void refreshItem(){
initRecyclerView();
cekDataRecyclerView();
onItemLoad();
}
void onItemLoad(){
swLayout2.setRefreshing(false);
}
});
}
}
ActivityKategori.java
package ptacs.ekatalog.com.e_katalogproduk.helper;
/**
* Created by Maxoto on 1/15/2018.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import ptacs.ekatalog.com.e_katalogproduk.model.Produk;
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "db_ekatalog"; // NAMA DATABASE
private static final String TABLE_PRODUK = "tb_produk"; // NAMA TABEL
private static final String COLUMN_ID = "id_produk"; // NAMA KOLOM ID
private static final String COLUMN_KD = "kd_produk"; // KODE PRODUK
private static final String COLUMN_NAMA = "nama_produk"; // NAMA KOLOM NAMA
private static final String COLUMN_MERK = "merk_produk"; //MERK PRODUK
private static final String COLUMN_JENIS = "jenis_produk"; //JENIS PRODUK
private static final String COLUMN_VARIASI = "variasi_produk"; //VARIASI PRODUK
private static final String COLUMN_FOTO = "foto_produk"; // FOTO PRODUK
public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// TODO : LANJUT SECTION 2
// FUNGSI UNTUK MEMBUAT DATABASENYA
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USER_TABLE = "CREATE TABLE "
+ TABLE_PRODUK +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_KD + " TEXT,"
+ COLUMN_NAMA + " TEXT, "
+ COLUMN_MERK + " TEXT, "
+ COLUMN_JENIS + " TEXT, "
+ COLUMN_VARIASI + " TEXT, "
+ COLUMN_FOTO + " TEXT" + ")";
db.execSQL(CREATE_USER_TABLE);
}
// FUNGSI UNTUK MENGECEK DATABASE ADA ATAU TIDAK.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUK);
onCreate(db);
}
// FUNGSI UNTUK TAMBAH DATA PRODUK
public void tambahProduk(Produk produk){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_KD, produk.getKd_produk());
values.put(COLUMN_NAMA, produk.getNama_produk());
values.put(COLUMN_MERK, produk.getMerk_produk());
values.put(COLUMN_JENIS, produk.getJenis_produk());
values.put(COLUMN_VARIASI, produk.getVariasi_produk());
values.put(COLUMN_FOTO, produk.getFoto_produk());
db.insert(TABLE_PRODUK, null, values);
db.close();
}
// FUNGSI UNTUK AMBIL 1 DATA PRODUK
public Produk getProduk(int id_produk){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_PRODUK, new String[]{COLUMN_ID, COLUMN_KD ,COLUMN_NAMA, COLUMN_MERK
,COLUMN_JENIS, COLUMN_VARIASI , COLUMN_FOTO },
COLUMN_ID + "=?", new String[]{String.valueOf(id_produk)}, null, null,null, null);
if (cursor != null)
cursor.moveToFirst();
Produk produk = new Produk(cursor.getString(1), cursor.getString(2),cursor.getString(3),
cursor.getString(4),cursor.getString(5),cursor.getString(6));
return produk;
}
// FUNGSI UNTUK AMBIL SEMUA DATA PRODUK
public List<Produk> getSemuaProduk(){
List<Produk> produkList = new ArrayList<>();
String selectQuery = " SELECT * FROM " + TABLE_PRODUK ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()){
do {
Produk produk = new Produk(cursor.getString(1), cursor.getString(2),cursor.getString(3),
cursor.getString(4),cursor.getString(5),cursor.getString(6));
produkList.add(produk);
} while (cursor.moveToNext());
}
return produkList;
}
// FUNGSI MENGHITUNG ADA BEBERAPA DATA
public int getProdukCount(){
String countQuery = "SELECT * FROM " + TABLE_PRODUK;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
return cursor.getCount();
}
// FUNGSI UPDATE DATA PRODUK
public int updateDataProduk(Produk produk) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_KD, produk.getKd_produk());
values.put(COLUMN_NAMA, produk.getNama_produk());
values.put(COLUMN_MERK, produk.getMerk_produk());
values.put(COLUMN_JENIS, produk.getJenis_produk());
values.put(COLUMN_VARIASI, produk.getVariasi_produk());
values.put(COLUMN_FOTO, produk.getFoto_produk());
return db.update(TABLE_PRODUK, values, COLUMN_ID + " = ?",
new String[]{String.valueOf(produk.getId())});
}
// FUNGSI HAPUS DATA 1 PRODUK
public void hapusDataProduk(Produk produk) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_PRODUK, COLUMN_ID + " = ?",
new String[]{String.valueOf(produk.getId())});
db.close();
}
// FUNGSI UNTUK MENGHAPUS SEMUA DATA PRODUK
public void hapusSemuaDataProduk(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUK);
}
//FUNGSI MENGAMBIL DATA WHERE DI ACTIVITY KATEGORY
public List<Produk> getKategoryProduk(String mMerkProduk) {
List<Produk> kategoriList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_PRODUK + " WHERE " + COLUMN_MERK + " =? " ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery,new String[]{ mMerkProduk } );
if (cursor.moveToFirst()) {
do {
Produk kategori = new Produk(cursor.getString(1), cursor.getString(2), cursor.getString(3),
cursor.getString(4), cursor.getString(5), cursor.getString(6));
kategoriList.add(kategori);
} while (cursor.moveToNext());
}
return kategoriList;
}
}
这是错误:
01-29 14:10:25.342 370-370/ptacs.ekatalog.com.e_katalogproduk E/AndroidRuntime: FATAL EXCEPTION: main
Process: ptacs.ekatalog.com.e_katalogproduk, PID: 370
java.lang.RuntimeException: Unable to start activity ComponentInfo{ptacs.ekatalog.com.e_katalogproduk/ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori}: java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5235)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at ptacs.ekatalog.com.e_katalogproduk.helper.DBHandler.getKategoryProduk(DBHandler.java:152)
at ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori.initRecyclerView(ActivityKategori.java:68)
at ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori.onCreate(ActivityKategori.java:43)
at android.app.Activity.performCreate(Activity.java:6001)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5235)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
01-29 14:10:25.364 370-370/? I/Process: Sending signal. PID: 370 SIG: 9
答案 0 :(得分:3)
在您初始化initRecyclerView();
之前,您正在呼叫mMerkProduk
。因此,请在initRecyclerView();
阻止后调用if (getIntent().getExtras() != null) {....}
。就像这样
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getExtras();
mMerkProduk = bundle.getString(Constant.BUNDLE_MERK_PRODUK); //MERK kategori
//Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle(mMerkProduk);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
dbHandler = new DBHandler(this);
}
initRecyclerView();
cekDataRecyclerView();
答案 1 :(得分:0)
变量值来自bundle
不确定为什么重要...从Bundle中提取值并将其值作为数据库方法的参数使用,就像任何其他值一样
您似乎已经知道如何使用bundle.getString()
,例如
如果你想要的只是看到数据,你的OnCreate应该是这样的。
最重要的是,您需要在之前分配值查询数据库并填充列表。
如果您查看错误的这些行,那么请查看您的代码,目标尚未读取,并且您的字符串为空
at ptacs.ekatalog.com.e_katalogproduk.helper.DBHandler.getKategoryProduk(DBHandler.java:152)
at ptacs.ekatalog.com.e_katalogproduk.activity.ActivityKategori.initRecyclerView(ActivityKategori.java:68)
尝试将查询列表中的代码与查询数据库并填充列表的代码分开
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kategori);
dbHandler = new DBHandler(ActivityKategori.this);
//Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initRecyclerView();
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getExtras();
mMerkProduk = bundle.getString(Constant.BUNDLE_MERK_PRODUK);
setTitle(mMerkProduk);
fillRecyclerView(mMerkProduk);
}
// cekDataRecyclerView();
}
private void initRecyclerView(){
recyclerView = (RecyclerView) findViewById(R.id.rv_kategori);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
}
private void fillRecyclerView(String product) {
if (product!=null) {
kategoriList = dbHandler.getKategoryProduk(product);
adapter = new KategoriAdapter(kategoriList);
recyclerView.setAdapter(adapter);
}
}
答案 2 :(得分:0)
正如Logcat所说
引起:java.lang.IllegalArgumentException:索引1处的绑定值为null
我认为您的mMerkProduk
为空。查询前检查mMerkProduk
为空。
检查this回答。我认为它与你的情况相同。