1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Android Studio / SQL Lite Array List Issues

Discussão em 'Outras Linguagens' iniciado por Stack, Maio 17, 2021.

  1. Stack

    Stack Membro Participativo

    Having trouble with SQLLite and android studio. Basically, for the register button, even if the user has the same credentials, they're being registered another time, and for the login button it doesn't seem like any user is being saved because the login just doesn't work. This is my first time using this website, so I may be slightly confused, if It's an SQL/ class problem I can also paste those!

    public class MainActivity extends AppCompatActivity {

    EditText ed1,ed2;
    Button bRegister,bLogin,b3;
    public static final String prefkey ="Signup";
    public static final String prefname = "Default";

    SQLShop sqlsp;
    ArrayList<Shopper> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sqlsp = new SQLShop(getApplicationContext());
    list = sqlsp.getAllShoppers();


    ed1 = findViewById(R.id.editTextTextPersonName);
    ed2 = findViewById(R.id.editTextTextPassword);
    bLogin = findViewById(R.id.logb);
    bRegister = findViewById(R.id.regb);
    b3 = findViewById(R.id.button3);
    bLogin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    SQLShop sp2 = new SQLShop(getApplicationContext());
    list = sp2.getAllShoppers();
    String username = ed1.getText().toString();
    String password = ed2.getText().toString();
    if (list.isEmpty()) {
    Toast.makeText(getApplicationContext(), "No User Has Been Registered Yet! ", Toast.LENGTH_SHORT).show();
    } else {
    boolean RegUser = false;
    for (int i = 0; i < list.size(); i++) {
    Shopper shopper = (Shopper) list.get(i);
    if (shopper.getShopperUN().equals(username) && shopper.getShopperPS().equals(password)) {
    RegUser = true;
    SharedPreferences pref = getSharedPreferences(prefkey, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString(prefname, username);
    editor.apply();
    Intent intent = new Intent(getApplicationContext(), Shop.class);
    startActivity(intent);
    break;
    }
    }
    if (!RegUser) {
    Toast.makeText(getApplicationContext(), "Wrong Password or ID", Toast.LENGTH_SHORT).show();
    }
    }


    }
    });

    bRegister.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    String username = ed1.getText().toString();
    String password = ed2.getText().toString();
    Shopper shopper = new Shopper();
    shopper.setShopperUN(username);
    shopper.setShopperPS(password);
    shopper.setShopperID(System.currentTimeMillis());
    if(!ShopperExist(shopper))
    {
    SQLShop sp = new SQLShop(getApplicationContext());
    sp.insertShopper(shopper);
    Toast.makeText(getApplicationContext(), "Shopper Created!", Toast.LENGTH_SHORT).show();

    } else {
    Toast.makeText(getApplicationContext(), "Shopper Already Exists!", Toast.LENGTH_SHORT).show();
    }

    }
    });
    }

    private boolean ShopperExist(Shopper shopper)
    {
    SQLShop sp = new SQLShop(getApplicationContext());
    ArrayList splist = sp.getAllShoppers();

    boolean found = false;

    for (int i=0; i< splist.size(); i++)
    {
    Shopper u = (Shopper) splist.get(i);
    if(shopper.getShopperUN().equalsIgnoreCase(u.getShopperUN()))
    {

    found = true;
    break;
    }
    }
    return found;
    }

    }

    Continue reading...

Compartilhe esta Página