我正在进行一个Android项目,其中包含大学列表,点击该列表会显示大学详情。
以下是快照:
- 学校列表 - 学校详情 在学校详情的右上方工具栏中,我设置了一个小星形图标。单击该图标后,图标变为填充图标,并将该学校的名称保存到书签类中,以列表的形式显示。现在我希望也能存储详细信息。我的意思是,在单击已加书签的列表项时,我想存储并显示详细信息,就像SchoolDetails类一样。我想知道如何使用SQLite数据库来实现。请指导我如何做。
这是我的书签类。
任何建议都将受到热烈欢迎。
以下是快照:
- 学校列表 - 学校详情 在学校详情的右上方工具栏中,我设置了一个小星形图标。单击该图标后,图标变为填充图标,并将该学校的名称保存到书签类中,以列表的形式显示。现在我希望也能存储详细信息。我的意思是,在单击已加书签的列表项时,我想存储并显示详细信息,就像SchoolDetails类一样。我想知道如何使用SQLite数据库来实现。请指导我如何做。
这是我的书签类。
public class Bookmark extends AppCompatActivity {
private List<Bookmarkitem> ourBookmarkListItems = new ArrayList<Bookmarkitem>();
private ListView listView;
private BookmarkAdapter adapter;
DatabaseHelper dbhelper;
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookmark);
dbhelper = new DatabaseHelper(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Bookmark");
List<Bookmarkitem> bookmarkedSchools = dbhelper.getAllSchoolBookmark();
listView = (ListView) findViewById(R.id.list_bookmarked);
if (bookmarkedSchools.size() != 0) {
adapter = new BookmarkAdapter(this, bookmarkedSchools);
listView.setAdapter(adapter);
} else {
Toast.makeText(this, "You have no bookmark yet.", Toast.LENGTH_SHORT).show();
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(Bookmark.this, SchoolDetails.class);
startActivity(i);
}
});
}
这是我的数据库类,现在只存储学校的名称、标志和地址。
package com.example.user.educationhunt.database;
import android.accessibilityservice.GestureDescription;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.MenuItem;
import com.example.user.educationhunt.listner.DatabaseUpdatedListener;
import com.example.user.educationhunt.pojos.Bookmarkitem;
import java.util.ArrayList;
import java.util.List;
/**
* Created by user on 11/22/2016.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseUpdatedListener databaseUpdatedListener;
static final String DATABASE_NAME = "BookmarkDatabase";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME_BOOKMARK = "bookmark";
// Contact table columns name
private static final String ID = "Id";
private static final String SID = "SId";
private static final String NAME = "Name";
private static final String LOGO = "Logo";
private static final String LOCATION = "Location";
String createTableBookmark = "Create table if not exists `Bookmark` ("
+ "`name` TEXT," + "`location` TEXT," + "`logo` TEXT);";
String CREATE_SCHOOL_BOOKMARK_TABLE = "CREATE TABLE " + TABLE_NAME_BOOKMARK + "("
+ ID + " INTEGER PRIMARY KEY, "
+ SID + " INTEGER, "
+ NAME + " TEXT, "
+ LOGO + " TEXT, "
+ LOCATION + " TEXT " + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void insertBookmarkData(Bookmarkitem bookmarkitem) {
SQLiteDatabase db = getWritableDatabase();
ContentValues content = new ContentValues();
content.put("name", bookmarkitem.name);
content.put("location", bookmarkitem.address);
content.put("logo", bookmarkitem.logo);
db.insert("Bookmark", null, content);
}
public ArrayList<Bookmarkitem> getBookmarkist() {
String sql = "select * from Bookmark ";
ArrayList<Bookmarkitem> bookmarklist = new ArrayList<Bookmarkitem>();
Cursor c = getWritableDatabase().rawQuery(sql, null);
while (c.moveToNext()) {
Bookmarkitem info = new Bookmarkitem();
info.name = c.getString(c.getColumnIndex("name"));
info.address = c.getString(c.getColumnIndex("location"));
info.logo = c.getString(c.getColumnIndex("logo"));
bookmarklist.add(info);
}
c.close();
return bookmarklist;
}
public Bookmarkitem getBookmarkData(String bookmarkName) {
String sql = "select * from Bookmark where id='" + bookmarkName + "'";
Cursor c = getWritableDatabase().rawQuery(sql, null);
while (c.moveToNext()) {
Bookmarkitem info = new Bookmarkitem();
info.name = c.getString(c.getColumnIndex("name"));
info.address = c.getString(c.getColumnIndex("location"));
info.logo = c.getString(c.getColumnIndex("logo"));
}
c.close();
Bookmarkitem info = null;
return info;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// TODO Auto-generated method stub
sqLiteDatabase.execSQL(CREATE_SCHOOL_BOOKMARK_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int arg1, int arg2) {
// TODO Auto-generated method stub
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_BOOKMARK);
onCreate(sqLiteDatabase);
}
public void addSchoolBookmark(Bookmarkitem bookmarkitem, MenuItem menuItem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SID, bookmarkitem.getBookmarkID());
values.put(NAME, bookmarkitem.getName());
values.put(LOGO, bookmarkitem.getLogo());
values.put(LOCATION, bookmarkitem.getAddress());
//inserting row
if (db.insert(TABLE_NAME_BOOKMARK, null, values) != -1) {
databaseUpdatedListener.setDatabaseSuccess(bookmarkitem.getName(), menuItem);
} else {
databaseUpdatedListener.setDatabaseError("Failed to insert");
}
db.close();
}
public List<Bookmarkitem> getAllSchoolBookmark() {
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_NAME_BOOKMARK;
List<Bookmarkitem> bookmarkitems = new ArrayList<>();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Bookmarkitem bookmarkitem = new Bookmarkitem();
bookmarkitem.setBookmarkID(Integer.parseInt(cursor.getString(0)));
bookmarkitem.setName(cursor.getString(1));
bookmarkitem.setLogo(cursor.getString(2));
bookmarkitem.setAddress(cursor.getString(3));
bookmarkitems.add(bookmarkitem);
} while (cursor.moveToNext());
}
return bookmarkitems;
}
public void removeBookmarkItem(int sID) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME_BOOKMARK + " WHERE " + SID+ "= '" + sID+ "'");
db.close();
}
}
这是我的SchoolDetails类
package com.example.user.educationhunt;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.user.educationhunt.database.DatabaseHelper;
import com.example.user.educationhunt.fragment.About;
import com.example.user.educationhunt.fragment.Admission;
import com.example.user.educationhunt.fragment.FeeStructure;
import com.example.user.educationhunt.listner.DatabaseUpdatedListener;
import com.example.user.educationhunt.pojos.Bookmarkitem;
import com.example.user.educationhunt.pojos.OurSchool;
import java.util.ArrayList;
import java.util.List;
public class SchoolDetails extends AppCompatActivity implements DatabaseUpdatedListener {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
Boolean isStarFilled = false;
DatabaseHelper db;
OurSchool ourSchool;
private Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_details);
ourSchool = (OurSchool) getIntent().getSerializableExtra("school");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(ourSchool.getSchoolName());
db = new DatabaseHelper(this);
db.databaseUpdatedListener = this;
Toast.makeText(this, ourSchool.getSchoolName(), Toast.LENGTH_SHORT).show();
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.fav_school, menu);
this.menu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.star_School:
Bookmarkitem bookmarkitem = new Bookmarkitem();
bookmarkitem.setBookmarkID(ourSchool.getSchoolId());
bookmarkitem.setName(ourSchool.getSchoolName());
bookmarkitem.setLogo(ourSchool.getSchoolLogo());
bookmarkitem.setAddress(ourSchool.getSchoolAddress());
db.addSchoolBookmark(bookmarkitem, item);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
List<Bookmarkitem> bookmarkitems = db.getAllSchoolBookmark();
if (bookmarkitems.size() != 0) {
for (Bookmarkitem bookmarkitem : bookmarkitems) {
if (bookmarkitem.getBookmarkID() == ourSchool.getSchoolId()) {
isStarFilled = true;
break;
}
else isStarFilled=false;
}
if (isStarFilled) {
menu.getItem(0).setIcon(getResources().getDrawable(R.mipmap.starfilled));
}else if (isStarFilled.booleanValue()==true){
delete();
menu.getItem(0).setIcon(getResources().getDrawable(R.mipmap.star));
}
}
return true;
}
public void delete(){
List<Bookmarkitem> bookmarkitems = db.getAllSchoolBookmark();
db.removeBookmarkItem(ourSchool.getSchoolId()); }
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new About(), "ABOUT US");
adapter.addFragment(new Admission(), "ADMISSION");
adapter.addFragment(new FeeStructure(), "FEE");
viewPager.setAdapter(adapter);
}
@Override
public void setDatabaseSuccess(String schoolName, MenuItem item) {
Toast.makeText(this, schoolName + "successfully added as bookmark", Toast.LENGTH_SHORT).show();
item.setIcon(R.mipmap.starfilled);
}
@Override
public void setDatabaseError(String failureMessage) {
Toast.makeText(this, failureMessage, Toast.LENGTH_SHORT).show();
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
任何建议都将受到热烈欢迎。