使用EditText从SQLite中的两个日期获取数据

时间:2013-08-22 04:14:18

标签: android string sqlite date arraylist

我无法弄清楚如何在我的Activity中使用EditText来搜索sqlite的日期范围。我想我需要以某种方式制作editText1和2个字符串,但我不知道如何在我的数据库中这样做。

在sqlite中

public ArrayList<HashMap<String, String>> getAllsaleweekly() {
    ArrayList<HashMap<String, String>> wordList;
    GregorianCalendar gc = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd");
    String today = sdf.format(gc.getTime());
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM INSERTS WHERE saleDate BETWEEN '2012-Aug-16' AND '2013-Aug-21'";

除非我不想要2012年8月16日'和'2013年8月21日的静态值,否则这种方法很完美。我完全在我的arrayList中显示它只是来自sqlite的静态日期。

在我的活动中,我有两个editText ...

EditText editText1,editText2;
edittext1 =(EditText)findViewById(R.id.editText1);
edittext2 =(EditText)findViewById(R.id.editText2);

我有一个onClick for Button bydate

bydate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v){
            ArrayList<HashMap<String, String>> saleList =  controller.getAllsaleweekly();
            ListAdapter adapter = new SimpleAdapter(whatever.this,whateverList, R.layout.whatever, new String[] {"whatever","whatever", "whatever", "whatever","whatever","whatever", "whatever"},  new int[] { R.id.whatever, R.id.whatever, R.id.whatever, R.id.whatever,R.id.whatever, R.id.v, R.id.whatever}); 
setListAdapter(adapter);




        }}); 

2 个答案:

答案 0 :(得分:1)

修改您的onClickListener

    bydate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
          String date1 = edittext1.getText().toString(); 
          String date2 = edittext2.getText().toString();
          if(date1 != null && date2 != null){ 
                ArrayList<HashMap<String, String>> saleList =  controller.getAllsaleweekly(date1 ,date2);
                ListAdapter adapter = new SimpleAdapter(whatever.this,whateverList, R.layout.whatever, new String[] {"whatever","whatever", "whatever", "whatever","whatever","whatever", "whatever"},  new int[] { R.id.whatever, R.id.whatever, R.id.whatever, R.id.whatever,R.id.whatever, R.id.v, R.id.whatever}); 
    setListAdapter(adapter);
}

        }}); 

public ArrayList<HashMap<String, String>> getAllsaleweekly(String date1,String date2) {
    ArrayList<HashMap<String, String>> wordList;
    GregorianCalendar gc = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd");
    String today = sdf.format(gc.getTime());
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM INSERTS WHERE saleDate BETWEEN '" + date1 +"' AND '"+date2+"'";

答案 1 :(得分:1)

点击按钮,您可以获得Edittext的值

  String value1 = edittext1.getText().toString();
  String value2 = edittext2.getText().toString(); 

然后

  controller.getAllsaleweekly(value1,value2); 

更改

  public ArrayList<HashMap<String, String>> getAllsaleweekly(String date1,String date2)   
  {

   // use date1 and date 2 here
  }