Hi,
I'm doing a salary calculator application, and i'm using ListView.
When i add my first task it shows up like i want it to, but when i go
for the second one, it does not appear on the screen. I'm using tabs,
so the left tab is to add a shift, and the middle one is to see the
list. Sometimes when i switch back (after adding the second shift and
not seeing it on the list) the program crashes. Here are the codes
that i'm using:
"
package com.dg.android.salarycalculator;
import java.util.ArrayList;
import com.dg.android.salarycalculator.adapter.ShiftListAdapter;
import com.dg.android.salarycalculator.custom.CustomDigitalClock;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.TimePicker;
public class ViewSalaryActivity extends ListActivity {
private boolean switcher;
private Button timer;
private TextView cashView;
private SalaryApplication app;
private DigitalClock stopWatch;
private TimePicker startTimePicker;
private ShiftListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
switcher = false;
app = (SalaryApplication) getApplication();
TabHost th = (TabHost) findViewById(R.id.th);
th.setup();
TabSpec ts = th.newTabSpec("stopperView");
ts.setContent(R.id.stopperView);
ts.setIndicator("Stopper");
th.addTab(ts);
ts = th.newTabSpec("manualInput");
ts.setContent(R.id.manualInput);
ts.setIndicator("Manual Input");
th.addTab(ts);
ts = th.newTabSpec("settings");
ts.setContent(R.id.settings);
ts.setIndicator("Settings");
th.addTab(ts);
setUpButtons();
adapter = new ShiftListAdapter(app.getSalaries(), this);
setListAdapter(adapter);
}
@Override
protected void onResume()
{
super.onResume();
adapter.forceReload(app.getSalaries());
}
private void setUpButtons() {
stopWatch = (DigitalClock) findViewById(R.id.clock);
timer = (Button) findViewById(R.id.stopper);
cashView = (TextView) findViewById(R.id.cashView);
timer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!switcher)
{
app.StartTimer();
} else {
app.StopTimer();
ArrayList<Salary> tmp =
app.getSalaries();
cashView.setText(" ILS = " +
tmp.get(tmp.size()-1).getCash());
}
switcher = !switcher;
if(switcher) {
timer.setText(" End ");
} else {
timer.setText("Start");
}
}
});
}
}
"
"
package com.dg.android.salarycalculator;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import android.app.Application;
public class SalaryApplication extends Application {
private Stopper timer;
private double mainPayRate;
private ArrayList<Salary> salaries;
private ArrayList<SpecialDay> spcShift;
@Override
public void onCreate() {
super.onCreate();
timer = new Stopper();
salaries = new ArrayList<Salary>();
spcShift = new ArrayList<SpecialDay>(7);
mainPayRate = 20.7;
setUpSpcShift();
}
private void setUpSpcShift() {
Calendar friday = new GregorianCalendar(),
saturday = new GregorianCalendar(),
ntrl = new GregorianCalendar();
friday.setTime(new Date(0,0,0,20,0,0));
saturday.setTime(new Date(0,0,0,20,0,0));
ntrl.setTime(new Date(0,0,0,0,0,0));
SpecialDay tmp;
for(int i = 0; i < 7; i++)
{
spcShift.add(new SpecialDay());
}
tmp = new SpecialDay(friday, ntrl, mainPayRate);
spcShift.set(5, tmp);
tmp = new SpecialDay(ntrl, saturday, mainPayRate);
spcShift.set(6, tmp);
}
public void StartTimer() {
timer.Start();
}
public void StopTimer() {
timer.End();
Salary salary = new Salary(mainPayRate);
salary.Evaluate(timer, spcShift);
salaries.add(salary);
}
public Stopper getTimer() {
return timer;
}
public ArrayList<Salary> getSalaries()
{
return salaries;
}
public void setSpcShift(ArrayList<SpecialDay> spcShift) {
this.spcShift = spcShift;
}
public ArrayList<SpecialDay> getSpcShift() {
return spcShift;
}
public void setMainPayRate(double mainPayRate) {
this.mainPayRate = mainPayRate;
}
public double getMainPayRate() {
return mainPayRate;
}
}
"
"
package com.dg.android.salarycalculator.adapter;
import java.util.ArrayList;
import com.dg.android.salarycalculator.Salary;
import com.dg.android.salarycalculator.views.ShiftListItem;
import com.dg.android.salarycalculator.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class ShiftListAdapter extends BaseAdapter {
private ArrayList<Salary> shifts;
private Context context;
public ShiftListAdapter(ArrayList<Salary> shifts, Context context) {
super();
this.shifts = shifts;
this.context = context;
}
public int getCount() {
return shifts.size();
}
public Salary getItem(int position) {
return (null == shifts) ? null : shifts.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ShiftListItem sli;
if (null == convertView) {
sli = (ShiftListItem)View.inflate(context,
R.layout.shift_list_item, null);
} else {
sli = (ShiftListItem)convertView;
}
sli.setSalary(shifts.get(position));
return sli;
}
public void forceReload(ArrayList<Salary> shifts)
{
notifyDataSetChanged();
}
}
"
"package com.dg.android.salarycalculator.views;
import java.util.Date;
import com.dg.android.salarycalculator.R;
import com.dg.android.salarycalculator.Salary;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ShiftListItem extends LinearLayout {
private Salary salary;
private TextView itemTimeAndDate;
private TextView itemCashView;
public ShiftListItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
itemTimeAndDate = (TextView) findViewById(R.id.itemTimeAndDate);
itemCashView = (TextView) findViewById(R.id.itemCashView);
}
public void setSalary(Salary salary) {
this.salary = salary;
Date start = salary.getShiftTime().getStart().getTime();
Date end = salary.getShiftTime().getEnd().getTime();
itemTimeAndDate.setText("Time: (Start) " + start.getHours() +
":" +
start.getMinutes()
+" (End)" + end.getHours() +
":"+end.getMinutes()
+"|Date: " + (start.getDay() + 1) + "-" +
(end.getDay() +1 ) +
"/"+start.getMonth() + "/" + (start.getYear() + 1900));
itemCashView.setText(salary.getCash() + "" +
salary.getCurrency() +
" Total Time Of: "
+
salary.getShiftTime().getDifference(salary.getShiftTime().getStart(),
salary.getShiftTime().getEnd()));
}
public Salary getSalary() {
return salary;
}
}
"
"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost
android:id="@+id/th"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent">
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/stopperView"
>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/start"
android:id="@+id/stopper"
android:layout_above="@+id/cashView"
android:layout_centerHorizontal="true"></Button>
<TextView
android:text=" ILS = 0.00"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@id/cashView"
android:textSize="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"></TextView>
<DigitalClock
android:textSize="50dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/clock"
android:text="DigitalClock"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"></DigitalClock>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/manualInput"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/startTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Time"
android:layout_above="@+id/endTime"
/>
<Button
android:id="@id/endTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" End Time "
android:layout_centerHorizontal="true"
android:layout_above="@+id/addShift"
/>
<Button
android:id="@id/addShift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add A Shift"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/settings"
>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
"
"
<?xml version="1.0" encoding="utf-8"?>
<com.dg.android.salarycalculator.views.ShiftListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/itemTimeAndDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/itemCashView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</com.dg.android.salarycalculator.views.ShiftListItem>
"
P.S. When I added this.shifts = shifts and changed forceReload() to
forceReload(ArrayList<Salary> shifts) it looks like i've got the list
to populate, but at the beginning i had double items, and then at some
point it added a single item.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en