android studio中onclick方法的错误

时间:2016-06-08 12:40:47

标签: android

我在警告对话框构建器中的getAll()方法中收到错误。它说非静态方法'getAll'不能从静态上下文中引用。我不知道为什么我不能从android studio中的数据库中调用getAll方法。我实际上是在列表视图中显示数据。

package com.example.user.swen;


import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import java.util.ArrayList;
import java.util.List;



import com.example.user.swen.DB.RecordsDataSource;
import com.example.user.swen.Model.Records;

public class Record extends ListActivity {


    public RecordsDataSource Recordsdatasource;
    ArrayAdapter<Records> RecordAdapter;
    List<Records> records;
    ListView listview;
    Records selectedRecord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        //Referencing the Database
        Recordsdatasource = new RecordsDataSource(this);
        Recordsdatasource.open();

        //set the listView to use the custom List Adapter
        records = (List<Records>) RecordsDataSource.getAll();
        RecordAdapter = new RecordAdapter(this, 0, (ArrayList<Records>) records);
        listview = (ListView) findViewById(android.R.id.list);
        listview.setAdapter(RecordAdapter);



        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedRecord = records.get(position);
                AlertDialog.Builder a_builder = new AlertDialog.Builder(Record.this);
                a_builder.setMessage("Duty: " + selectedRecord.getType())
                        .setCancelable(false)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                AlertDialog alert = a_builder.create();
                alert.setTitle(Html.fromHtml("<font color='#08ae9e'>Information</font>"));
                alert.show();


            }
        });

    }
}

数据库中的getAll()方法。

public List<Records> getAll() {
        List<Records> records = new ArrayList<Records>();

        Cursor cursor = database.query(DatabaseHelper.TABLE_RECORDS, allColumns,
                null, null, null, null, null);

        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                Records record = new Records();
                record.setId(cursor.getLong(cursor.getColumnIndex(DatabaseHelper.RECORD_ID)));
                record.setEmployeeName(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_EMPLOYEE_NAME)));
                record.setBirthDate(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_DATE_OF_BIRTH)));
                record.setAddress(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_ADDRESS)));
                record.setPhone(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_PHONE)));
                record.setType(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_DUTY_TYPE)));
                record.setBankNo(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_BANK_NO)));
                records.add(record);
            }
        }
        return records;

0 个答案:

没有答案