所有功能都正常运行,但只存在一个问题,即如果我记录一天的详细信息,并且如果我将标签切换到历史记录选项卡以查看新记录,则历史记录类中的视图列表应显示该记录。但是视图列表不显示最新记录,并且我检查数据库,最新记录已写入数据库。查看新记录的唯一方法是关闭模拟器并重新启动它。
我发现问题在启动时,记录器和历史记录选项卡都将在OnCreate()方法中初始化。但之后,当我在这两个标签之间切换时,它不会被初始化。因此,在history.class中,它不会打开数据库并读取数据。那就是问题所在。请有人帮助我,谢谢
标签原型类
package com.example.tabpro;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class TabBar extends TabActivity{
static TabHost tabHost=null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost = getTabHost();
Intent recorderIntent = new Intent(TabBar.this, Recorder.class);
TabSpec recorderTabSpec = tabHost.newTabSpec("tab1");
recorderTabSpec.setIndicator("Recorder");
recorderTabSpec.setContent(recorderIntent);
tabHost.addTab(recorderTabSpec);
Intent historyIntent = new Intent(TabBar.this,History.class);
TabSpec historyTabSpec = tabHost.newTabSpec("tab2");
historyTabSpec.setIndicator("History");
historyTabSpec.setContent(historyIntent);
tabHost.addTab(historyTabSpec);
tabHost.setCurrentTab(0);
}
public void switchTab(int tab)
{
tabHost.setCurrentTab(tab);
}
}
Recorder.class
package com.example.tabpro;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
public class Recorder extends Activity implements OnSeekBarChangeListener {
int mYear;
int mMonth;
int mDay;
TextView dateDisplay;
Button pickDateButton;
TextView sbHourValue;
TextView sbMinValue;
int hours;
int mins;
static final int DATE_DIALOG_ID = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.recorder);
initialDatePicker();
initialSeekBar();
initialSaveButton();
}
public void initialDatePicker()
{
dateDisplay = (TextView)findViewById(R.id.dateDisplay);
pickDateButton = (Button)findViewById(R.id.pickDate);
pickDateButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar currentDate = Calendar.getInstance();
mYear = currentDate.get(Calendar.YEAR);
mMonth = currentDate.get(Calendar.MONTH);
mDay = currentDate.get(Calendar.DAY_OF_MONTH);
dateDisplay.setText(new StringBuilder()
.append(mYear).append("-")
.append(mMonth + 1).append("-")
.append(mDay));
}
public DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
dateDisplay.setText(new StringBuilder()
.append(mYear).append("-")
.append(mMonth + 1).append("-")//start from 0
.append(mDay));
}
};
protected Dialog onCreateDialog(int id){
switch(id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
}
return null;
}
public void initialSeekBar()
{
SeekBar sbHour = (SeekBar)findViewById(R.id.seekBar_hour);
sbHour.setMax(23);
sbHour.setProgress(0);
sbHour.setOnSeekBarChangeListener(this);
sbHourValue = (TextView)findViewById(R.id.textView_hour);
sbHourValue.setText("0"+ " hour(s)");
SeekBar sbMin = (SeekBar)findViewById(R.id.seekBar_min);
sbMin.setMax(59);
sbMin.setProgress(0);
sbMin.setOnSeekBarChangeListener(this);
sbMinValue = (TextView)findViewById(R.id.TextView_min);
sbMinValue.setText("0" + " minute(s)");
}
public void initialSaveButton()
{
Button saveButton = (Button)findViewById(R.id.button_save);
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String time;
String date;
date = dateDisplay.getText().toString();
time = hours + " hour(s) " + mins + " minute(s)";
TabProDB db;
db = new TabProDB(Recorder.this);
db.open();
boolean findSameDay = false;
Cursor c = db.GetAllRecords();
if(c!=null)
{
if (c.moveToFirst())
{
do {
String dateToCompare = c.getString(c.getColumnIndex(TabProDB.KEY_DATE));
if(dateToCompare.equalsIgnoreCase(date))
{
findSameDay = true;
}
} while (c.moveToNext());
}
}
if(findSameDay!=true)
{
long id = db.insertRecord(date, time);
db.close();
Toast.makeText(Recorder.this, "Record Saved" , Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Recorder.this, "You have already recorded the today: " + date, Toast.LENGTH_SHORT).show();
db.close();
}
switchTabInActivity(1);
}
});
}
public void switchTabInActivity(int index)
{
TabBar parentActivity;
parentActivity = (TabBar)this.getParent();
parentActivity.switchTab(index);
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
if(arg0.getId() == R.id.seekBar_hour){
// update hour value
hours = arg1;
sbHourValue.setText(Integer.toString(arg1)+" hour(s)");
}
else{
// update minute value
mins = arg1;
sbMinValue.setText(Integer.toString(arg1)+" minute(s)");
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
History.class
package com.example.tabpro;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class History extends Activity implements OnItemClickListener {
int getdbID;
String Date;
String Time;
ListView date_time_ListView;
ArrayList<String> listItems = null;
ArrayAdapter arrayAdapter = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
date_time_ListView = (ListView) findViewById(R.id.listView1);
TabProDB db = new TabProDB(this);
try{
listItems = new ArrayList<String>();
db.open();
Cursor c = db.GetAllRecords();
if(c!=null)
{
if (c.moveToFirst())
{
do{
String date1 = c.getString(c.getColumnIndex(TabProDB.KEY_DATE) );
String time1 = c.getString(c.getColumnIndex(TabProDB.KEY_TIME) );
String date_time1 = date1 + "\n" +time1;
listItems.add(date_time1);
}while (c.moveToNext());
}
}
db.close();
}catch (Exception e) {}
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems );
date_time_ListView.setAdapter(arrayAdapter);
date_time_ListView.setOnItemClickListener(this);
date_time_ListView.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder ad = new AlertDialog.Builder(History.this);
ad.setTitle("Delete?");
ad.setMessage("Are you sure you want to delete this record?");
final int positionToRemove = position;
String selectedFromList = (date_time_ListView.getItemAtPosition(position).toString());
String[] splitDateTime = selectedFromList.split("\n");
final String splitDate = splitDateTime[0];
Toast.makeText(History.this, splitDate, Toast.LENGTH_SHORT).show();
String splitTime = splitDateTime[1];
ad.setNegativeButton("Cancel", null);
ad.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
TabProDB db = new TabProDB(History.this);
try
{
db.open();
Cursor c = db.GetAllRecords();
if(c!=null)
{
if(c.moveToFirst())
{
do
{
//search database to find a date that equals the date on the listview
String findDate = c.getString(c.getColumnIndex(TabProDB.KEY_DATE));
if(splitDate.equalsIgnoreCase(findDate))
{
getdbID =c.getInt(c.getColumnIndex(TabProDB.KEY_ROWID));
db.deleteRow(getdbID);
break;
}
else
{
}
}while(c.moveToNext());
}
}
}
catch(Exception e){}
db.close();
listItems.remove(positionToRemove);
arrayAdapter.notifyDataSetChanged();
}
});
ad.show();
return false;
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(History.this, Update.class);
String str1[] = ((TextView) view).getText().toString().split("\n");
String str2 = str1[0];
String str3 = str1[1];
i.putExtra("date", str2);
i.putExtra("time", str3);
i.putExtra("position", position);
startActivity(i);
}
}
答案 0 :(得分:0)
final Intent i = new Intent().setClass(TabBar.this,
Recorder.class);
TabSpec spec1 = tabHost.newTabSpec("Recoder");
spec1.setContent(i);
spec1.setIndicator(getResources().getString(R.string.title_card_post1),
getResources().getDrawable(R.drawable.tab1));
final Intent i = new Intent().setClass(TabBar.this,
History.class);
TabSpec spec1 = tabHost.newTabSpec("History");
spec1.setContent(i);
spec1.setIndicator(getResources().getString(R.string.title_card_post1),
getResources().getDrawable(R.drawable.tab1));
tabHost.addTab(spec1);
tabHost.addTab(spec2);
试试这段代码,让我知道这是否适合你,因为这个解决方案对我有用,我的一个应用程序确实有它。