Android:如何为 Android ListView 实现这样的操作?

Posted

技术标签:

【中文标题】Android:如何为 Android ListView 实现这样的操作?【英文标题】:Android: How to Implement such operation for Android ListView? 【发布时间】:2012-01-27 19:51:51 【问题描述】:

我的应用程序具有以下功能:

我已经完成了使用自定义适配器设置值。但现在我想实现功能,比如添加另一个数据、删除显示数据、转到下一个数据并返回到以前的数据。在所有这些功能中,只有蓝色值应该改变。 (即代码中的 orderStatus)。左一个值 (orderName()) 应该保持不变。那么如何实现呢?

如果我要添加新数据,则应将其临时存储,因为我想在报告中显示该数据。

在我的应用程序中,我将首次添加数据,如下代码:

getOrder函数设置OrderName和OrderStatus的Value。

    public void getOrders()
        try
//              if(employee)
//              
                    System.out.println("You are in Employee");
                    m_orders = new ArrayList<Order>();
                    payListForEmployee = new String[] "Tax Code","Pay period Begin","Pay Frequency","Salary/Wage per pay",
                        "Net or Gross Amount","KS Employee deduction","KS Employee contributions","Child Support deduction","Payroll giving donation";

                    Order o[]=new Order[payListForEmployee.length];
                    System.out.println("The Length is: "+payListForEmployee.length);
                    for (int i = 0; i < payListForEmployee.length; i++) 
                    
                        o[i]=new Order();
                        o[i].setOrderName(payListForEmployee[i]);

                        //myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
                        // for TAXCODE
                        if(i==0) 
                            taxCodeValue = PAYEEmployeeDetail.taxCodeFinalValue;
                            if((taxCodeValue==null)) 
                                taxCodeValue = "Please select";
                            
                            o[i].setOrderStatus(taxCodeValue);
                            //System.out.println("The Value is: "+o[i].getOrderStatus());

                        
                        // for Pay Period Frequency
                        if(i==1) 
                            //payPeriodValue = myPrefs.getString("payperiod", "12 09 2011");
                            payPeriodValue =  PAYEEmployeeDetail.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 =  PAYEEmployeeDetail.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 =  PAYEEmployeeDetail.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 KS Employee Deduction
                        if(i==5) 
                            employeeDeductionValue =  PAYEEmployeeDetail.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==6) 
                            employeeContributionValue =  PAYEEmployeeDetail.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 Child Support Deduction
                        if(i==7) 
                            childSupportDeductionValue =  PAYEEmployeeDetail.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==8) 
                            payrollDonationValue =  PAYEEmployeeDetail.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: "+PAYEEmployeeDetail.taxCodeFinalValue);
//                      System.out.println("==> PAyPeriod Value: "+PAYEEmployeeDetail.payPeriodFinalValue);
//                      System.out.println("==> PayFrequency Value: "+PAYEEmployeeDetail.payFrequencyFinalValue);
//                      System.out.println("==> Salary/Wage Value: "+PAYEEmployeeDetail.salaryWageFinalValue);
//                      System.out.println("==> Gross/Net Value "+"STATIC VALUE = GROSS");
//                      System.out.println("==> Employee Deduction Value "+PAYEEmployeeDetail.employeeDeductionFinalValue);
//                      System.out.println("==> Employee Contribution Value "+PAYEEmployeeDetail.employeeContributionFinalValue);
//                      System.out.println("==> Child Support Deduction Value "+PAYEEmployeeDetail.childSupportDeductionFinalValue);
//                      System.out.println("==> PayRoll Donation Value "+PAYEEmployeeDetail.payrollDonationFinalValue);
//                      
                    
//              
//              else
//              
//                  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";
//                  
//                  System.out.println("The Length is: "+payListForEmployer.length);
//                  
//                  Order o[]=new Order[payListForEmployer.length];
//                  for (int i = 0; i < payListForEmployer.length; i++) 
//                  
//                      o[i]=new Order();
//                      o[i].setOrderName(payListForEmployer[i]);
//                      o[i].setOrderStatus("Pending");
//                      m_orders.add(o[i]);
//                  
//              

                Thread.sleep(100);
                Log.i("ARRAY", ""+ m_orders.size());
             catch (Exception e)  
                e.printStackTrace();
         
         runOnUiThread(returnRes);
    
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();
        
;

然后像这样创建和调用线程:

 viewOrders = new Runnable()
        @Override
        public void run() 
            getOrders();
        
    ;

    thread =  new Thread(null, viewOrders, "MagentoBackground");
    thread.start();

所以请帮助我为此提供逻辑或提供一些可以帮助我实现该功能的代码。 谢谢。

【问题讨论】:

【参考方案1】:

根据您的问题

我已完成使用自定义适配器设置值。但现在我想要 实现功能,如添加另一个数据,删除 显示数据,转到下一个数据并返回上一个数据。在这一切中 功能只有蓝色值应该改变。 (那就是 代码中的订单状态)。左一个值 (orderName()) 应保持 相同的。那么如何实现呢?

这是我的几个步骤的答案,并假设您在 var 'A' 中传递列表值:

1.您应该在列表的custom adapter 中扩展BaseAdapter

2. 将初始值设置为A 并将其传递给adapterset it to the listview

3. 现在,如果您想添加更多值,那么 add it in your var Acall the notifyDataSetChanged() 会将您的新值反映到列表中。

4.如果您想显示下一个值,那么还要在 A 中设置适当的值并调用 notifyDataSetChanged()

5.如果你想删除 setOnItemClickListener() 到你的列表,然后点击从 A 中删除值并调用 notifyDataSetChanged()

注意:要保存列表值,您可以使用 SQLite。您可以通过lisview 学习本教程。

【讨论】:

我不想将值保存到数据库中。但我将查看所有值以报告这就是我想要所有值的原因。 . 如果您不想保存这些值,请不要保存,但请确保您自己拥有报告的所有值.. 好的。那么我应该使用 ArrayList 还是 HashMap 来实现它?

以上是关于Android:如何为 Android ListView 实现这样的操作?的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何为参数变量设置默认值

如何为 AVD 管理器创建自定义 Android 设备配置文件?

如何为 Android 升级 ProGuard?

如何为菜单项Android添加行分隔符

Android:如何为图标创建通知徽章?

如何为现有的非 android Eclipse 项目制作 android manifest?