如何解析 "Cannot resolve method 'Insertdata'" in Java?
How to resolve "Cannot resolve method 'Insertdata'" in Java?
我正在尝试将数据插入 SQLite。但是 AddActivity.java 中有些问题。我的概念是将数据插入 SQLite。从数据显示列表中读取数据后。
当点击数据列表中的列表时,将显示该列表的详细信息。
这里是错误:
Cannot resolve method 'Insertdata(java.lang.String,java.lang.String,java.lang.String)'
myDBClass.java
package com.example.puen.projectdemo;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
public class myDBClass extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mydatabase";
// Table Name
private static final String TABLE_MEMBER = "members";
public myDBClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_MEMBER +
"(MemberID INTEGER PRIMARY KEY AUTOINCREMENT," +
" Name TEXT(100)," +
" Tel TEXT(100));");
Log.d("CREATE TABLE", "Create Table Successfully.");
}
// Select Data
public String[] SelectData(String strMemberID) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_MEMBER, new String[]{"*"},
"MemberID=?",
new String[]{String.valueOf(strMemberID)}, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
/***
* 0 = MemberID
* 1 = Name
* 2 = Tel
*/
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
arrData[2] = cursor.getString(2);
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
// Show All Data
public ArrayList<HashMap<String, String>> SelectAllData() {
// TODO Auto-generated method stub
try {
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<>();
HashMap<String, String> map;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT * FROM " + TABLE_MEMBER;
Cursor cursor = db.rawQuery(strSQL, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
do {
map = new HashMap<>();
map.put("MemberID", cursor.getString(0));
map.put("Name", cursor.getString(1));
map.put("Tel", cursor.getString(2));
MyArrList.add(map);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return MyArrList;
} catch (Exception e) {
return null;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
// Re Create on method onCreate
onCreate(db);
}
}
AddActivity.java
package com.example.puen.projectdemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// btnSave (Save)
final Button save = (Button) findViewById(R.id.btnSave);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// If Save Complete
if(SaveData())
{
// Open Form Main
Intent newActivity = new Intent(AddActivity.this,MainActivity.class);
startActivity(newActivity);
}
}
});
// btnCancel (Cancel)
final Button cancel = (Button) findViewById(R.id.btnCancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open Form Main
Intent newActivity = new Intent(AddActivity.this,MainActivity.class);
startActivity(newActivity);
}
});
}
public boolean SaveData()
{
// txtMemberID, txtName, txtTel
final EditText tMemberID = (EditText) findViewById(R.id.txtMemberID);
final EditText tName = (EditText) findViewById(R.id.txtName);
final EditText tTel = (EditText) findViewById(R.id.txtTel);
// Dialog
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
AlertDialog ad = adb.create();
// Check MemberID
if(tMemberID.getText().length() == 0)
{
ad.setMessage("Please input [MemberID] ");
ad.show();
tMemberID.requestFocus();
return false;
}
// Check Name
if(tName.getText().length() == 0)
{
ad.setMessage("Please input [Name] ");
ad.show();
tName.requestFocus();
return false;
}
// Check Tel
if(tTel.getText().length() == 0)
{
ad.setMessage("Please input [Tel] ");
ad.show();
tTel.requestFocus();
return false;
}
// new Class DB
final myDBClass myDb = new myDBClass(this);
// Check Data (MemberID exists)
String arrData[] = myDb.SelectData(tMemberID.getText().toString());
if(arrData != null)
{
ad.setMessage("MemberID already exists! ");
ad.show();
tMemberID.requestFocus();
return false;
}
// Save Data
long saveStatus = myDb.InsertData(tMemberID.getText().toString(),
tName.getText().toString(),
tTel.getText().toString());
if(saveStatus <= 0)
{
ad.setMessage("Error!! ");
ad.show();
return false;
}
Toast.makeText(AddActivity.this,"Add Data Successfully. ",
Toast.LENGTH_SHORT).show();
return true;
}
}
DetailActivity.java
package com.example.puen.projectdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DetailActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// Read var from Intent
Intent intent= getIntent();
String MemID = intent.getStringExtra("MemID");
// Show Data
ShowData(MemID);
// btnCancel (Cancel)
final Button cancel = (Button) findViewById(R.id.btnCancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open Form Show
Intent newActivity = new Intent(DetailActivity.this,ShowActivity.class);
startActivity(newActivity);
}
});
}
public void ShowData(String MemID)
{
// txtMemberID, txtName, txtTel
final TextView tMemberID = (TextView) findViewById(R.id.txtMemberID);
final TextView tName = (TextView) findViewById(R.id.txtName);
final TextView tTel = (TextView) findViewById(R.id.txtTel);
// new Class DB
final myDBClass myDb = new myDBClass(this);
// Show Data
String arrData[] = myDb.SelectData(MemID);
if(arrData != null)
{
tMemberID.setText(arrData[0]);
tName.setText(arrData[1]);
tTel.setText(arrData[2]);
}
}
}
ShowActivity.java
package com.example.puen.projectdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class ShowActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
final myDBClass myDb = new myDBClass(this);
final ArrayList<HashMap<String, String>> MebmerList = myDb.SelectAllData();
// listView1
ListView lisView1 = (ListView)findViewById(R.id.listView1);
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(ShowActivity.this, MebmerList, R.layout.activity_column,
new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});
lisView1.setAdapter(sAdap);
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
// Show on new activity
Intent newActivity = new Intent(ShowActivity.this,DetailActivity.class);
newActivity.putExtra("MemID", MebmerList.get(position).get("MemberID").toString());
startActivity(newActivity);
}
});
// btnCancel (Cancel)
final Button cancel = (Button) findViewById(R.id.btnCancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open Form Main
Intent newActivity = new Intent(ShowActivity.this,MainActivity.class);
startActivity(newActivity);
}
});
}
}
您的 myDBClass 中没有 Insertdata 方法。您正在调用尚未初始化的内容。
class"myDBClass"中没有"InsertData"这样的方法。您应该将 InsertData 方法添加到 myDBClass,并在该方法中添加插入数据库的逻辑。
我正在尝试将数据插入 SQLite。但是 AddActivity.java 中有些问题。我的概念是将数据插入 SQLite。从数据显示列表中读取数据后。
当点击数据列表中的列表时,将显示该列表的详细信息。
这里是错误:
Cannot resolve method 'Insertdata(java.lang.String,java.lang.String,java.lang.String)'
myDBClass.java
package com.example.puen.projectdemo;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
public class myDBClass extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mydatabase";
// Table Name
private static final String TABLE_MEMBER = "members";
public myDBClass(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// Create Table Name
db.execSQL("CREATE TABLE " + TABLE_MEMBER +
"(MemberID INTEGER PRIMARY KEY AUTOINCREMENT," +
" Name TEXT(100)," +
" Tel TEXT(100));");
Log.d("CREATE TABLE", "Create Table Successfully.");
}
// Select Data
public String[] SelectData(String strMemberID) {
// TODO Auto-generated method stub
try {
String arrData[] = null;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
Cursor cursor = db.query(TABLE_MEMBER, new String[]{"*"},
"MemberID=?",
new String[]{String.valueOf(strMemberID)}, null, null, null, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
arrData = new String[cursor.getColumnCount()];
/***
* 0 = MemberID
* 1 = Name
* 2 = Tel
*/
arrData[0] = cursor.getString(0);
arrData[1] = cursor.getString(1);
arrData[2] = cursor.getString(2);
}
}
cursor.close();
db.close();
return arrData;
} catch (Exception e) {
return null;
}
}
// Show All Data
public ArrayList<HashMap<String, String>> SelectAllData() {
// TODO Auto-generated method stub
try {
ArrayList<HashMap<String, String>> MyArrList = new ArrayList<>();
HashMap<String, String> map;
SQLiteDatabase db;
db = this.getReadableDatabase(); // Read Data
String strSQL = "SELECT * FROM " + TABLE_MEMBER;
Cursor cursor = db.rawQuery(strSQL, null);
if(cursor != null)
{
if (cursor.moveToFirst()) {
do {
map = new HashMap<>();
map.put("MemberID", cursor.getString(0));
map.put("Name", cursor.getString(1));
map.put("Tel", cursor.getString(2));
MyArrList.add(map);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return MyArrList;
} catch (Exception e) {
return null;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
// Re Create on method onCreate
onCreate(db);
}
}
AddActivity.java
package com.example.puen.projectdemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// btnSave (Save)
final Button save = (Button) findViewById(R.id.btnSave);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// If Save Complete
if(SaveData())
{
// Open Form Main
Intent newActivity = new Intent(AddActivity.this,MainActivity.class);
startActivity(newActivity);
}
}
});
// btnCancel (Cancel)
final Button cancel = (Button) findViewById(R.id.btnCancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open Form Main
Intent newActivity = new Intent(AddActivity.this,MainActivity.class);
startActivity(newActivity);
}
});
}
public boolean SaveData()
{
// txtMemberID, txtName, txtTel
final EditText tMemberID = (EditText) findViewById(R.id.txtMemberID);
final EditText tName = (EditText) findViewById(R.id.txtName);
final EditText tTel = (EditText) findViewById(R.id.txtTel);
// Dialog
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
AlertDialog ad = adb.create();
// Check MemberID
if(tMemberID.getText().length() == 0)
{
ad.setMessage("Please input [MemberID] ");
ad.show();
tMemberID.requestFocus();
return false;
}
// Check Name
if(tName.getText().length() == 0)
{
ad.setMessage("Please input [Name] ");
ad.show();
tName.requestFocus();
return false;
}
// Check Tel
if(tTel.getText().length() == 0)
{
ad.setMessage("Please input [Tel] ");
ad.show();
tTel.requestFocus();
return false;
}
// new Class DB
final myDBClass myDb = new myDBClass(this);
// Check Data (MemberID exists)
String arrData[] = myDb.SelectData(tMemberID.getText().toString());
if(arrData != null)
{
ad.setMessage("MemberID already exists! ");
ad.show();
tMemberID.requestFocus();
return false;
}
// Save Data
long saveStatus = myDb.InsertData(tMemberID.getText().toString(),
tName.getText().toString(),
tTel.getText().toString());
if(saveStatus <= 0)
{
ad.setMessage("Error!! ");
ad.show();
return false;
}
Toast.makeText(AddActivity.this,"Add Data Successfully. ",
Toast.LENGTH_SHORT).show();
return true;
}
}
DetailActivity.java
package com.example.puen.projectdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DetailActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// Read var from Intent
Intent intent= getIntent();
String MemID = intent.getStringExtra("MemID");
// Show Data
ShowData(MemID);
// btnCancel (Cancel)
final Button cancel = (Button) findViewById(R.id.btnCancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open Form Show
Intent newActivity = new Intent(DetailActivity.this,ShowActivity.class);
startActivity(newActivity);
}
});
}
public void ShowData(String MemID)
{
// txtMemberID, txtName, txtTel
final TextView tMemberID = (TextView) findViewById(R.id.txtMemberID);
final TextView tName = (TextView) findViewById(R.id.txtName);
final TextView tTel = (TextView) findViewById(R.id.txtTel);
// new Class DB
final myDBClass myDb = new myDBClass(this);
// Show Data
String arrData[] = myDb.SelectData(MemID);
if(arrData != null)
{
tMemberID.setText(arrData[0]);
tName.setText(arrData[1]);
tTel.setText(arrData[2]);
}
}
}
ShowActivity.java
package com.example.puen.projectdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class ShowActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
final myDBClass myDb = new myDBClass(this);
final ArrayList<HashMap<String, String>> MebmerList = myDb.SelectAllData();
// listView1
ListView lisView1 = (ListView)findViewById(R.id.listView1);
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(ShowActivity.this, MebmerList, R.layout.activity_column,
new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel});
lisView1.setAdapter(sAdap);
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
// Show on new activity
Intent newActivity = new Intent(ShowActivity.this,DetailActivity.class);
newActivity.putExtra("MemID", MebmerList.get(position).get("MemberID").toString());
startActivity(newActivity);
}
});
// btnCancel (Cancel)
final Button cancel = (Button) findViewById(R.id.btnCancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Open Form Main
Intent newActivity = new Intent(ShowActivity.this,MainActivity.class);
startActivity(newActivity);
}
});
}
}
您的 myDBClass 中没有 Insertdata 方法。您正在调用尚未初始化的内容。
class"myDBClass"中没有"InsertData"这样的方法。您应该将 InsertData 方法添加到 myDBClass,并在该方法中添加插入数据库的逻辑。