There is a ListView in YearActivity - the list of months, after clicking on which in another ListMonth I want to display a list of only data from the month for which the choice was made:

 lvYear.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (position == 0) { Intent intent = new Intent(YearActivity.this, ListMonth.class); startActivity(intent); } } }); 

ListMonth:

 public class ListMonth extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> ... private static class MySimpleCursor extends CursorLoader { DB db; public MySimpleCursor(Context context, DB db) { super(context); this.db = db; } @Override public Cursor loadInBackground() { Cursor cursor = db.getData(); return cursor; } } 

There is a database. It has a method where I want to sort the data by month:

 public Cursor getData() { selectionArgs = new String[]{"11"}; selection = "month = ?"; orderBy = "day"; return sqLiteDatabase.query(DB_TABLE, null, selection, selectionArgs, null, null, orderBy); } 

How in selectionArgs to transfer, on what position the user has clicked?

selectionArgs = new String[]{"11"} - like this, for example, only November is displayed.

    1 answer 1

    In YourActivity , you have a transition to ListMonth activity. You need to put the date of the month in the Intent . For example, for November

     Intent intent = new Intent(YearActivity.this, ListMonth.class); intent.putExtra("month",11); startActivity(intent); 

    And in the onCreate() method of the onCreate() activity ListMonth get this number, translate it to String and use it in the query() method:

     String month = "" + getIntent().getIntExtra("month", 1); ... selectionArgs = new String[]{month}; ... 

    Now to the task

    Do you have a list of 12 months? Do they go in order? If yes, then you need to send position+1 . The addition of one is necessary, since the index is less than a position by one.

     Intent intent = new Intent(YearActivity.this, ListMonth.class); intent.putExtra("month" ,position+1); startActivity(intent); 

    Well, in the second activity do as I wrote above

    • Thanks for the amendment - Flippy
    • Sergey Grushin Thank you for the answer. - Bombey77
    • I can not transfer the Intent to the DB class and create an onCreate method in it. - Bombey77
    • @ Bombey77 why? - Sergey Gornostaev