Modified files:
1) 解決在 Header indexed 0 的問題;
2) 精簡java檔;夏季共用冬季的java檔:(可以刪除 SummerDisciplineItem.java,
SummerEventItem.java,
SummerEventWithDiscipline.java)
3)共用Eventitem.java (精簡化:刪除EventWithDiscipline.java)
WinterSports.java:
public class WinterSports extends ListActivity {
SportsData datahelper;
@Override
public void onCreate(Bundle winter) {
super.onCreate(winter);
datahelper = new SportsData();
//Header
ListView listview = getListView();
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView textview1 = (TextView)header.findViewById(R.id.headerTextView01);
textview1.setText("Winter Olympic: "); //textview1.setText(getEvent + " : ");
TextView textview2 = (TextView)header.findViewById(R.id.headerTextView02);
textview2.setText(Integer.toString(datahelper.wintersportsEng.length) + " sports");
listview.addHeaderView(header);
//Set customized Adapter
ArrayAdapter<String> adapter = new WinterAdapter(this, datahelper.wintersportsEng, datahelper.wintersportsChi, datahelper.wintersportsRus);
setListAdapter(adapter);
ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position != 0) { //In order to skip the first row index (0)
ListView listview = getListView();
String strTested = listview.getItemAtPosition(position).toString(); //取得 the selected row item
// Toast.makeText(getApplicationContext(), strTested,
// Toast.LENGTH_SHORT).show();
int flag = 0;
for (int i = 0; i < datahelper.winterDisciplines.length; i++) { //to compare with data for discipline check
if (strTested == datahelper.winterDisciplines[i]) {
flag = 1;
}
}
if (flag == 0) { //sport with no disciplines
Intent intent = new Intent(WinterSports.this,Eventitem.class);
intent.putExtra("EVENT",strTested);
intent.putExtra("SEASON", "winter");
intent.putExtra("SportOrDiscipline", "sport");//
startActivity(intent);
} else if (flag == 1) { // sport with disciplies
// List discipline items
Intent intent = new Intent(WinterSports.this,DisciplineItem.class);
intent.putExtra("DISCIPLINE",strTested);
intent.putExtra("SEASON", "winter");
startActivity(intent);
}
}
}
});
}
}
*****************************************************
SummerSports.java
*****************************************************
SummerSports.java
public class SummerSports extends ListActivity{
SportsData helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
helper = new SportsData();
//Header
ListView listview = getListView();
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView textview1 = (TextView)header.findViewById(R.id.headerTextView01);
textview1.setText("Summer Olympic: "); //textview1.setText(getEvent + " : ");
TextView textview2 = (TextView)header.findViewById(R.id.headerTextView02);
textview2.setText(Integer.toString(helper.summersportsEng.length) + " sports");
listview.addHeaderView(header);
ArrayAdapter<String> adapter = new WinterAdapter(this, helper.summersportsEng, helper.summersportsChi, helper.summersportsRus);
setListAdapter(adapter);
ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position != 0) {
ListView listview = getListView();
String disciplineTested = listview.getItemAtPosition(position).toString();
int flag = 0;
for (int i = 0; i< helper.summerDisciplines.length; i++ ){
if (helper.summerDisciplines[i] == disciplineTested){
flag = 1;
}
}
if (flag == 0){
Intent intent = new Intent(SummerSports.this, Eventitem.class);
intent.putExtra("EVENT", disciplineTested);
intent.putExtra("SEASON", "summer");
intent.putExtra("SportOrDiscipline", "sport");
startActivity(intent);
}else if (flag == 1){
//List discipline items
Intent intent = new Intent(SummerSports.this, DisciplineItem.class);
intent.putExtra("DISCIPLINE", disciplineTested);
intent.putExtra("SEASON", "summer");
startActivity(intent);
//Toast.makeText(SummerSports.this, helper.summersportsEng[position] + " has subitems!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
********************************************
DisciplineItem.java
public class DisciplineItem extends ListActivity{
public static final String PATH = "/databases";
private final String DATABASE_FILENAME = "yundon.sqlite";
DBConnection dbHelper;
SQLiteDatabase db = openDatabase();
String getDiscipline;
String season;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null){
getDiscipline = extras.getString("DISCIPLINE");
season = extras.getString("SEASON");
}
Cursor c = getDisciplines(getDiscipline);
c.moveToFirst();
final String [] strEng = new String [c.getCount()];
String [] strChi = new String [c.getCount()];
String [] strRus = new String [c.getCount()];
for (int i=0; i < strEng.length; i++){
strEng [i] = c.getString(0);
strChi [i] = c.getString(1);
strRus [i] = c.getString(2);
c.moveToNext();
}
c.close();
ArrayAdapter <String> adapter = new WinterAdapter(this, strEng, strChi, strRus);
setListAdapter(adapter);
ListView list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
//Toast.makeText(DisciplineItem.this, strEng[position], Toast.LENGTH_SHORT).show();
Intent intent = new Intent(DisciplineItem.this, EventWithDiscipline.class);
intent.putExtra("EVENT", strEng[position]);
intent.putExtra("SEASON", season);
intent.putExtra("SportOrDiscipline", "discipline");
startActivity(intent);
}
});
}
public static class DBConnection extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "yundon.sqlite";
private static final int DATABASE_VERSION = 1;
final SQLiteDatabase db;
public DBConnection(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
db = this.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public Cursor getDisciplines(String strSport){
// String table = "events";
// String [] columns = {"discipline", "disciplinechi", "disciplinerus"};
// String selection = "sport=?";
// String [] selectionArgs = new String [] {strSport};
// return db.query(table, columns, selection, selectionArgs, null, null, null);
String sql = "select distinct discipline, disciplinechi, disciplinerus from events where sport='" + strSport + "'";
Cursor c = db.rawQuery(sql, null);
return c;
}
public SQLiteDatabase openDatabase(){
try {
final String DATABASE_PATH = "/mnt/sdcard/databases";
final String PATH = "/databases";
File myDataPath = new File (DATABASE_PATH + PATH);
String databaseFilename = myDataPath + "/" + DATABASE_FILENAME;
if (!myDataPath.exists()){
myDataPath.mkdirs();
}
if (!(new File(databaseFilename)).exists()){
InputStream is = getResources().openRawResource(R.raw.yundon);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte [] buffer = new byte [8192];
int count = 0;
while ((count = is.read(buffer)) > 0){
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
SQLiteDatabase database = SQLiteDatabase.openDatabase(databaseFilename, null, RESULT_OK);
return database;
}catch (Exception e){
Log.i("DB", "DB_DIR_EXCEPTION: ");
}
return null;
}
}
********************************************
Eventitem.java
public class Eventitem extends ListActivity{
public static final String PATH = "/databases";
private final String DATABASE_FILENAME = "yundon.sqlite";
DBConnection dbHelper;
SQLiteDatabase db = openDatabase();
String getEvent;
String getSeason;
String getSportOrDiscipline;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null){
getEvent = extras.getString("EVENT");
getSeason = extras.getString("SEASON");
getSportOrDiscipline = extras.getString("SportOrDiscipline");
}
Cursor c = getEvents(
getEvent, getSportOrDiscipline, getSeason);
getEvent, getSportOrDiscipline, getSeason);
c.moveToFirst();
String [] str1 = new String [c.getCount()];
String [] str2 = new String [c.getCount()];
for (int i = 0; i < str1.length; i++){
str1[i] = c.getString(0);
str2[i] = c.getString(1);
c.moveToNext();
}
c.close();
//Header
ListView listview = getListView();
View header = getLayoutInflater().inflate(R.layout.header, null);
TextView textview1 = (TextView)header.findViewById(R.id.headerTextView01);
textview1.setText(getEvent + " :"); //textview1.setText(getEvent + " : ");
TextView textview2 = (TextView)header.findViewById(R.id.headerTextView02);
textview2.setText(Integer.toString(str1.length) + " events");
listview.addHeaderView(header);
//Custom adapter: EventAdapter
ArrayAdapter<String> adapter = new EventAdapter(this, str1, str2);
setListAdapter(adapter);
}
public static class DBConnection extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "yundon.sqlite";
private static final int DATABASE_VERSION = 1;
final SQLiteDatabase db;
public DBConnection(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
db = this.getReadableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public Cursor getEvents(String strSport, String season){
String table = "events";
String [] columns = {"event", "pic"};
String selection = sportOrDiscipline + "=? AND " + season + "=?";
//原來String selection = "sport=? AND winter=?";
String [] selectionArgs = new String [] {strSport, "y"};
return db.query(table, columns, selection, selectionArgs, null, null, null);
}
public SQLiteDatabase openDatabase(){
try {
final String DATABASE_PATH = "/mnt/sdcard/databases";
final String PATH = "/databases";
File myDataPath = new File (DATABASE_PATH + PATH);
String databaseFilename = myDataPath + "/" + DATABASE_FILENAME;
if (!myDataPath.exists()){
myDataPath.mkdirs();
}
if (!(new File(databaseFilename)).exists()){
InputStream is = getResources().openRawResource(R.raw.yundon);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte [] buffer = new byte [8192];
int count = 0;
while ((count = is.read(buffer)) > 0){
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
SQLiteDatabase database = SQLiteDatabase.openDatabase(databaseFilename, null, RESULT_OK);
return database;
}catch (Exception e){
Log.i("DB", "DB_DIR_EXCEPTION: ");
}
return null;
}
}
************************************************
沒有留言:
張貼留言