vendredi 16 octobre 2015

look up of text in a sqlite table makes the app crash

I have an edittext, a button and a sqlite table. The action of the button is a search of the text of the edittext in the table. I have the problem that the app works fine if the text introduced is composed by numbers, while it crashes if it contains letters. Here is the schema of the table:

    public static abstract class FeedEntry implements BaseColumns {
    public static final String TABLE_NAME = "entry";
    public static final String STOP = "entryid";
    public static final String LINE = "title";
    public static final String SCHEDULE = "schedule";
}

The table is initialized. Then the button has an onclick method whose function is:

public void lookUp(View v) {
        mEdit = (EditText) findViewById(R.id.text);
    String string = mEdit.getText().toString();

    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    String[] projection = {
            FeedReaderContract.FeedEntry.STOP,
            FeedReaderContract.FeedEntry.LINE,
            FeedReaderContract.FeedEntry.SCHEDULE,
    };

    String sortOrder =
            FeedReaderContract.FeedEntry.SCHEDULE + " ASC";

    Cursor cursor = db.query(
            FeedReaderContract.FeedEntry.TABLE_NAME,
            projection,
            FeedReaderContract.FeedEntry.LINE+" = " + string ,
            null,
            null,
            null,
            sortOrder
    );
    String result = "";

    int iRow = cursor.getColumnIndex(FeedReaderContract.FeedEntry.STOP);
    int iName = cursor.getColumnIndex(FeedReaderContract.FeedEntry.LINE);
    int iSchedule = cursor.getColumnIndex(FeedReaderContract.FeedEntry.SCHEDULE);

    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
        result = result + cursor.getString(iRow) + " " + cursor.getString(iName) +
                " " + cursor.getString(iSchedule) +
                "\n";
    }
    System.out.println(result);

    TextView textview = new TextView(getApplicationContext());
    textview.setText(result);
    RelativeLayout myLayout;
    myLayout = (RelativeLayout) findViewById(R.id.content_main);
    RelativeLayout.LayoutParams params =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    TextView tv = (TextView) findViewById(R.id.button);
    textview.setTextColor(Color.RED);
    params.addRule(RelativeLayout.BELOW, tv.getId());
    myLayout.addView(textview, params);
}

This function looks up in the table and if the element with the specific column value exists, it creates a new textview whose text is constituted by the elements of the row of the table. Why does the app crashes when I have letters in the edittext? It is strange because the columns of the table have string types, as well as nowhere when I look up of the table I specify that I am looking for numbers. Hope to have expressed my question in a clear way.

Aucun commentaire:

Enregistrer un commentaire