根据按钮操作更改SQL查询

时间:2016-10-22 21:47:29

标签: java android sqlite android-sqlite

我有一个列表视图,显示我数据库中的所有项目。我正在尝试创建一个按钮,将显示的数据更改为仅显示与今天相匹配的项目。

根据按钮推送更新正在应用中运行的查询并更新列表视图的最佳方法是什么?

我已经在onclick()方法中设置了一个标志,该方法与持有查询调用的if-else语句配对,但它似乎没有切换被调用的语句。

该标志是boolean filterToday。在todayButton的onClickListener中设置。

public class MainActivity extends AppCompatActivity {


public final static String KEY_EXTRA_CONTACT_ID = "KEY_EXTRA_CONTACT_ID";
private ListView listView;
DBHelper dbHelper;
boolean filterToday;
Cursor cursor;
String [] columns;
int [] widgets;


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

    Button addButton = (Button) findViewById(R.id.addNew);
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, AddTaskActivity.class);
            intent.putExtra(KEY_EXTRA_CONTACT_ID, 0);
            startActivity(intent);
        }
    });

    Button todayButton = (Button) findViewById(R.id.today);
    todayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            filterToday = true;
        }
    });

    dbHelper = new DBHelper(this);

    if(filterToday == true){
        Calendar cal = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(cal.getTime());
        Toast.makeText(this,date,Toast.LENGTH_LONG).show();
        cursor = dbHelper.getTodaysTasks(date);
    }

    else{
        cursor = dbHelper.getAllTasks();
    }

    columns = new String[] {
            DBHelper.TASK_COLUMN_NAME,
            DBHelper.TASK_COLUMN_TYPE,
            DBHelper.TASK_COLUMN_DATE

    };
    widgets = new int[] {
            R.id.taskName,
            R.id.taskType,
            R.id.taskDate
    };


    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.task_info,
            cursor, columns, widgets, 0);
    listView = (ListView)findViewById(R.id.listView1);
    listView.setAdapter(cursorAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> listView, View view,
                                int position, long id) {
            Cursor itemCursor = (Cursor) MainActivity.this.listView.getItemAtPosition(position);
            int taskID = itemCursor.getInt(itemCursor.getColumnIndex(DBHelper.TASK_COLUMN_ID));
            Intent intent = new Intent(getApplicationContext(), AddTaskActivity.class);
            intent.putExtra(KEY_EXTRA_CONTACT_ID, taskID);
            startActivity(intent);
        }
    });

}
}

这是我试图切换的两个查询:

public Cursor getAllTasks() {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "SELECT * FROM " + TASK_TABLE_NAME, null );
    return res;
}

public Cursor getTodaysTasks(String date){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM " + TASK_TABLE_NAME + " WHERE " +
            TASK_COLUMN_DATE + " =?", new String[]{date});
    return res;
}

2 个答案:

答案 0 :(得分:0)

好的首先要注意的是,filterToday = true;听众中的onclick更新,if(filterToday == true){...}外部的人都不知道。该部分仅在oncreate()

中执行一次

如果您想执行该操作,我建议进行一些小改动......如下所示

在数据加载之外创建一个单独的函数

private void LoadMyData() {

if(filterToday == true){
    Calendar cal = Calendar.getInstance();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    String date = df.format(cal.getTime());
    //you might need to update this line
   //Toast.makeText(this,date,Toast.LENGTH_LONG).show();

   //assume dbHelper is ready at this point
    cursor = dbHelper.getTodaysTasks(date);
}

else{
    cursor = dbHelper.getAllTasks();
}

}

onclick listner

中调用此函数
 filterToday = false;//initialize to false if necessary
 todayButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        filterToday = !filterToday ;//by doing it like this you can switch back and forth. add/check extra logic if necessary
        LoadMyData();
    }
});
LoadMyData(); //Call this outside in the oncreate to load the data initially

我不建议在这里改进代码。只是改变你已经拥有的东西。希望它有所帮助。

答案 1 :(得分:0)

我遇到了两个问题,第一个是将字符串与SQL字符串[]进行比较,这意味着在比较工作时,它的行为并不像我预期的那样。修复此问题后,更新了ListView对象。这是通过创建一个新的适配器并将其附加到ListView来处理的,然后ListView自行更新。

根据这个答案:Refresh Current Fragment (ListView Data) remaining in the same activity

另一种选择是在适配器上调用notifyDataSetChanged(),但这与我设置程序的方式不一样。