2017-01-23 123 views
-1

我正在開發一個應用程序與swipe fragment和我的問題是,客戶端需要,如果他們刷過視圖裏面的數據將自動從sqlite表保存例如在視圖中有在輸入EditText的值後,它內部的EditText,當它們滑動到另一個視圖或下一個項目時,該值將保存在sqlite table中。 PLEASE HELP ME插入到SQLITE表上刷卡片段

//PagerFragment.java類,其中i呼籲viewpager從XML

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; 


    } 

//ListviewAdapter.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; 
    } 
} 

//FragmentItempagerAdapter.java

public class FragmentItemPagerAdapter extends FragmentStatePagerAdapter { 
    private List<PageData> data; 



    public FragmentItemPagerAdapter(FragmentManager fm, List<PageData> data){ 
     super(fm); 
     this.data = data; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = new PageFragment(); 
     Bundle args = new Bundle(); 
     args.putString(PageFragment.id, data.get(position).getId()); 
     args.putString(PageFragment.cate, data.get(position).getCategory()); 
     args.putString(PageFragment.code, data.get(position).getCode()); 
     args.putString(PageFragment.desk, data.get(position).getDesk()); 
     args.putString(PageFragment.unit, data.get(position).getUnit()); 
     args.putString(PageFragment.quan, data.get(position).getQuantity()); 

     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return data.size(); 
    } 

    public static class PageFragment extends Fragment{ 
     public static final String id = "id"; 
     public static final String cate = "category"; 
     public static final String code = "code"; 
     public static final String desk = "description"; 
     public static final String unit = "unit"; 
     public static final String quan = "Quantity"; 



     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_item, container, false); 
      ((TextView) rootView.findViewById(R.id.item_label2)).setText(
        getArguments().getString(cate)); 
      ((TextView) rootView.findViewById(R.id.item_label3)).setText(
        getArguments().getString(code)); 
      ((TextView) rootView.findViewById(R.id.item_label4)).setText(
        getArguments().getString(desk)); 
      ((TextView) rootView.findViewById(R.id.item_label5)).setText(
        getArguments().getString(unit)); 
      ((TextView) rootView.findViewById(R.id.item_label6)).setText(
        getArguments().getString(quan)); 
      ((TextView) rootView.findViewById(R.id.item_label1)).setText(
        getArguments().getString(id)); 
      ((EditText) rootView.findViewById(R.id.editText2)).setText(
        getArguments().getString(quan)); 

      return rootView; 

     } 


    } 

} 
+0

您使用Viewpager來實現此刷卡功能嗎?如果是這樣,請參閱此更改偵聽器。 https://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html –

+0

是的即時通訊使用viewpager和感謝的鏈接,但你可以給我一個例子,但仍然生病嘗試讀取它並儘快給你反饋:) – RaRa

+0

http://stackoverflow.com/questions/30368641/updating-the-contents-on-tab-changed-in-view-pager這個鏈接可能會幫助你的例子。執行您的數據庫操作,而不是此鏈接中提到的網絡操作。 –

回答

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查詢

+0

先生我不能找到我要把它放在哪裏 – RaRa

+0

你在viewpager中添加片段的權利? –

+0

是的,先生我張貼我的代碼爲你先生 – RaRa

0

內這裏有一個簡單的example.Hope這有助於。
這是一個公共方法的片段來獲得的EditText與字符串

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(); 
} 

}

適配器的viewpager -

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); 
} 

}

和活動 -

int prevPosition = 0; // first item position 0 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ViewPager viewPager = (ViewPager) findViewById(R.id.vp); 
    final Adapter adapter = new Adapter(getSupportFragmentManager()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override 
     public void onPageSelected(int position) { 
      // when moves from one view to another on swiping 
      TestFragment testFragment = (TestFragment) adapter.getRegisteredFragment(prevPosition); 
      prevPosition = position; 
      insertDataIntoTable(testFragment.getEditTextData()); 
     } 

     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    }); 
} 

所以邏輯是每次用戶滑動viewpager時,我們從視圖中獲取edittext的數據並將其插入到數據庫中。現在,獲取片段的edittext數據,我們藉助適配器在特定位置返回片段的實例。我測試了它,它工作正常!

+0

其良好的答案maam,但我試過了,我不能在我的代碼中實現它可以實現你的示例代碼在我的代碼中,如果你不那麼忙:(謝謝你這麼多,以支持我的注意力 – RaRa