رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
هنگ کردن برنامه هنگام ذخیره در دیتابیس
#1
با سلام و خسته نباشید خدمت اساتید محترم.بنده طبق اموزش دیتا بیس داخلی مهندس شهرکی برای ذخیره یک مقدار string وقتی رو باتن سیو دیالوگ Add کلیک میکنم هیچ اتفاقی نمیفته برنامه هنگ یا گیر میکنه.و بعد از مدتی میگه ایا برنامه رو ببندیم؟کد قسمت های مهم رو میزارم اینجا :

DB
public class DB extends SQLiteOpenHelper {
    private static final String DB_NAME = "moraldb.sqlite";
    private static final ArrayList<String> TABLE_SCHEMA = new ArrayList<>();
    private static int DB_VERSION = 1;

    public DB() {
        super(App.CONTEXT, DB_NAME, null, DB_VERSION);
        TABLE_SCHEMA.add("CREATE  TABLE  IF NOT EXISTS "moral" (" +
                ""id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , " +
                ""mosbat" TEXT, " +
                ""manfi" TEXT, " +
                ""shekast" TEXT, " +
                ""piruzi" TEXT" +
                ")");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        create(db);
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        drop(db);
        create(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        drop(db);
        create(db);
    }

    private void create(SQLiteDatabase db) {
        db.beginTransaction();
        for (String createTable : TABLE_SCHEMA) {
            db.execSQL(createTable);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    private void drop(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS "moral"");
    }
}


App
public class App extends Application {
    public static final Handler HANDLER = new Handler();
    public static AppCompatActivity ACTIVITY;
    public static Context CONTEXT;
    public static LayoutInflater INFLATER;
    public static SQLiteDatabase DB;
//    public static Typeface FONT_BOLD;
//    public static Typeface FONT_NORMAL;

    public static void startActivity(Class targetActivity) {
        startActivity(targetActivity, false);
    }

    public static void startActivity(Class targetActivity, boolean finish) {
        Intent intent = new Intent(ACTIVITY, targetActivity);
        ACTIVITY.startActivity(intent);
        if (finish) {
            ACTIVITY.finish();
        }
    }

    public static void toast(String message) {
        toast(message, Toast.LENGTH_SHORT);
    }

    public static void toast(String message, int duration) {
        View view = INFLATER.inflate(R.layout.toast, null);
        TextView txtMessage = (TextView) view.findViewById(R.id.txtMessage);
        txtMessage.setText(message);
        Toast toast = new Toast(CONTEXT);
        toast.setDuration(duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(view);
        toast.show();
    }


    @Override
    public void onCreate() {
        CONTEXT = getApplicationContext();
        DB = new DB().getWritableDatabase();

        INFLATER = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
//        AssetManager assetManager = getAssets();
//        FONT_BOLD = Typeface.createFromAsset(assetManager, "font/IRANSans_Bold.ttf");
//        FONT_NORMAL = Typeface.createFromAsset(assetManager, "font/IRANSans_Normal.ttf");
    }
}

Person
public class Persons {
    public static String tableName = "moral";

    public static ArrayList<StructPerson> all() {
        return all();
    }

//    public static ArrayList<StructPerson> all(Boolean onlyFriends) {
//        ArrayList<StructPerson> result = new ArrayList<>();
//        Cursor cursor;
////        if (!onlyFriends) {
//            cursor = App.DB.rawQuery("SELECT * FROM "" + tableName + """, null);
////        } else {
////            cursor = App.DB.rawQuery("SELECT * FROM "" + tableName + "" WHERE ("friend"=1)", null);
////        }
//        while (cursor.moveToNext()) {
//            result.add(extract(cursor));
//        }
//        cursor.close();
//        return result;
//    }

    public static void clear() {
        App.DB.delete(tableName, "1", null);
    }

    public static boolean delete(int id) {
        return App.DB.delete(tableName, ""id"=?", new String[]{"" + id}) > 0;
    }

    public static boolean exists(int id) {
        return one(id) != null;
    }

    public static boolean insert(StructPerson person) {
        ContentValues values = new ContentValues();
        values.put("mosbat", person.mosbat);
        values.put("manfi", person.manfi);
        values.put("shekast", person.shekast);
        values.put("piruzi", person.piruzi);
//        values.put("male", person.male ? 1 : 0);
//        values.put("friend", person.friend ? 1 : 0);
//        values.put("score", person.score);
        return App.DB.insert(tableName, null, values) != -1;
    }

    public static StructPerson one(int id) {
        StructPerson result = null;
        Cursor cursor = App.DB.rawQuery("SELECT * FROM "" + tableName + "" WHERE ("id"=?)", new String[]{"" + id});
        if (cursor.moveToNext()) {
            result = extract(cursor);
        }
        cursor.close();
        return result;
    }

    public static boolean save(StructPerson person) {
        if (person.id == 0 || !exists(person.id)) {
            return insert(person);
        }
        return update(person);
    }

    public static boolean update(StructPerson person) {
        ContentValues values = new ContentValues();
        values.put("mosbat", person.mosbat);
        values.put("manfi", person.manfi);
        values.put("shekast", person.shekast);
        values.put("piruzi", person.piruzi);
//        values.put("male", person.male ? 1 : 0);
//        values.put("friend", person.friend ? 1 : 0);
//        values.put("score", person.score);
        return App.DB.update(tableName, values, ""id"=?", new String[]{"" + person.id}) > 0;
    }

    private static StructPerson extract(Cursor cursor) {
        StructPerson result = new StructPerson();
        result.id = cursor.getInt(cursor.getColumnIndex("id"));
        result.mosbat = cursor.getString(cursor.getColumnIndex("mosbat"));
        result.manfi = cursor.getString(cursor.getColumnIndex("manfi"));
        result.shekast = cursor.getString(cursor.getColumnIndex("shekast"));
        result.piruzi = cursor.getString(cursor.getColumnIndex("piruzi"));
//        result.male = cursor.getInt(cursor.getColumnIndex("male")) == 1;
//        result.friend = cursor.getInt(cursor.getColumnIndex("friend")) == 1;
//        result.score = cursor.getFloat(cursor.getColumnIndex("score"));
        return result;
    }
}

MainActivity
public class MainActivity extends ActivityEnhanced {
    private Toolbar mToolbar;
//    Button mosbatbtn;
//    EditText moralEdittext;
    public AdapterPerson adapter;
    private RecyclerView rvPersons;
    private SwipeRefreshLayout swpReload;
//    private boolean exit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Pushe.initialize(this,true);
        setContentView(R.layout.activity_main);


//        rvPersons = (RecyclerView) findViewById(R.id.rvPersons);
//        rvPersons.setHasFixedSize(true);
//        rvPersons.setLayoutManager(new LinearLayoutManager(App.ACTIVITY));
//        adapter = new AdapterPerson(Persons.all());
////        rvPersons.setAdapter(adapter);
//        adapter.notifyDataSetChanged();


        findViewById(R.id.mosbatbtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DialogAdd(App.ACTIVITY, null)
                        .setListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialogInterface) {
//                                swpReload.setRefreshing(true);
                                adapter.persons = Persons.all();
//                                adapter.notifyDataSetChanged();
//                                swpReload.setRefreshing(false);
                            }
                        })
                        .show();
            }
        });

Dialog Add
public class DialogAdd extends Dialog {
    private StructPerson person;
    private OnDismissListener listener;

//    private AppCompatCheckBox chkFriend;
    private Button btnSave;
    private EditText moralEdittext;
//    private CustomEditText edtMobile;
//    private CustomEditText edtPhone;
//    private CustomEditText edtAddress;
//    private CustomTextViewBold txtFemale;
//    private CustomTextViewBold txtMale;
//    private ImageView imgFemale;
//    private ImageView imgMale;
//    private RatingBar rtbScore;
//    private ViewGroup lytFemale;
//    private ViewGroup lytMale;

    private int colorAccent;
    private int colorLightest;

    public DialogAdd(Context context, StructPerson person) {
        super(context);
        this.person = person;
        init();
    }

    public DialogAdd(Context context, int themeResId, StructPerson person) {
        super(context, themeResId);
        this.person = person;
        init();
    }

    private void init() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_add);

        getItems();

//        chkFriend.setChecked(person.friend);
//        chkFriend.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//                person.friend = isChecked;
//            }
//        });
//        edtName.setText(person.name);
//        edtMobile.setText(person.mobile);
//        edtPhone.setText(person.phone);
//        edtAddress.setText(person.address);
//        if (person.male) {
//            txtFemale.setTextColor(colorLightest);
//            imgFemale.setColorFilter(colorLightest);
//            txtMale.setTextColor(colorAccent);
//            imgMale.setColorFilter(colorAccent);
//        } else {
//            txtFemale.setTextColor(colorAccent);
//            imgFemale.setColorFilter(colorAccent);
//            txtMale.setTextColor(colorLightest);
//            imgMale.setColorFilter(colorLightest);
//        }
//        rtbScore.setRating(person.score);
//        lytFemale.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                person.male = false;
//                txtFemale.setTextColor(colorAccent);
//                imgFemale.setColorFilter(colorAccent);
//                txtMale.setTextColor(colorLightest);
//                imgMale.setColorFilter(colorLightest);
//            }
//        });
//        lytMale.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                person.male = true;
//                txtFemale.setTextColor(colorLightest);
//                imgFemale.setColorFilter(colorLightest);
//                txtMale.setTextColor(colorAccent);
//                imgMale.setColorFilter(colorAccent);
//            }
//        });
//        rtbScore.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
//            @Override
//            public void onRatingChanged(RatingBar ratingBar, float value, boolean fromUser) {
//                if (fromUser) {
//                    person.score = value;
//                }
//            }
//        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                person.mosbat = moralEdittext.getText().toString();
//                person.mobile = edtMobile.getText().toString();
//                person.phone = edtPhone.getText().toString();
//                person.address = edtAddress.getText().toString();

                if (Persons.save(person)) {
                    App.toast(App.CONTEXT.getString(R.string.save_success));
                    dismiss();
                } else {
                    App.toast(App.CONTEXT.getString(R.string.save_error));
                }
            }
        });
    }

    private void getItems() {
//        colorAccent = ContextCompat.getColor(App.CONTEXT, R.color.colorAccent);
//        colorLightest = ContextCompat.getColor(App.CONTEXT, R.color.colorLightest);

        if (person == null) {
            person = new StructPerson();
            person.manfi = "omid";
        }

//        chkFriend = (AppCompatCheckBox) findViewById(R.id.chkFriend);
        btnSave = (Button) findViewById(R.id.addbtn);
        moralEdittext = (EditText) findViewById(R.id.moralEdittext);
//        edtMobile = (CustomEditText) findViewById(R.id.edtMobile);
//        edtPhone = (CustomEditText) findViewById(R.id.edtPhone);
//        edtAddress = (CustomEditText) findViewById(R.id.edtAddress);
//        imgFemale = (ImageView) findViewById(R.id.imgFemale);
//        imgMale = (ImageView) findViewById(R.id.imgMale);
//        txtFemale = (CustomTextViewBold) findViewById(R.id.txtFemale);
//        txtMale = (CustomTextViewBold) findViewById(R.id.txtMale);
//        rtbScore = (RatingBar) findViewById(R.id.rtbScore);
//        lytFemale = (ViewGroup) findViewById(R.id.lytFemale);
//        lytMale = (ViewGroup) findViewById(R.id.lytMale);
    }

    public DialogAdd setListener(OnDismissListener listener) {
        this.listener = listener;
        return this;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (listener != null) {
            listener.onDismiss(this);
        }
    }
}
پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: 1 مهمان