问题
Right now i am using the ArrayList for My ListView and there is a Custom ListView in My Application.
See the Image Below:
Now i am using this code to display the list and set the value to the listView:
public void getOrders(){
try{
// if(employer)
// {
System.out.println("You are in Employer");
m_orders = new ArrayList<Order>();
payListForEmployer = new String[] {"Tax Code","Pay period Begin","Pay Frequency","Salary/Wage per pay",
"Net or Gross Amount","KiwiSaver Member","Employee Deduction","Employee Contributions",
"Complying Fund Member","Fund Contribution","ESCT Tax Rate","Child Support Deduction","Payroll giving donation"};
Order o[]=new Order[payListForEmployer.length];
System.out.println("The Length of payListEmployer is: "+payListForEmployer.length);
for (int i = 0; i < payListForEmployer.length; i++)
{
o[i]=new Order();
o[i].setOrderName(payListForEmployer[i]);
// for TAXCODE
if(i==0) {
taxCodeValue = PAYEEmployerDetail.taxCodeFinalValue;
if((taxCodeValue==null)) {
taxCodeValue = "Please select";
}
o[i].setOrderStatus(taxCodeValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// for Pay Period Begin
if(i==1) {
//payPeriodValue = myPrefs.getString("payperiod", "12 09 2011");
payPeriodValue = PAYEEmployerDetail.payPeriodFinalValue;
if((payPeriodValue==null)) {
payPeriodValue = "--/--/----";
}
o[i].setOrderStatus(payPeriodValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// for Pay frequency
if(i==2) {
//payFrequencyValue = myPrefs.getString("payfrequency", "Weekly");
payFrequencyValue = PAYEEmployerDetail.payFrequencyFinalValue;
if((payFrequencyValue==null)) {
payFrequencyValue = "Please select";
}
o[i].setOrderStatus(payFrequencyValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// for salary / wage
if(i==3) {
salaryWageValue = PAYEEmployerDetail.salaryWageFinalValue;
if((salaryWageValue==null)||(salaryWageValue.equals(""))) {
salaryWageValue = "$0";
}
else{
salaryWageValue = "$"+salaryWageValue;
}
o[i].setOrderStatus(salaryWageValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// For Gross / Net
if(i==4) {
o[i].setOrderStatus("Gross");
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// for KiwiSaver Member
if(i==5){
kiwiSaverMemberValue = PAYEEmployerDetail.kiwiSaverMemberFinalValue;
if((kiwiSaverMemberValue==null)||(kiwiSaverMemberValue.equals(""))){
kiwiSaverMemberValue = "No";
}else{
kiwiSaverMemberValue = PAYEEmployerDetail.kiwiSaverMemberFinalValue;
}
o[i].setOrderStatus(kiwiSaverMemberValue);
}
// for KS Employee Deduction
if(i==6) {
employeeDeductionValue = PAYEEmployerDetail.employeeDeductionFinalValue;
if((employeeDeductionValue==null)||(employeeDeductionValue.equals(""))) {
employeeDeductionValue = "0%";
}
o[i].setOrderStatus(employeeDeductionValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// for KS Employee Contribution
if(i==7) {
employeeContributionValue = PAYEEmployerDetail.employeeContributionFinalValue;
if((employeeContributionValue==null)||(employeeContributionValue.equals(""))) {
employeeContributionValue = "0%";
}
else{
employeeContributionValue = employeeContributionValue+"%";
}
o[i].setOrderStatus(employeeContributionValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// for Complying Fund Member
if(i==8){
complyingFundMemberValue = PAYEEmployerDetail.complyingFundMemberFinalValue;
if((complyingFundMemberValue==null)||(complyingFundMemberValue.equals(""))){
complyingFundMemberValue = "No";
}else{
complyingFundMemberValue = PAYEEmployerDetail.complyingFundMemberFinalValue;
}
o[i].setOrderStatus(complyingFundMemberValue);
}
// for Fund Contribution
if(i==9){
fundContributionValue = PAYEEmployerDetail.fundContributionFinalValue;
if((fundContributionValue==null)||(fundContributionValue.equals(""))) {
fundContributionValue = "$0";
}
else{
fundContributionValue = "$"+fundContributionValue;
}
o[i].setOrderStatus(fundContributionValue);
}
// for ESCT Tax Rate
if(i==10){
ESCTTaxRateValue = PAYEEmployerDetail.ESCTTaxRateFinalValue;
if((ESCTTaxRateValue==null)||(ESCTTaxRateValue.equals(""))) {
ESCTTaxRateValue = "0%";
}
o[i].setOrderStatus(ESCTTaxRateValue);
}
// for Child Support Deduction
if(i==11) {
childSupportDeductionValue = PAYEEmployerDetail.childSupportDeductionFinalValue;
if((childSupportDeductionValue==null)||(childSupportDeductionValue.equals(""))) {
childSupportDeductionValue = "$0";
}
else{
childSupportDeductionValue = "$"+childSupportDeductionValue;
}
o[i].setOrderStatus(childSupportDeductionValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
// for Payroll giving donation
if(i==12) {
payrollDonationValue = PAYEEmployerDetail.payrollDonationFinalValue;
if((payrollDonationValue==null)||(payrollDonationValue.equals(""))) {
payrollDonationValue = "$0";
}
else{
payrollDonationValue = "$"+payrollDonationValue;
}
o[i].setOrderStatus(payrollDonationValue);
//System.out.println("The Value is: "+o[i].getOrderStatus());
}
m_orders.add(o[i]);
//lv.invalidate();
// System.out.println("==> TaXCode Value: "+PAYEEmployerDetail.taxCodeFinalValue);
// System.out.println("==> PAyPeriod Value: "+PAYEEmployerDetail.payPeriodFinalValue);
// System.out.println("==> PayFrequency Value: "+PAYEEmployerDetail.payFrequencyFinalValue);
// System.out.println("==> Salary/Wage Value: "+PAYEEmployerDetail.salaryWageFinalValue);
// System.out.println("==> Gross/Net Value "+"STATIC VALUE = GROSS");
// System.out.println("==> Employee Deduction Value "+PAYEEmployerDetail.employeeDeductionFinalValue);
// System.out.println("==> Employee Contribution Value "+PAYEEmployerDetail.employeeContributionFinalValue);
// System.out.println("==> Child Support Deduction Value "+PAYEEmployerDetail.childSupportDeductionFinalValue);
// System.out.println("==> PayRoll Donation Value "+PAYEEmployerDetail.payrollDonationFinalValue);
//
}
Thread.sleep(100);
Log.i("ARRAY", ""+ m_orders.size());
} catch (Exception e) {
e.printStackTrace();
}
runOnUiThread(returnRes);
}
viewOrders = new Runnable(){
@Override
public void run() {
getOrders();
}
};
thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
private Runnable returnRes = new Runnable() {
@Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
}
// m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
};
and the OrderAdapter is like:
private class OrderAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> items;
public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.paye_list_row, null);
}
Order o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.value);
if (tt != null) {
tt.setText(o.getOrderName()); // Setting the Value here
}
if(bt != null){
bt.setText(o.getOrderStatus()); // Setting the Value here
System.out.println("The "+o.getOrderName()+" Value is: "+o.getOrderStatus());
}
}
return v;
}
}
There is also one Order class where i am going to get and set the Value by getter setter method.
Now i want to implement such thing for multiple employee. Example: right now it is for one emplpoyee, and if uesr click on add button, then present all the value of orderStatus should be stored. and after that i should be able to add the another employee detail (data).
I also want to go to Next Employee, to previoue Employee and also delete that shown employee.
So how it is possible with the HashMap? or mY code is good enough to implement it ? Please help me for that.
Thanks.
回答1:
Instead of using only one employee object, you could use an ArrayList<Employee> and use that one.
As you modify data, you should think about when to save it. Possible would be to save every change, or just to save if you change the employee.
I would recommend to save the data in the database, so you can easily read and write them.
A small tip: You should refactor your if (i==1) ... to a switch/case for more maintainability.
来源:https://stackoverflow.com/questions/8697542/how-to-use-arraylist-for-such-implementation-or-if-hashmap-is-good-for-it-then