I try to implement data output from the database by clicking on the button in the ListView, but nothing happens. Errors do not give any. I tried to do the example http://startandroid.ru/ru/uroki/vse-uroki-spiskom/278-urok-136-cursorloader.html .
This is what happened to the MainActivity class.
public class MainActivity extends AppCompatActivity { private EditText in; ListView list; SimpleCursorAdapter scAdapter; TestDBHeler db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); db = new TestDBHeler(this); try { db.createDataBase(); db.openDataBase(); } catch (IOException ex) { } in = (EditText) findViewById(R.id.in); list = (ListView) findViewById(R.id.list); } @Override protected void onDestroy() { super.onDestroy(); db.close(); } public void search() { String string = in.getText().toString().trim(); String query = "SELECT " + Entry.COLUMN_COL2 + " FROM " + Entry.TABLE_NAME + " WHERE " + Entry.COLUMN_COL1 + " = " + "\"" + string + "\""; Cursor cursor = db.database.rawQuery(query, null); if (!string.equals("")) { try { if (cursor.getCount() != 0) { while (cursor.moveToNext()) { int col2ColumnIndex = cursor.getColumnIndex(Entry.COLUMN_COL2); String col2 = cursor.getString(col2ColumnIndex); String[] from = new String[] {col2}; int[] to = new int[] {R.id.itemTxt}; scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0); list.setAdapter(scAdapter); } } } catch (SQLException ex) { } finally { cursor.close(); } } } public void onClickBtnSearch(View view) { search(); } } item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/itemTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="10dp" android:text="" android:textSize="18sp"> </TextView> </LinearLayout> activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_light" android:padding="5dp"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/in" android:layout_weight="1" android:height="46dp" android:hint="Введите слово" android:background="@android:color/background_light" android:theme="@style/Widget.AppCompat.EditText" android:textSize="18sp" android:padding="10dp" android:layout_marginRight="10dp" /> <ImageButton android:layout_width="wrap_content" android:layout_height="match_parent" app:srcCompat="@android:drawable/stat_notify_sync" android:id="@+id/imageButton" android:onClick="onClickBtnSearch" android:background="@android:color/holo_green_dark" android:layout_weight="0.28" /> </LinearLayout> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:id="@+id/list" /> </LinearLayout> </ScrollView> </FrameLayout> Class TestDBHeler where ready DB is used
public class TestDBHeler extends SQLiteOpenHelper { private static String DB_PATH = "/data/data/test.testdb/databases/"; private static final String DATABASE_NAME = "databaza.db"; private static final int DATABASE_VERSION = 1; public SQLiteDatabase database; private Context myContext; public TestDBHeler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.myContext = context; } public void createDataBase() throws IOException{ boolean dbExist = checkDataBase(); if(dbExist){ }else{ this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } } private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try { String myPath = DB_PATH + DATABASE_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } catch(SQLiteException e){ } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } private void copyDataBase() throws IOException{ InputStream myInput = myContext.getAssets().open(DATABASE_NAME); String outFileName = DB_PATH + DATABASE_NAME; OutputStream myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){ myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } public void openDataBase() throws SQLException { String myPath = DB_PATH + DATABASE_NAME; database = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } @Override public synchronized void close() { if(database != null) database.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } What did I miss?
PS tried to implement the LoaderCallbacks interface as in the link example, but nothing happened either
try...catchblocks without error. Perhaps the error is just at the time of creation / opening of the database? And also at the time of the choice of data? Add stack output to allSQLiteException- ChubatiyString[] from = new String[] {Entry.COLUMN_COL2};- Yura Ivanov