package com.example.computerbookstore;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class BookCategoryActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_category);
ListView listBook = findViewById(R.id.list_item);
SQLiteOpenHelper helper = new MySQLiteHelper(this);
try {
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query("BOOK", new String[]{"_id","NAME"},
null, null,null,null,null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, new String[]{"NAME"},
new int[]{android.R.id.text1}, 0);
listBook.setAdapter(adapter);
} catch (SQLiteException e) {
Toast.makeText(this, "数据库不能被创建", Toast.LENGTH_LONG).show();
}
listBook.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(this, BookActivity.class);
intent.putExtra(BookActivity.EXTRA_BOOKID, (int) l);
startActivity(intent);
}
}