【发布时间】:2018-03-27 07:28:51
【问题描述】:
DB类和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();
}
}
}
具有实现 Fragment 和 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;
}
}
}
我正在尝试将名为“name”(nameIndex) 的字段从数据库移动到 viewpager 屏幕。我无法通过创建 Fragment 将光标值移动到类中。我阅读了将数据输入列表的方法并从那里移动到 FragmentPager 所需位置的数量,但它并没有完全解决。有人可以知道一个简单而优雅的方法来解决这个问题吗?
【问题讨论】:
-
我看不到您发布的两个文件之间的关系。如果您需要 viewpager 片段中的数据,为什么不从 db 本身检索呢?
-
让这个通过类构造函数传递给
SectionsPagerAdapter,然后作为参数传入片段newInstance。 -
我把数据从数据库放到光标,然后把它放到数组“标题”并设置为ListView(这样做,因为在此之前构建ListView实现),比我实现ViewPager ,但我以前从未使用过它。我不明白我必须通过什么,即我的 ViewPagerAdapter 检索数据。光标、数组“标题”、arrayAdapter 或我的数组上的位置..
-
@grabarz121 我必须将什么传递给构造函数?光标、数组、ArrayAdapter 或我的数组上的位置?
-
@Anuraag Baishya Сan 你更详细地解释一下我必须从 DB 到 ViewPager 检索的具体内容?
标签: android database android-viewpager cursor