2014-01-21 73 views
0

我想使用表格佈局來做一個視圖輸出。如何根據日期輸入(edittext)查看數據?

我的想法是這樣的。我有一個主頁,名爲activity_main.xml 當你點擊取消按鈕時,它會轉到一個總結頁面, ,如同data.xml一樣。 在data.xml中,我有一個日期編輯文本,當我輸入日期時,例如12/2/2013, ,並且在單擊按鈕搜索之後,它會向我顯示它的記錄。 但是,我不知道該怎麼做。 如果我沒有輸入任何日期並點擊「搜索」,它會顯示所有記錄。

現在,我可以顯示所有記錄,而不需要搜索數據。 以下是我的代碼。

有人可以幫助我與日期搜索? 我希望我已經清楚地解釋了我自己。

DBAdapter.java

public class DBAdapter { 



    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_DATE = "date"; 
    public static final String KEY_PRICE = "fuelprice"; 
    public static final String KEY_FUEL = "fuelpump"; 
    public static final String KEY_COST = "tcost"; 
    public static final String KEY_ODM = "odometer"; 
    public static final String KEY_CON = "fcon"; 

    private static final String TAG = "DBAdapter"; 

    private static final String DATABASE_NAME = "MyDB"; 
    private static final String DATABASE_TABLE = "fuelLog"; 
    private static final int DATABASE_VERSION = 2; 



    private static final String DATABASE_CREATE = 
      "create table fuelLog (_id integer primary key autoincrement, " + "date text not null, fuelprice text not null, fuelpump text not null, tcost text not null, odometer text not null, fcon text not null);"; 


     private final Context context;  

     private DatabaseHelper DBHelper; 
     private SQLiteDatabase db; 

     public DBAdapter(Context ctx){ 
      this.context = ctx; 
      DBHelper = new DatabaseHelper(context); 
     } 

     private static class DatabaseHelper extends SQLiteOpenHelper 
     { 
      DatabaseHelper(Context context){ 
       super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      } 

      public void onCreate(SQLiteDatabase db) 
      { 
       try{ 
        db.execSQL(DATABASE_CREATE);  
       }catch (SQLException e){ 
        e.printStackTrace(); 
       } 
      }//onCreate 

      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
      { 
       Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 
       db.execSQL("DROP TABLE IF EXISTS contacts"); 
       onCreate(db); 
      }//onUpgrade 

     }//DatabaseHelper 


     public DBAdapter open() throws SQLException 
     { 
      db = DBHelper.getWritableDatabase(); 
      return this; 
     }//open 


     //---closes the database---  
     public void close() 
     { 
      DBHelper.close(); 
     }//close 


     //---insert a log into the database--- 
     public long insertLog(String date, String fuelprice, String fuelpump,String tcost,String odometer,String fcon) 
     { 
      ContentValues initialValues = new ContentValues(); 
      initialValues.put(KEY_DATE, date); 
      initialValues.put(KEY_PRICE, fuelprice); 
      initialValues.put(KEY_FUEL, fuelpump); 
      initialValues.put(KEY_COST, tcost); 
      initialValues.put(KEY_ODM, odometer); 
      initialValues.put(KEY_CON, fcon); 


      return db.insert(DATABASE_TABLE, null, initialValues); 
     }//insertLog 






    // --retrieves all the data 
     public Cursor getAllLog() 
     { 
     return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_DATE, KEY_PRICE, KEY_FUEL,KEY_ODM,KEY_CON}, null, null, null, null, null); 
     } 


} 

summary.java

public class summary extends Activity{ 
    TableLayout tablelayout_Log = null; 
    Button searchButton = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.data); 



     tablelayout_Log = (TableLayout) findViewById(R.id.tableLayout_Log); 

     tablelayout_Log.setStretchAllColumns(true); 
     tablelayout_Log.setShrinkAllColumns(true); 


     //View 
       searchButton = (Button) findViewById(R.id.searchBtn); 
       searchButton.setOnClickListener(new OnClickListener() 
       { 
        public void onClick(View v) 
        { 
         try{ 
          refreshTable(); 
         } 
         catch (Exception e) 
         { 
          Log.d("Fuel Log", e.getMessage()); 
         } 
        } 

       }); 
       }//oncreate 



      public void refreshTable() 
      { 
       tablelayout_Log.removeAllViews(); 
       TableRow rowTitle = new TableRow(this); 
       rowTitle.setGravity(Gravity.CENTER_HORIZONTAL); 

       TextView title = new TextView(this); 
       title.setText("Fuel Log"); 
       title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18); 
       title.setGravity(Gravity.CENTER); 
       title.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 

       TableRow.LayoutParams params = new TableRow.LayoutParams(); 
       params.span = 5; 

       rowTitle.addView(title, params); 
       tablelayout_Log.addView(rowTitle); 
       DBAdapter dbAdaptor = new DBAdapter(getApplicationContext()); 
       Cursor cursor = null; 
       try 
       { 
        dbAdaptor.open(); 

        cursor = dbAdaptor.getAllLog(); 
        cursor.moveToFirst(); 
        do{ 
         long id = cursor.getLong(0); 
         String date = cursor.getString(1); 
         String price = cursor.getString(2); 
         String pump = cursor.getString(3); 

         String odometer = cursor.getString(4); 
         String fcon = cursor.getString(5); 


         TextView viewId = new TextView(getApplicationContext()); 
         viewId.setText("" + id); 

         TextView viewDate = new TextView(getApplicationContext()); 
         viewDate.setText(date); 

         TextView viewPrice = new TextView(getApplicationContext()); 
         viewPrice.setText(price); 

         TextView viewPump = new TextView(getApplicationContext()); 
         viewPump.setText(pump); 


         TextView viewOdometer = new TextView(getApplicationContext()); 
         viewOdometer.setText(odometer); 

         TextView viewCon = new TextView(getApplicationContext()); 
         viewCon.setText(fcon); 

         TableRow row = new TableRow(this); 
         row.setGravity(Gravity.CENTER_HORIZONTAL); 
         row.addView(viewId); 
         row.addView(viewDate); 
         row.addView(viewPrice); 
         row.addView(viewPump); 
         row.addView(viewOdometer); 
         row.addView(viewCon); 
         tablelayout_Log.addView(row); 
        } 
        while(cursor.moveToNext()); 
       } 
       catch(Exception e){ 
        Log.d("Fuel Log", e.getMessage()); 
       } 
       finally 
       { 
        if (cursor != null) 
         cursor.close(); 
        if(dbAdaptor != null) 
         dbAdaptor.close(); 
       } 
      }//refreshTable 

     }//main 

MainActivity.java

public class MainActivity extends Activity { 
    TableLayout tablelayout_Contacts = null; 
    Button insertButton = null; 
    EditText nameEdit = null; 
    EditText contactEdit = null; 
    Button viewButton = null; 
    Button deleteButton = null; 

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

      tablelayout_Contacts = (TableLayout) findViewById(R.id.tableLayout_Contacts); 

      tablelayout_Contacts.setStretchAllColumns(true); 
      tablelayout_Contacts.setShrinkAllColumns(true); 

      nameEdit = (EditText) findViewById(R.id.editText_Name); 
      contactEdit = (EditText) findViewById(R.id.editText_Number); 







    insertButton = (Button) findViewById(R.id.button1); 
    insertButton.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      DBAdapter dbAdaptor = new DBAdapter(getApplicationContext()); 
      try{ 
       dbAdaptor.open(); 
       String name = nameEdit.getText().toString(); 
       String number = contactEdit.getText().toString(); 
       dbAdaptor.insertContact(name, number); 
      } 
      catch (Exception e) 
      { 
       Log.d("Contact Manager",e.getMessage()); 
      } 
      finally{ 
       if(dbAdaptor != null) 
        dbAdaptor.close(); 
      } 
     } 
    }); 



    //View records 
    viewButton = (Button) findViewById(R.id.button2); 
    viewButton.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      try{ 
       refreshTable(); 
      } 
      catch (Exception e) 
      { 
       Log.d("Contact Manager",e.getMessage()); 
      } 
     } 
    }); 

    //delete records 
    deleteButton = (Button) findViewById(R.id.button3); 
    deleteButton.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      DBAdapter dbAdaptor = new DBAdapter(getApplicationContext()); 

      try{ 
       refreshTable(); 
       String name = nameEdit.getText().toString(); 


       if(!name.equals("")) 
       { 
        dbAdaptor.deleteContact(name); 
       } 

      } 
      catch (Exception e) 
      { 
       Log.d("Contact Manager",e.getMessage()); 
      } 
     } 
    }); 



    }//oncreate 











    //refresh table 


    public void refreshTable() 
    { 
     tablelayout_Contacts.removeAllViews(); 
     TableRow rowTitle = new TableRow(this); 
     rowTitle.setGravity(Gravity.CENTER_HORIZONTAL); 

     TextView title = new TextView(this); 
     title.setText("Contacts"); 
     title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18); 
     title.setGravity(Gravity.CENTER); 
     title.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 

     TableRow.LayoutParams params = new TableRow.LayoutParams(); 

     params.span =3; 
     rowTitle.addView(title,params); 
     tablelayout_Contacts.addView(rowTitle); 
     DBAdapter dbAdaptor = new DBAdapter(getApplicationContext()); 

     Cursor cursor = null; 

     try{ 
      dbAdaptor.open(); 
      cursor = dbAdaptor.getAllContacts(); 
      cursor.moveToFirst(); 
      do{ 
       long id = cursor.getLong(0); 
       String name = cursor.getString(1); 
       String contact = cursor.getString(2); 

       TextView idView = new TextView(getApplicationContext()); 
       idView.setText("" + id); 

       TextView nameView = new TextView(getApplicationContext()); 
       nameView.setText(name); 

       TextView contactView = new TextView(getApplicationContext()); 
       nameView.setText(contact); 

       TableRow row = new TableRow(this); 
       row.setGravity(Gravity.CENTER_HORIZONTAL); 
       row.addView(idView); 
       row.addView(nameView); 
       row.addView(contactView); 

       tablelayout_Contacts.addView(row); 
      } 
      while (cursor.moveToNext()); 
     } 
     catch (Exception e) 
     { 
      Log.d("Contact Manager", e.getMessage()); 
     } 
     finally{ 
      if (cursor != null) 
       cursor.close(); 
      if(dbAdaptor != null) 
       dbAdaptor.close(); 


      } 
    } 




} 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    tools:context=".MainActivity" > 


    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1"> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/datetxtview" 
       android:text="@string/date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
       <EditText 
       android:id="@+id/date" 
       android:text="" 
       android:inputType="date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/fuelpricetxtview" 
       android:text="@string/fuelprice" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
      <EditText 
       android:id="@+id/fuelprice" 
       android:text="" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/fuelpumptxtview" 
       android:text="@string/fuelpump" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
      <EditText 
       android:id="@+id/fuelpump" 
       android:text="" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 


      <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/totalcosttxtview" 
       android:text="@string/totalcost" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <TextView 
       android:id="@+id/tcost" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="" /> 

     </TableRow> 



     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/odometertxtview" 
       android:text="@string/odometer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <EditText 
       android:id="@+id/odometer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ems="10" > 

       <requestFocus /> 
      </EditText> 

     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow6" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/fctxtview" 
       android:text="@string/fc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <TextView 
       android:id="@+id/fcon" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="" /> 

     </TableRow>  
     </TableLayout> 


    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/saveBTN" 
      android:text="@string/save" 
      android:layout_width="wrap_content" 
      android:layout_height="60px" > 
     </Button> 

      <Button 
      android:id="@+id/updateBTN" 
      android:text="Update" 
      android:layout_width="wrap_content" 
      android:layout_height="60px" > 
     </Button> 
     <Button 
      android:id="@+id/cancelBTN" 
      android:text="@string/cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="60px" > 
     </Button> 
    </LinearLayout> 



</LinearLayout> 

data.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


<TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1"> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/datetxtview" 
       android:text="@string/date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 

      <EditText 
       android:id="@+id/datepast" 
       android:text="" 
       android:inputType="date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/fctxtview" 
       android:text="@string/fc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
      <EditText 
       android:id="@+id/fc" 
       android:text="" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
      <TextView 
       android:id="@+id/highpricetxtview" 
       android:text="@string/highprice" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </TextView> 
      <EditText 
       android:id="@+id/highprice" 
       android:text="" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
      </EditText> 
     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 
     <Button 
    android:id="@+id/searchBtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Search" /> 
    <Button 
    android:id="@+id/deleteBTN" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Delete" /> 
     <Button 
    android:id="@+id/backBTN" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Back" /> 
     </TableRow> 
     </TableLayout> 

<TableLayout  
     android:id="@+id/tableLayout_Log" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
    </TableLayout> 



</LinearLayout> 
+0

請按照以下指南改善您的問題:http://stackoverflow.com/help/mcve –

回答

0

你所得到的錯誤是一個空指針,這意味着一些對象,它是在MainActivity線205,或者是在該行使用的是空。先檢查一下,然後嘗試再次運行。

+0

searchButton =(Button)findViewById(R.id.searchBtn); \t \t searchButton.setOnClickListener(新OnClickListener()是空但爲什麼 – Chloe

+0

我剛剛更新了我的代碼 – Chloe

+0

嘗試是這樣的:?查看視圖= findViewById(R.id.tableRow4),然後搜索按鈕=(按鈕)查看.findViewById(R.id.searchBtn); – user3182577

0

眼前的問題是在這裏:

catch (Exception e) 
{ 
    Log.d("Fuel Log", e.getMessage()); 
} 

並非所有可拋出有一個消息,可以返回null。 A null無法記錄。它會導致堆棧跟蹤中的「println需要消息」異常。

首先,將其改變爲例如

catch (Exception e) 
{ 
    Log.e("Fuel Log", "", e); 
} 

這將記錄具有錯誤級別,空消息和完整堆棧跟蹤的異常。

然後,您可以首先看到導致該異常的原因。

+0

嗨,我剛更改我的代碼並編輯我的問題 – Chloe

0

你好日期類型在Sqlite數據庫中是不支持的。你把日期賦值爲文本,所以它需要字符串,所以你需要保持字符串的正確順序,以便它可以作爲日期搜索工作。

你可以存儲在2013年12月23日(20131223)形式的日期,那麼就可以得到由路過日期 您查詢,您可以嘗試像下面

public ArrayList<String> getEventsForNotification(String dateSearch) 

    { 
    ArrayList<String> arrayList=new ArrayList<String>(); 
     String sql="SELECT "+KEY_EVENT_NAME+" FROM "+ TABLE_EVENT +" WHERE SUBSTR("+KEY_EVENT_DATE+",6) like '"+dateSearch+"'"; 
     Cursor cursor=sqLiteDatabase.rawQuery(sql, null); 
     if(cursor.moveToFirst()) 
     { 

      do 
      { 
       arrayList.add(cursor.getString(0)); 
       }while(cursor.moveToNext()); 

      cursor.close(); 
      cursor=null; 
     } 
     return arrayList; 
    } 

根據您的需要進行修改。