如何在Android中使用平均功能?

机器人技术

我想在列表视图中显示SQLite数据库所有列的平均值。我已经尝试过,但是当我运行该应用程序时它崩溃并获得Null指针异常。如何在SQLite db中使用avg函数。有人能帮我吗。提前致谢。

这是我的适配器类

public class PerformanceList_Adapter extends BaseAdapter
{
    Context context;
    ArrayList<Performance_Pojo> Performance_List;

    public PerformanceList_Adapter(Context context,
            ArrayList<Performance_Pojo> performance_List) {
        super();
        this.context = context;
        Performance_List = performance_List;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Performance_List.size();
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return Performance_List.get(position);
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Performance_Pojo PerformanceListItems =Performance_List.get(position);
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.performance_list_items, null);
        }


        TextView textView_Month = (TextView) convertView.findViewById(R.id.textView_PerformanceMonth);
        textView_Month.setText(PerformanceListItems.get_strPerformanceMonth());


        TextView textView_RateOne = (TextView) convertView.findViewById(R.id.textView_performance_rate_one);
        textView_RateOne.setText(PerformanceListItems.get_strPerformance_rate_one());

        TextView textView_RateTwo = (TextView) convertView.findViewById(R.id.textView_performance_rate_two);
        textView_RateTwo.setText(PerformanceListItems.get_strPerformance_rate_two());

        TextView textView_RateThree = (TextView) convertView.findViewById(R.id.textView_performance_rate_three);
        textView_RateThree.setText(PerformanceListItems.get_strPerformance_rate_three());

        TextView textView_RateFour = (TextView) convertView.findViewById(R.id.textView_performance_rate_four);
        textView_RateFour.setText(PerformanceListItems.get_strPerformance_rate_four());

        TextView textView_RateFive = (TextView) convertView.findViewById(R.id.textView_performance_rate_five);
        textView_RateFive.setText(PerformanceListItems.get_strPerformance_rate_five());


        return convertView;


    }


}

这是我的代码

public class Performance_Details extends Activity
{
    Spinner spinneEmployeeName;
    String selectedEmployeeName;
    String strSeparated_Id;
    String strSeparated_EmpName;
    DatabaseHelper databaseHelper;
    ListView list_PerformanceDetails;
    SQLiteDatabase db;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.performance_details);
        databaseHelper = new DatabaseHelper(this);
        databaseHelper.onOpen(db);

        spinneEmployeeName = (Spinner)findViewById(R.id.spinnerPerformance_EmployeeName);
        loadSerachEmpName();
        spinneEmployeeName.setOnItemSelectedListener(new OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                selectedEmployeeName = spinneEmployeeName.getSelectedItem().toString().trim();
                System.out.println("selectedProjectName " + selectedEmployeeName);

                String[] separated = selectedEmployeeName.split(" ");
                strSeparated_Id = separated[0].trim();
                System.out.println("strSeparated_Id  = " +strSeparated_Id);

                strSeparated_EmpName = separated[1].trim();
                System.out.println("strSeparated_EmpName = " +strSeparated_EmpName);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });


        list_PerformanceDetails = (ListView)findViewById(R.id.list_PerformanceDetails);
        showPerformanceDetails();

    }

    private void showPerformanceDetails()
    {
        ArrayList<Performance_Pojo> Performance_PojoList = new ArrayList<Performance_Pojo>();  
        Performance_PojoList.clear();   

        Cursor cursor = db.rawQuery("SELECT performance_month, AVG(performance_rate_one),  AVG(performance_rate_two),  AVG(performance_rate_three),  AVG(performance_rate_four),  AVG(performance_rate_five)  FROM performance where "+ "Emp_id" + " = ?",new String[]{strSeparated_Id});

        SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();

        if (cursor != null && cursor.getCount() != 0) 
        { 
            if (cursor.moveToFirst())
            {
                do
                {
                    Performance_Pojo Performance_PojoListItems = new Performance_Pojo();  

                    Performance_PojoListItems.set_strPerformanceMonth(cursor.getString(cursor.getColumnIndex("performance_month")));
                    Performance_PojoListItems.set_strPerformance_rate_one(cursor.getString(cursor.getColumnIndex("performance_rate_one")));
                    Performance_PojoListItems.set_strPerformance_rate_two(cursor.getString(cursor.getColumnIndex("performance_rate_two")));
                    Performance_PojoListItems.set_strPerformance_rate_three(cursor.getString(cursor.getColumnIndex("performance_rate_three")));
                    Performance_PojoListItems.set_strPerformance_rate_four(cursor.getString(cursor.getColumnIndex("performance_rate_four")));
                    Performance_PojoListItems.set_strPerformance_rate_five(cursor.getString(cursor.getColumnIndex("performance_rate_five")));

                    Performance_PojoList.add(Performance_PojoListItems);     

                }while (cursor.moveToNext());   
            }

            sqlDatabase.close();
            cursor.close();

        }


        PerformanceList_Adapter performanceList_Adapter = new PerformanceList_Adapter(Performance_Details.this, Performance_PojoList); 
        list_PerformanceDetails.setAdapter(performanceList_Adapter);  

    }



    private void loadSerachEmpName() 
    {
        DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
        // Spinner Drop down elements
                List<String> projectsName = databaseHelper.getStaffEmployeePerformer_Name();

                // Creating adapter for spinner
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, projectsName);

                // Drop down layout style - list view with radio button
                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                // attaching data adapter to spinner
                spinneEmployeeName.setAdapter(dataAdapter);
    }

}

这是我的原木猫

07-01 10:47:19.767: E/AndroidRuntime(394): FATAL EXCEPTION: main
07-01 10:47:19.767: E/AndroidRuntime(394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sqlitedemo/com.sqlitedemo.Performance_Details}: java.lang.NullPointerException
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.os.Looper.loop(Looper.java:123)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 10:47:19.767: E/AndroidRuntime(394):  at java.lang.reflect.Method.invokeNative(Native Method)
07-01 10:47:19.767: E/AndroidRuntime(394):  at java.lang.reflect.Method.invoke(Method.java:507)
07-01 10:47:19.767: E/AndroidRuntime(394):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 10:47:19.767: E/AndroidRuntime(394):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 10:47:19.767: E/AndroidRuntime(394):  at dalvik.system.NativeStart.main(Native Method)
07-01 10:47:19.767: E/AndroidRuntime(394): Caused by: java.lang.NullPointerException
07-01 10:47:19.767: E/AndroidRuntime(394):  at com.sqlitedemo.Performance_Details.showPerformanceDetails(Performance_Details.java:76)
07-01 10:47:19.767: E/AndroidRuntime(394):  at com.sqlitedemo.Performance_Details.onCreate(Performance_Details.java:67)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 10:47:19.767: E/AndroidRuntime(394):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-01 10:47:19.767: E/AndroidRuntime(394):  ... 11 more
海浪

db的未初始化,因此在尝试对rawQuery()null对象进行调用会崩溃

Cursor cursor = db.rawQuery("SELECT performance_month, AVG(performance_rate_one),  AVG(performance_rate_two),  AVG(performance_rate_three),  AVG(performance_rate_four),  AVG(performance_rate_five)  FROM performance where "+ "Emp_id" + " = ?",new String[]{strSeparated_Id});

SQLiteDatabase sqlDatabase = databaseHelper.getWritableDatabase();

如与初始化它getWritableDatabase()交换这两行和呼叫:您对下一行rawQuery()sqlDatabase,而不是db

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章