我是初学者,我正在尝试开发简单的Android应用程序,但我需要一些帮助。
我有一些按钮,标签等主要活动。按下一个按钮后,我启动第二个活动<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.9/fabric.min.js"></script>
<p draggable="true" id="text">Drag me into the canvas!</p>
<canvas id="canvas" width="200" height="200" ondrop="onDrop(event)" ondragover="onDragOver(event)"></canvas>
,这是我需要帮助的地方。
SearchActivity.java
SearchActivity
DatabaseHelper.java
public class SearchActivity extends AppCompatActivity {
ListView lv;
SearchView sv;
ArrayAdapter<String> adapter;
private ArrayList<String> items = getResults();
private ArrayList<String> getResults(){
DatabaseHelper db = new DatabaseHelper(this);
db.openDatabase();
ArrayList<String> resultList = new ArrayList<String>();
Cursor c = db.query(tablename,column names,null,null,null,null,null);
c.close();
db.close();
return resultList;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
lv = (ListView)findViewById(R.id.listview);
sv = (SearchView)findViewById(R.id.search);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items);
lv.setAdapter(adapter);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
}
activity_search.xml只包含一个public class DatabaseHelper extends SQLiteOpenHelper
{
String DB_PATH = null;
private static String DB_NAME = "dat";
private SQLiteDatabase myDatabase;
private final Context myContext;
public DatabaseHelper(Context context){
super(context, DB_NAME, null, 10);
this.myContext = context;
this.DB_PATH = "/data/data" + context.getPackageName() + "/" + "databases/";
Log.e("Path 1", DB_PATH);
}
public void createDatabase() throws IOException
{
boolean dbExist = checkDatabase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDatabse();
} catch (IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkDatabase()
{
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabse() throws IOException
{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[10];
int length;
while((length = myInput.read(buffer)) > 0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase() throws SQLiteException
{
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close(){
if(myDatabase != null)
myDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion > oldVersion){
try{
copyDatabse();
} catch (IOException e){
e.printStackTrace();
}
}
}
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
{
return myDatabase.query(table,columns,null,null,null,null,null);
}
}
和一个SearchView
。
我在这里尝试的是从数据库加载表数据并能够使用ListView
从中搜索并在SearchView
中显示结果,只有字符串数组可用,但是这样做,整个TextView
导致应用程序崩溃。
以下是我的错误:
SearchActivity
感谢您的回复。