Activity not running (Android, Database) (Beginner)

Asked 2 years ago, Updated 2 years ago, 135 views

public class DBHelperForSearched extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "searched";
public static final String TABLE_NAME = "searched_table";
public static final String COL1 = "ID";
public static final String COL2 = "ITEM1";
public static final String COL3 = "ITEM2";


DBAdapterForSearched dbAdapter;
Cursor cursor;
ListView list;

public DBHelperForSearched(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + "(ITEM1 TEXT primary key," + " ITEM2 TEXT)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertDataToSearched(String item1, String item2) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, item1);
    contentValues.put(COL3, item2);

    long result = db.insert(TABLE_NAME, null, contentValues);

    if (result == -1) {
        return false;
    } } else {
        return true;
    }
}

public Cursor getListContents(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
}

}

The db helper code. We are creating an activity that stores a region without duplicate primary key values in db by setting it as primary key, but for some reason, the execution of that activity continues to stop. I'm not sure what the problem is. Please give me some advice.

Below is an activity that utilizes the above db.

---------------------------------------------------------------------------------------------------------------------------

public class Searched extends AppCompatActivity {

static String DATABASE_NAME;
static String TABLE_NAME;
static int DATABASE_VERSION;
private static final String TAG = "TestDataBaseActivity";
private DBHelperForSearched db;
ListView list;
public String data_city, data_country;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.searched_list);


    db = new DBHelperForSearched(this);


    list = (ListView) findViewById(R.id.searched_list);
    db.getListContents();

    final ArrayList<InfoClassForSearched> weatherList = new ArrayList<InfoClassForSearched>();
    Cursor data = db.getListContents();

    if (data.getCount() == 0) {
        Toast.makeText(this, "There are no contents in this list!", Toast.LENGTH_LONG).show();
    } } else {
        while (data.moveToNext()) {
            InfoClassForSearched newInfo = new InfoClassForSearched(data.getString(0), data.getString(1));
            weatherList.add(newInfo);
            DBAdapterForSearched dbAdapter = new DBAdapterForSearched(this, weatherList);
            list.setAdapter(dbAdapter);
        }
    }


    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            InfoClassForSearched selectedInfoClass = weatherList.get(position);
            data_city = selectedInfoClass.getCity();
            data_country = selectedInfoClass.getCountry();

            Intent intent = new Intent(Searched.this, MainActivity.class);

            intent.putExtra("data_city", data_city);
            intent.putExtra("data_country", data_country);


            setResult(Activity.RESULT_OK, intent);
            finish();

        }
    });
}

}

android database activity

2022-09-22 19:16

1 Answers

data.getString(1), data.getString(2) Modify to data.getString(0), data.getString(1)

There are only item1 and item2 columns in the sqlite table, but I think there is an error when I try to access column index 2.

----- Add -------

String createTable = "CREATE TABLE " + TABLE_NAME + "(ITEM1 TEXT primary key," + " ITEM2 TEXT)";

Here (ITEM1....this part

" (ITEM1... Try running it with a space after double quotation marks.


2022-09-22 19:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.