2014-09-26 64 views
0
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(); 
     } 
    } 

} 

我已經上傳了我的DatabaseHelper類,請看。 我已經更新我的代碼在BaseAdapter中顯示的Sqlite數據ListView

我已經上傳我的DatabaseHelper類,請看。 我已經更新我的代碼

公共類DataBaseHelper擴展SQLiteOpenHelper {

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類,請看。 我已經更新我的代碼

+0

僅供參考,getReadableDatabase行爲應該在生產中被禁用,因爲它是效率低下,打破了許多應用程序。應該使用getWritableDatabase。請參閱此處:http://grepcode.com/file_/repository.grepcode.com/java/ext/com.google.android/android/5.L_preview/android/database/sqlite/SQLiteOpenHelper.java/?v=source – 2014-09-29 18:03:20

回答

0
​​

刪除ArrayList<Employee>因爲你在你的適配器使用arrayList改變employeesarrayList

arrayList = dataBaseHelper.getAllEmlopyee(); 
0
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 
} 
+0

不,它不工作。 在登錄貓它顯示錯誤等 錯誤開口跟蹤文件:沒有這樣的文件或目錄(2) 致命異常:主 android.database.CursorIndexOutOfBoundsException:索引9請求與一個大小爲9 @ MEHUL Joisar – 2014-09-26 11:44:23

+0

交'getAllEmlopyee()'方法的代碼,它有問題 – 2014-09-26 12:17:32

+0

我編輯了我的DatabaseHelper類檢查它。 – 2014-09-26 12:30:50