fms 0 ارسال شده در ۴ شهریور ۱۳۹۷ گزارش اشتراک گذاری ارسال شده در ۴ شهریور ۱۳۹۷ سلام طبق این کدها میخام یه لیست علاقه مندیها درست کنم اما آیتم ها add نمیشن نمیدونم مشکل چیه و ارور خاصی هم نداره ،یه باتن به آیتم های ریسایکلر ویو اضافه کردن که همون add هست. دیتابیس آفلاین و آماده است. DatabaseHelper.java: public class DatabaseHelper extends SQLiteOpenHelper { private static String DB_NAME= "Data.db"; private static String DB_PATH=""; private SQLiteDatabase myDatabase; private Context myContext; public DatabaseHelper(Context context) { super(context, DB_NAME, null, 1); if(Build.VERSION.SDK_INT>=15) { DB_PATH = context.getApplicationInfo().dataDir + "/databases/"; } else{ DB_PATH= Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/"; } this.myContext=context; } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql="database already exist"; db.execSQL(sql); } public void checkAndCopyDatabase(){ boolean dbExist=checkDatabase(); if (dbExist){ Log.d("TAG","database already exist"); } else {this.getReadableDatabase();} try { copyDatabase(); } catch (IOException e) { e.printStackTrace(); Log.d("TAG","Error copy database"); } } public boolean checkDatabase(){ SQLiteDatabase checkDB=null; try { String myPath=DB_PATH+DB_NAME; checkDB=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE); }catch (SQLiteException e){} if(checkDB !=null){ checkDB.close(); } return checkDB !=null ? true : false; } public void copyDatabase()throws IOException{ InputStream myInput=myContext.getAssets().open(DB_NAME); String outFileName=DB_PATH + DB_NAME; OutputStream myOutput=new FileOutputStream(outFileName); byte[] buffer=new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0){ myOutput.write(buffer, 0,length); } myOutput.flush(); myOutput.close(); myInput.close(); } public void openDatabase(){ String myPath=DB_PATH + DB_NAME; myDatabase=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE); } public synchronized void close(){ if (myDatabase !=null){ myDatabase.close(); } super.close(); } public Cursor QueryData(String query){ return myDatabase.rawQuery(query,null); } } Item ها: ublic class Item { int id; String dis; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getdis() { return dis; } public void setdis(String dis) { this.dis = dis; } } کدهایSetViewHolder : public class SetViewHolder extends RecyclerView.ViewHolder { public TextView txt_h; public Button button2; public Button button3; private Cursor cursor; private DatabaseHelper databaseHelper; public SetViewHolder(final View itemView) { super(itemView); txt_h=(TextView)itemView.findViewById(R.id.txt_word); button2=(Button)itemView.findViewById(R.id.button2); button3= (Button) itemView.findViewById(R.id.button3); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { databaseHelper=new DatabaseHelper(itemView.getContext()); databaseHelper.openDatabase(); databaseHelper.getReadableDatabase(); databaseHelper.QueryData("INSERT INTO fav(dis) VALUES('"+txt_h.getText()+"')"); databaseHelper.close(); // cursor=databaseHelper.QueryData("INSERT INTO fav(dis) VALUES('"+txt_h.getText()+"')"); // cursor.getString(1); } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT,txt_h.getText().toString()); shareIntent.putExtra(Intent.EXTRA_SUBJECT, "jj"); v.getContext().startActivity(Intent.createChooser(shareIntent, "Share ...")); } }); } } کدهای Adapter: public class VocabularyAdapter extends RecyclerView.Adapter<SetViewHolder> { private Activity activity; List<Item> items= Collections.emptyList(); public VocabularyAdapter(Activity activity, List<Item> items){ this.activity=activity; this.items=items; } @Override public SetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false); return new SetViewHolder(view); } @Override public void onBindViewHolder(SetViewHolder holder, final int position) { holder.txt_h.setText(items.get(position).getdis()); } @Override public int getItemCount() { return items.size(); } } کدهای اکتویتی اصلی : public class HadisActivity extends AppCompatActivity { private RecyclerView recyclerView; private DatabaseHelper databaseHelper; private Cursor cursor; private VocabularyAdapter adapter; private ArrayList<Item> arrayList =new ArrayList<Item>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.h_layout); recyclerView=(RecyclerView)findViewById(R.id.recycler_view); loadDatabase(); } public void loadDatabase(){ databaseHelper=new DatabaseHelper(this); try { databaseHelper.checkAndCopyDatabase(); databaseHelper.openDatabase(); }catch (SQLiteException e){ e.printStackTrace(); } try { cursor=databaseHelper.QueryData("select * from tbl"); if (cursor !=null) { if(cursor.moveToFirst()){ do { Item item=new Item(); item.setdis(cursor.getString(1)); arrayList.add(item); }while (cursor.moveToNext()); } } }catch (SQLiteException e){ e.printStackTrace(); } LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); adapter=new VocabularyAdapter(this,arrayList); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(linearLayoutManager); recyclerView.setAdapter(adapter); } } مشکل در کد INSERT (در SetViewHolder) میباشد که آیتم به جدول علاقه مندی ها اضافه نمیشود دو کد رو تست کردم اما درست نشد: button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { databaseHelper=new DatabaseHelper(itemView.getContext()); databaseHelper.openDatabase(); databaseHelper.getReadableDatabase(); databaseHelper.QueryData("INSERT INTO fav(dis) VALUES('"+txt_h.getText()+"')"); databaseHelper.close(); // cursor=databaseHelper.QueryData("INSERT INTO fav(dis) VALUES('"+txt_h.getText()+"')"); // cursor.getString(1); } }); نقل قول لینک ارسال به اشتراک گذاری در سایت های دیگر
پست های پیشنهاد شده
به گفتگو بپیوندید
هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .