我有一个活动,它有一个微调器来选择一天和一个列表,它应该根据从微调器中选择的项目显示列表项。
我正在查询sqlite数据库,并且必须对在微调器上选择的不同项目使用不同的查询。
我正在使用一个简单的游标适配器。
我为我的微调器设置了onItemSelected(AdapterView<?>
parent,View view,int position,long id。在那里我检查所选的微调器项目是否等于今天。
如果是这样,我需要更改显示的列表。
我的问题是,无论选择哪个微调项目,列表都保持不变。它不会改变。
请注意,我的微调器和列表位于同一页面上。有人可以为此建议一个解决方案吗?
public class RecentJobListActivity extends Activity {
ListView listView ;
private Spinner spinner;
private TakenJobDataBaseOpenHelper jobDatabaseHelper;
private Cursor cursor;
SimpleCursorAdapter cursoradapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent_job_list);
addItemsOnSpinner();
//This is the database helper class
jobDatabaseHelper = new TakenJobDataBaseOpenHelper(this);
String[] fromColumns = {TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_RequestID,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_Destination,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_CustomerName};
int[] toViews = {R.id.singleRequestID, R.id.singleDestination,R.id.singleCustomerName};
//when the page first loads the result of queru getAlljobs() will be shown.this works
cursor = jobDatabaseHelper.getAllJobs();
cursoradapter = new SimpleCursorAdapter(this,
R.layout.single_job_activity, cursor, fromColumns, toViews, 0);
listView = (ListView)findViewById(R.id.activities_list);
listView.setAdapter(cursoradapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.recent_job_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addItemsOnSpinner(){
spinner = (Spinner) findViewById(R.id.time_periods_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.time_periods_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String s = parent.getItemAtPosition(position).toString();
//even the log doesn't show.
if(s.equals("Today")){
Log.d("today","today");
cursor = jobDatabaseHelper.getTodayJobs();
cursoradapter.notifyDataSetChanged();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
谢谢!!!
答案 0 :(得分:0)
尝试使用CursorAdapter.swapCursor()。这应该有用。