动态更新viewpager

时间:2017-09-25 13:45:29

标签: android android-viewpager fragmentpageradapter android-pageradapter

我有一个ViewPager。数据存储在数据库中。我想动态更新它。元素必须立即消失,但它不起作用。在我调用方法onCreate之后更新数据。我尝试了所有的建议,但它没有帮助。我做错了什么?请帮我找一个解决方案。

TextPagerAdapter.java

public class TextPagerAdapter extends FragmentStatePagerAdapter {

List<String> data;

public TextPagerAdapter(FragmentManager fm, List<String> data) {
    super(fm);
    this.data = data;

}

@Override
public Fragment getItem(int i) {

    Fragment fragment = new ItemFragment();

    Bundle args = new Bundle();
    args.putString(ItemFragment.ARG_TEXT, data.get(i));
    args.putInt(ItemFragment.ARG_POSITION, i+1);
    args.putInt(ItemFragment.ARG_COUNT, getCount());

    fragment.setArguments(args);

    return fragment;

}

@Override
public int getCount() {

    return data.size();
}

public String getTextVP(int i) {
    return data.get(i);
}

@Override
public CharSequence getPageTitle(int position) {
    return "Item " + (position + 1);
}


@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}
}

ItemFragment.java

public class ItemFragment extends Fragment {

public static final String ARG_TEXT = "item_text";
public static final String ARG_POSITION = "item_position";
public static final String ARG_COUNT = "item_count";
TextView text1, textCount;

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(
            R.layout.fragment, container, false);

    Bundle args = getArguments();

    text1 = (TextView) rootView.findViewById(R.id.text1);
    text1.setText(args.getString(ARG_TEXT));

    textCount = (TextView) rootView.findViewById(R.id.txtCount);
    textCount.setText(args.getInt(ARG_POSITION) + " / " + args.getInt(ARG_COUNT));

    return rootView;
}
}

StartAppScreen.java

public class StartAppScreen extends AppCompatActivity implements View.OnClickListener {

private DatabaseHelper mDBHelper;
private SQLiteDatabase mDb;
ViewPager mViewPager;
TextPagerAdapter mTextPagerAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.patters_layout);

    List<String> data = new ArrayList<>();
    mDBHelper = new DatabaseHelper(this);

    init();

    Intent intent = getIntent();
    String condition = intent.getStringExtra("condition");
    String column = intent.getStringExtra("column");
    Boolean allWords = intent.getBooleanExtra("allWords", false);

    data.addAll(getDataFromTable(mDb, column, condition, allWords));

    if (allWords) Collections.shuffle(data);

    mTextPagerAdapter = new TextPagerAdapter(
            getSupportFragmentManager(), data);

    mViewPager.setAdapter(mTextPagerAdapter);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btnDel:
            String s = mTextPagerAdapter.getTextVP(mViewPager.getCurrentItem());
            /*Delete data from db*/
            mDb.delete(TABLE_NAME, WORD_ROW_NAME + "= ?", new String[] {s} );
            /*It is not work*/
            mTextPagerAdapter.notifyDataSetChanged();
            break;
    }
}


private void init() {

    try {
        mDBHelper.updateDataBase();
    } catch (IOException mIOException) {
        throw new Error("UnableToUpdateDatabase");
    }

    try {
        mDb = mDBHelper.getWritableDatabase();
    } catch (SQLException mSQLException) {
        throw mSQLException;
    }

    Button btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(this);
    Button btnDel = (Button) findViewById(R.id.btnDel);
    btnDel.setOnClickListener(this);
    mViewPager = (ViewPager) findViewById(R.id.pager);
}
}

1 个答案:

答案 0 :(得分:0)

实际上,您不会在TextPagerAdapter中更新字符串列表。您只能在DB中更新。再次获取数据并更改mTextPagerAdapter中数据字段的值。