Class with DB and realization of Adapter.
public class MainActivity extends AppCompatActivity {//implements
View.OnClickListener{
ArrayList<String> titles = new ArrayList<>();
ArrayAdapter arrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
titles);
listView.setAdapter(arrayAdapter);
try {
SQLiteDatabase myDatabase = this.openOrCreateDatabase("Places",
MODE_PRIVATE, null);
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS places (name VARCHAR,
time INT(2), solo INT(1), id INTEGER PRIMARY KEY)");
myDatabase.execSQL("INSERT INTO places (name, time, solo) VALUES
('One', 23, 1)");
myDatabase.execSQL("INSERT INTO places (name, time, solo) VALUES
('Two', 24, 2)");
myDatabase.execSQL("INSERT INTO places (name, time, solo) VALUES
('Three', 22, 1)");
myDatabase.execSQL("INSERT INTO places (name, time, solo) VALUES
('Four', 02, 2)");
Cursor c = myDatabase.rawQuery("SELECT * FROM places WHERE solo =
1", null);
int nameIndex = c.getColumnIndex("name");
int timeIndex = c.getColumnIndex("time");
int soloIndex = c.getColumnIndex("solo");
int idIndex = c.getColumnIndex("id");
if (c.moveToFirst()) {
titles.clear();
do {
titles.add(c.getString(nameIndex));
} while (c.moveToNext());
arrayAdapter.notifyDataSetChanged();
}
c.close();
myDatabase.execSQL("DELETE FROM places");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Class with realization Fragment and PagerAdapter:
public class MyGallery extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_gallery);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my_gallery,
container, false);
TextView textView = (TextView)
rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format,
getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
I'm trying to move a field named "name" (nameIndex) from the database to the viewpager screen. I can not move cursor values to a class with the creation of a Fragment. I read about the method of entering data into the List and moved from there, the number of the required position to the FragmentPager, but it did not quite work out. Can someone know a simple and elegant way to solve this problem?
When you instantiate your FragmentPagerAdapter, pass an ArrayList to adapter constructor as argument
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager(), titles);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
In your adapter use a reference, and get string from array, such like
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private ArrayList<String> strings;
public SectionsPagerAdapter(FragmentManager fm, ArrayList<String> strings) {
super(fm);
this.strings = strings;
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1, strings.get(position + 1));
}
@Override
public int getCount() {
return strings.size();
}
}
And finally, in your Fragment class, pass it as argument, and get as a global variable from onCreate method
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private String title;
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber, String title) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
args.putString("key", title);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
title = getArguments().getString("key");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my_gallery,
container, false);
TextView textView = (TextView)
rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format,
getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
Note. For better control on your code, if you have limited number of fragment, you should make an all cases using switch
statement. Calling position + 1
in you adapter may throw nullpointerexception, when an index of your array will be exceeded. You can start any postion using viewPager.setCurrentItem(position);
, example of FragmentPagerAdapter with switch statement should looks like
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return PlaceholderFragment.newInstance(position, strings.get(position));
case 1:
return PlaceholderFragment.newInstance(position, strings.get(position));
case 2:
return PlaceholderFragment.newInstance(position, strings.get(position));
default:
return something...
}
}