此处包含存储在数据库中的详细信息(名字,姓氏,手机,密码,电子邮件ID)。
我设置了一个列表,以便将我的数据库详细信息提供给TextView
。我完成了DataBase
,ActionBar
,AlertDialogue
。点击ActionBar
时会打开AlertDialog
。点击“确定”按钮,详细信息必须安排在TextView
。
我的代码如下:
public class DatabaseHelper extends SQLiteOpenHelper {
public static String dataBaseName = "Login.db";
private static final int dataBaseVersion = 1;
private static final String tableName = "Accounts";
private static String Key_Id = "id";
private static String Key_FirstName = "firstname";
private static String Key_LastName = "lastname";
private static String Key_Password = "password";
private static String Key_Mobile = "mobile";
private static String Key_Email = "email";
public static String tag = "tag";
private static final String createTableAccounts = "CREATE TABLE " + tableName + "( " + Key_Id + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Key_FirstName + " TEXT, " + Key_LastName + " TEXT, " + Key_Password + " TEXT, " + Key_Mobile + " TEXT, " + Key_Email + " TEXT );";
public DatabaseHelper(Context context) {
super(context, dataBaseName, null, dataBaseVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createTableAccounts);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + createTableAccounts);
onCreate(db);
}
public long addAccountDetials(AccountsModel accounts) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Key_FirstName, accounts.firstName);
values.put(Key_LastName, accounts.lastName);
values.put(Key_Password, accounts.passWord);
values.put(Key_Mobile, accounts.mobile);
values.put(Key_Email, accounts.emailId);
long insert = db.insert(tableName, null, values);
return insert;
}
public int updateEntry(AccountsModel accounts) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Key_FirstName, accounts.firstName);
values.put(Key_LastName, accounts.lastName);
values.put(Key_Password, accounts.passWord);
values.put(Key_Mobile, accounts.mobile);
values.put(Key_Email, accounts.emailId);
return db.update(tableName, values, Key_Id + "=?", new String[]{String.valueOf(accounts.id)});
}
public void deleteEntry(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(tableName, Key_Id + " = ?", new String[]{String.valueOf(id)});
}
public AccountsModel getAccounts(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + tableName + " WHERE "
+ Key_Id + " = " + id;
Log.d(tag, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
AccountsModel accountsModel = new AccountsModel();
accountsModel.id = c.getString(c.getColumnIndex(Key_Id));
accountsModel.firstName = c.getString(c.getColumnIndex(Key_FirstName));
accountsModel.lastName = c.getString(c.getColumnIndex(Key_LastName));
accountsModel.mobile = c.getString(c.getColumnIndex(Key_Mobile));
accountsModel.passWord = c.getString(c.getColumnIndex(Key_Password));
accountsModel.emailId = c.getString(c.getColumnIndex(Key_Email));
return accountsModel;
}
public List<AccountsModel> getAllAccountList() {
List<AccountsModel> accountsModelArrayList = new ArrayList<AccountsModel>();
String selectQuery = "SELECT * FROM " + tableName;
Log.d(tag, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
AccountsModel accountsModel = new AccountsModel();
accountsModel.id = c.getString(c.getColumnIndex(Key_Id));
accountsModel.firstName = c.getString(c.getColumnIndex(Key_FirstName));
accountsModel.lastName = c.getString(c.getColumnIndex(Key_LastName));
accountsModel.mobile = c.getString(c.getColumnIndex(Key_Mobile));
accountsModel.passWord = c.getString(c.getColumnIndex(Key_Password));
accountsModel.emailId = c.getString(c.getColumnIndex(Key_Email));
accountsModelArrayList.add(accountsModel);
} while (c.moveToNext());
}
return accountsModelArrayList;
}}
MainActivity
public class MainActivity extends AppCompatActivity {
EditText firstName;
EditText lastName;
EditText mobile;
EditText password;
EditText email;
ListView listView;
List<AccountsModel>list=new ArrayList<AccountsModel>();
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper =new DatabaseHelper(getApplicationContext());
firstName=(EditText)findViewById(R.id.edt_firstname);
lastName=(EditText)findViewById(R.id.edt_lastname);
mobile=(EditText)findViewById(R.id.edt_mobile);
password=(EditText)findViewById(R.id.edt_password);
email=(EditText)findViewById(R.id.edt_email);
listView=(ListView)findViewById(R.id.lst_view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("ADD DATA TO LIST");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,View v) {
if(v == findViewById(R.id.action_add)){
listView.setAdapter(" ");
AccountsModel accountsModel = new AccountsModel();
accountsModel.firstName = firstName.getText().toString();
accountsModel.lastName = lastName.getText().toString();
accountsModel.mobile = mobile.getText().toString();
accountsModel.passWord = password.getText().toString();
accountsModel.emailId = email.getText().toString();
databaseHelper.addAccountDetials(accountsModel);
list = accountsModel.getAllAccountList();
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, value);
String value = "";
for (AccountsModel accountsModel : list) {
value = value + "id: " + accountsModel.id + ", firstName : " + accountsModel.firstName + " lastName : " + accountsModel.lastName + "mobile : " + accountsModel.mobile + " passWord : " + accountsModel.passWord + " email : " + accountsModel.emailId + "\n";
}
listView.setAdapter(value);
}
});
builder.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}}
AccountsModel
public class AccountsModel {
public int id;
public String firstname;
public String lastname;
public String password;
public String mobile;
public String email;
public AccountsModel(int id,String firstname,String lastname,String password,String mobile,String email)
{
this.id=id;
this.firstname=firstname;
this.lastname=lastname;
this.password=password;
this.mobile=mobile;
this.email=email;
}
public AccountsModel()
{
}}
请根据我的应用程序修改代码,以便我能理解,因为我是android的新手。
我正面临的错误
错误:(75,45)错误:找不到符号方法getAllAccountList() 错误:任务':app:compileDebugJavaWithJavac'的执行失败。 编译失败;有关详细信息,请参阅编译器错误输出。
答案 0 :(得分:1)
尝试删除@Override
over&#39; onClick&#39;方法
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
...
}
});
答案 1 :(得分:0)
accountsModel
是类AccountsModel
的对象。类AccountsModel
没有名为getAllAccountList()
的方法。这是给定错误的原因。 getAllAccountList()
方法存在于类DatabaseHelper
中。因此,它应该被称为databaseHelper.getAllAccountList()
而不是accountsModel.getAllAccountList()
。这是一个非常基本的错误,我想您应该尝试查看AndroidStudio提及的行以更好地理解错误。