Android 在BaseAdapter ListView中显示Sqlite数据

Android 在BaseAdapter ListView中显示Sqlite数据,android,sqlite,listview,Android,Sqlite,Listview,我上传了我的DatabaseHelper类,请看。 我已经更新了我的代码 ArrayList<Employee> employees = dataBaseHelper.getAllEmlopyee(); if (employees != null && employees.size() > 0) { here } 我上传了我的DatabaseHelper类,请看。 我已经更新了我的代码 ArrayList<Employee> employ

我上传了我的DatabaseHelper类,请看。 我已经更新了我的代码

ArrayList<Employee> employees = dataBaseHelper.getAllEmlopyee();
if (employees != null && employees.size() > 0) {
    here
} 
我上传了我的DatabaseHelper类,请看。 我已经更新了我的代码

ArrayList<Employee> employees = dataBaseHelper.getAllEmlopyee();
if (employees != null && employees.size() > 0) {
    here
} 
公共类DataBaseHelper扩展了SQLiteOpenHelper{

public class MainActivity extends ActionBarActivity {

    public EditText editText;
    ListView listView;
    public Button ok;
    public DataBaseHelper dataBaseHelper;
    public BaseAdapter baseAdapter;
    ArrayList<Employee> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0);
        setContentView(R.layout.activity_main);
        initializedAll();
    }

    private void initializedAll() {

        editText = (EditText) findViewById(R.id.editText);
        listView = (ListView) findViewById(R.id.listView);
        ok = (Button) findViewById(R.id.okbutton);
        dataBaseHelper = new DataBaseHelper(this);
        arrayList = new ArrayList<Employee>();
        baseAdapter = new BaseAdapter() {

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            @Override
            public View getView(int position, View view, ViewGroup viewGroup) {
                if (view == null) {
                    view = inflater.inflate(R.layout.list_item, null);
                }

                TextView message = (TextView) view
                        .findViewById(R.id.messagetextView);
                TextView datetime = (TextView) view
                        .findViewById(R.id.datetextView);
                message.setText(arrayList.get(position).getName());
                Date date = arrayList.get(position).getDate();
                datetime.setText(DateFormat.format("dd/MM/yyyy hh:mm:ss a",
                        date));
                return view;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return arrayList.get(position);
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return arrayList.size();
            }
        };

        listView.setAdapter(baseAdapter);

        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                save(v);
            }
        });

    }

    public void save(View v) {

        String namedb = editText.getText().toString();
        Date date = new Date();
        Employee employee = new Employee(namedb, date);
        arrayList.add(employee);
        baseAdapter.notifyDataSetChanged();
        Toast.makeText(getApplicationContext(), employee.toString(),
                Toast.LENGTH_SHORT).show();
        long inserted = dataBaseHelper.insertEmployee(employee);

        if (inserted >= 0) {
            Toast.makeText(getApplicationContext(), "Data Inserted",
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Data not Inserted",
                    Toast.LENGTH_SHORT).show();
        }

        ArrayList<Employee> employees = dataBaseHelper.getAllEmlopyee();
        if (employees != null && employees.size() > 0) {

        } else {
            Toast.makeText(getApplicationContext(), "No data found",
                    Toast.LENGTH_SHORT).show();
        }
    }

}
}

我应该写什么

public static final String DB_NAME = "task_management";
public static final int DB_VERSION = 1;

public static final String EMPLOYEE_TABLE = "employee";
public static final String ID_FIELD = "_id";
public static final String NAME_FIELD = "name";
public static final String TIME_DATE = "time_date";

public static final String EMPLOYEE_TABLE_SQL = "CREATE TABLE "
        + EMPLOYEE_TABLE + " (" + ID_FIELD + " INTEGER PRIMARY KEY, "
        + NAME_FIELD + " TEXT, " + TIME_DATE + " DATETIME);";

public DataBaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(EMPLOYEE_TABLE_SQL);
    Log.e("TABLE CREATOR", EMPLOYEE_TABLE_SQL);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

public long insertEmployee(Employee employee) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(NAME_FIELD, employee.getName());
    values.put(TIME_DATE, employee.getDatetime());

    long inserted = db.insertOrThrow(EMPLOYEE_TABLE, null, values);
    db.close();
    return inserted;
}

public ArrayList<Employee> getAllEmlopyee() {
    ArrayList<Employee> allemployee = new ArrayList<Employee>();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(EMPLOYEE_TABLE, null, null, null, null, null,
            null);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {

            int id = cursor.getInt(cursor.getColumnIndex(ID_FIELD));
            String name = cursor.getString(cursor
                    .getColumnIndex(NAME_FIELD));
            String datetime = cursor.getString(cursor
                    .getColumnIndex(TIME_DATE));
            Employee employee = new Employee(name, datetime);
            allemployee.add(employee);
            cursor.moveToNext();
        }
    }
    cursor.close();
    db.close();
    return allemployee;

}
在ListView中显示我的数据

我上传了我的DatabaseHelper类,请看。
我已经更新了我的代码

不,它不工作。在Log cat中,它显示错误,如打开跟踪文件时出错:没有这样的文件或目录2致命异常:main android.database.CursorIndexOutOfBoundsException:请求索引9,大小为9。@getAllEmlopyee方法的Mehul Joisarpost代码,它有问题fyi,应在生产中禁用getReadableDatabase行为,因为它效率低下,并且会破坏许多应用程序。应改用getWritableDatabase。请看这里:
ArrayList<Employee> employees = dataBaseHelper.getAllEmlopyee();
if (employees != null && employees.size() > 0) {
    here
} 
arrayList = dataBaseHelper.getAllEmlopyee();//save data in your existing arraylist
if (arrayList != null && arrayList.size() > 0) {
    baseAdapter.notifyDataSetChanged();//it will refresh your adapter with new data
}