۱۳۹۴/۰۵/۱۲, ۰۱:۳۲ ق.ظ
سلام
میشه بگید مشکل کد زیر چیه که موقع اجرا ارور میده؟DBAdapter:Beyt:
وقتی یه حرف رو تایپ میکنم ارور میده
میشه بگید مشکل کد زیر چیه که موقع اجرا ارور میده؟
کد پیاچپی:
public class MainActivity extends ListActivity {
List<Beyt> beyTha=null;
DBAdapter db=new DBAdapter(this);
String tbl_beyt=db.DB_TABLE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
String destPath="/data/data/"+getPackageName()+"/databases";
File f=new File(destPath);
if(!f.exists()){
f.mkdirs();
f.createNewFile();
CopyDB(getBaseContext().getAssets().open("laki"),
new FileOutputStream(destPath+"/laki"));
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
db.open();
List<Beyt> beytHa=db.getAllBeyt();
setListAdapter(new ArrayAdapter<Beyt>(MainActivity.this,
android.R.layout.simple_list_item_1, beytHa));
db.close();
final EditText et=(EditText)findViewById(R.id.eDitText1);
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
db.findBeyt(et.getText().toString(),tbl_beyt);
tazeSazi();
}
});
}
public void CopyDB(InputStream inputStream,
OutputStream outputStream)throws IOException {
byte[] buffer=new byte[1024];
int length;
while ((length=inputStream.read(buffer))>0) {
outputStream.write(buffer,0,length);
}
inputStream.close();
outputStream.close();
}
public void tazeSazi() {
ArrayAdapter<Beyt> adapter;
adapter=new ArrayAdapter<Beyt>(this,
android.R.layout.simple_list_item_1, beyTha);
setListAdapter(adapter);
}
}
کد پیاچپی:
public class DBAdapter {
static final String KEY_ROWID="_id";
static final String KEY_BEYT="beyt";
static final String TAG="DBAdapter";
static final String DB_NAME="laki";
static final String DB_TABLE="abyat";
static final int DB_VERSION=1;
static final String DB_CREATE=
"create table abyat (_id integer primary key autoincrement,"+
"beyt text not null);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter (Context ctx) {
this.context=ctx;
DBHelper=new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DB_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from "+oldVersion+"to"+newVersion+
",which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS abyat");
onCreate(db);
}
}
public DBAdapter open() throws SQLException {
db=DBHelper.getReadableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertBeyt(String beyt) {
ContentValues initialValue=new ContentValues();
initialValue.put(KEY_BEYT, beyt);
return db.insert(DB_TABLE, null, initialValue);
}
public boolean deleteBeyt(long rowId) {
return db.delete(DB_TABLE, KEY_ROWID+"="+rowId, null)>0;
}
public List<Beyt> getAllBeyt() {
Cursor cursor=db.query(DB_TABLE,new String[]{KEY_ROWID,KEY_BEYT},
null, null, null, null, null);
List<Beyt> beytha=cursorToList(cursor);
return beytha;
}
public Cursor getBeyt(long rowId)throws SQLException {
Cursor mCursor=db.query(true,
DB_TABLE, new String[]{KEY_ROWID,KEY_BEYT}, KEY_ROWID+"="+rowId,
null, null, null, null, null);
if(mCursor!=null){
mCursor.moveToFirst();
}
return mCursor;
}
private List<Beyt> cursorToList(Cursor cursor) {
List<Beyt> beYt=new ArrayList<Beyt>();
if(cursor.getCount()>0){
while (cursor.moveToNext()) {
Beyt beyt=new Beyt();
beyt.setId(cursor.getString(0));
beyt.setBeyt(cursor.getString(1));
beYt.add(beyt);
};
}
return beYt;
}
public List<Beyt> findBeyt(String beyt,String tabale) throws SQLException
{
Cursor cursor =
// % ha yani inke mohem nis ghabl ya badesh harchi mikhad bashe
db.query(true, tabale, new String[] {KEY_ROWID,
KEY_BEYT}, KEY_BEYT + " LIKE '%" + beyt + "%'", null,
null, null, null, null);
List<Beyt> beytha = cursorToList(cursor);
return beytha;
}
public boolean updateBeyt(long rowId,String beyt) {
ContentValues args=new ContentValues();
args.put(KEY_BEYT, beyt);
return db.update(DB_TABLE, args, KEY_ROWID+"="+rowId, null)>0;
}
}
کد پیاچپی:
public class Beyt implements Parcelable{
private String id;
private String beyt;
public Beyt() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id=id;
}
public String getBeyt() {
return beyt;
}
public void setBeyt(String beyt) {
this.beyt=beyt;
}
@Override
public String toString(){
return beyt+"";
}
public Beyt(Parcel in) {
id=in.readString();
beyt=in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(id);
dest.writeString(beyt);
}
public static final Parcelable.Creator<Beyt> creator() {
return new Parcelable.Creator<Beyt>() {
@Override
public Beyt createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return new Beyt(source);
}
@Override
public Beyt[] newArray(int size) {
// TODO Auto-generated method stub
return new Beyt[size];
}
};
}
}
وقتی یه حرف رو تایپ میکنم ارور میده