Search code examples
androidlistviewspinnersimplecursoradapternotifydatasetchanged

when a spinner item is selected the List doesn't change


I have an activity which has a spinner to select a day and a list that should show the list items according to the item selected from the spinner.

I'm querying the sqlite database and different queries must be used for different items selected on spinner.

I'm using a simple cursor adapter.

I have set onItemSelected(AdapterView<?> parent, View view, int position, long id) for my spinner. In that I check if the selected spinner item equals to today.

If so I need to change the list that is shown.

My problem is that the list stays the same no matter what spinner item is selected. It doesn't change.

Note that my spinner and the list are on same page. Can someone please suggest a solution for this?

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

            }
        });


    }

}

Thank you!!!


Solution

  • Try using CursorAdapter.swapCursor(). That should work.