我正在使用swipe fragment
开发一个应用程序,我的问题是客户端希望如果他们刷视图,其中的数据将自动从sqlite表中保存,例如在视图中有{{ 1}}在向EditText
输入值后,当它们滑动到另一个视图或下一个项目时,该值将保存在EditText
中。
请帮助我
// PagerFragment.java类,我从xml
调用viewpagersqlite table
// ListviewAdapter.java
public class PagerFragment extends Fragment {
private List<PageData> data;
private int currentPosition;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager, container, false);
ViewPager mViewPager = (ViewPager) v.findViewById(R.id.pager_view);
currentPosition = getArguments().getInt("CURRENT_POSITION");
data = getArguments().getParcelableArrayList("DATA_LIST");
FragmentItemPagerAdapter fragmentItemPagerAdapter = new FragmentItemPagerAdapter(getFragmentManager(), data);
mViewPager.setAdapter(fragmentItemPagerAdapter);
mViewPager.setCurrentItem(currentPosition);
return v;
}
// FragmentItempagerAdapter.java
public class ListViewAdapter extends ArrayAdapter<PageData> {
protected List<PageData> data;
protected LayoutInflater inflater;
public ListViewAdapter(Activity activity, int textViewResourceId, List<PageData> objects){
super(activity, textViewResourceId, objects);
data= objects;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.item_list, parent, false);
PageData pd = data.get(position);
TextView tv2 = (TextView)row.findViewById(R.id.cat);
tv2.setText(pd.getCategory());
// TextView tv3 = (TextView)row.findViewById(R.id.desc);
// tv3.setText(pd.getCategory());
return row;
}
}
答案 0 :(得分:0)
将此方法放入您的片段
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (!visible) {
// called when fragment will change
// insertIntoTable this is a method for insert data into your sqlite table
insertIntoTable(edittext.getText().toString());
}
}
为sqlite表中的插入数据创建方法insertIntoTable
并将其放入sqlite查询中
答案 1 :(得分:0)
这是一个简单的例子。希望这有帮助。
这是一个带有公共方法的片段,用于获取edittext字符串 -
?> <?php
}
viewpager的适配器 -
public class TestFragment extends Fragment {
Context mContext;
EditText etData;
public TestFragment() {
mContext = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank, container, false);
etData = (EditText) view.findViewById(R.id.etData);
return view;
}
public String getEditTextData(){
return etData.getText().toString();
}
}
和活动 -
public class Adapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public Adapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new TestFragment();
}
@Override
public int getCount() {
//create two pages
return 2;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
所以逻辑是每当用户刷一次viewpager时,我们从被刷的视图中获取edittext的数据并将其插入到db中。现在要获取片段的edittext数据,我们借助适配器返回特定位置的片段实例。我测试了它,它工作正常!