如何刷新第二个标签片段

时间:2017-01-17 07:25:19

标签: android sqlite android-fragments

我面临的一个问题是,当我从第一个标签片段向sqlite数据库插入数据时,当我从第二个标签片段中选择时,它无法立即显示,只能重新启动数据可以显示的应用。

主要活动

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{

//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
int ans =3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Adding toolbar to the activity
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Initializing the tablayout
    tabLayout = (TabLayout) findViewById(R.id.tabLayout);

    BasePagerAdapter adapter = new BasePagerAdapter(getSupportFragmentManager());
    Tab1 tab1 = new Tab1();
    Tab2 tab2 = new Tab2();
    Tab3 tab3 = new Tab3();

    adapter.addFragment(tab1, "1");
    adapter.addFragment(tab2, "2");
    adapter.addFragment(tab3, "3");
    //Initializing viewPager
    viewPager = (ViewPager) findViewById(R.id.pager);

    //Adding adapter to pager
    viewPager.setAdapter(adapter);

    //Adding onTabSelectedListener to swipe views
    tabLayout.setOnTabSelectedListener(this);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setupWithViewPager(viewPager);

}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}
public class BasePagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    public BasePagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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


    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }


    public void removeFragment(int position) {
        mFragmentTitleList.remove(position);
        mFragmentList.remove(position);
        notifyDataSetChanged();

    }
    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

}

}

第一个片段

public class Tab1 extends Fragment {



EditText editext,editext2,editext3;
Button test;
DBController db;

//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View h = inflater.inflate(R.layout.tab1, container, false);
    //Returning the layout file after inflating
    //Change R.layout.tab1 in you classest
    editext =(EditText)h.findViewById(R.id.editText);
    editext2 =(EditText)h.findViewById(R.id.editText2);
    editext3 =(EditText)h.findViewById(R.id.editText3);
    test = (Button)h.findViewById(R.id.button);

    test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           String name = editext.getText().toString();
           String hh = editext2.getText().toString();
           String image = editext3.getText().toString();

            SQLiteOpenHelper db = new DBController(getActivity());
            SQLiteDatabase db1 = db.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put("CategoryName", name);
            cv.put("CategoryH", hh);
            cv.put("CategoryImage", image);

            db1.insert("Category", null, cv);
            db1.close();
        }
    });



    return  h;
}

第二片段

public class Tab2 extends Fragment {
List<Category> category;
DBController db;
ListView testlist;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View h = inflater.inflate(R.layout.tab2, container, false);
    testlist = (ListView)h.findViewById(R.id.listview);
    category = getProList();
    testlist.setAdapter(new ViewAdapter());


        return h;

}
public class ViewAdapter extends BaseAdapter {

    LayoutInflater mInflater;

    public ViewAdapter() {
        mInflater = LayoutInflater.from(getActivity());
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.testlist_view, null);
        }

        final TextView Pname = (TextView) convertView.findViewById(R.id.TPname);
        Pname.setText("" + category.get(position).getCategoryName());

        final TextView PX = (TextView) convertView.findViewById(R.id.TPX);
        PX.setText("" + category.get(position).getCategoryH());

        final TextView PY = (TextView) convertView.findViewById(R.id.TPY);
        PY.setText("" + category.get(position).getCategoryImage());


        notifyDataSetChanged();

        return convertView;
    }
}

public List<Category> getProList() {

    String selectQuery = "SELECT  * FROM Category ";
    db = new DBController(getActivity());
    SQLiteDatabase db1 = db.getReadableDatabase();
    Cursor cursor = db1.rawQuery(selectQuery, null);
    List<Category> Prolist = new ArrayList<Category>();
    if (cursor.moveToFirst()) {
        do {
            Category list = new Category();
            list.setCategoryName(cursor.getString(cursor.getColumnIndex("CategoryName")));
            list.setCategoryW(cursor.getString(cursor.getColumnIndex("CategoryH")));
            list.setCategoryImage(cursor.getString(cursor.getColumnIndex("CategoryImage")));

            Prolist.add(list);
        } while (cursor.moveToNext());
    }

    return Prolist;
}

2 个答案:

答案 0 :(得分:0)

只需detach()attach()片段,它就会重新绘制,所以如果您在onCreateView()中获取数据,它将显示更新的数据。

答案 1 :(得分:0)

你几乎没有选择如何处理这个问题。

首先是使用CursorLoader。有一个documentation如何使用加载器。

第二是在两个片段之间实现回调。还有文档如何communicate between fragments

在你的情况下,只需在第二个片段public class Home_Fragment extends Fragment { public ProgressBar bar; public WebView mwebView; private View rootView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment rootView = inflater.inflate(R.layout.fragment_home_, null); bar=(ProgressBar)rootView.findViewById(R.id.progressBar); WebView view = (WebView) rootView.findViewById(R.id.webView); view.loadUrl("https://mbasic.facebook.com"); view.getSettings().setJavaScriptEnabled(true); view.setWebViewClient(new MyWebViewClient()); return rootView; } private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } @Override public void onPageStarted(final WebView view, final String url, final Bitmap favicon) { bar.setVisibility(View.VISIBLE); super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { bar.setVisibility(View.GONE); super.onPageFinished(view, url); } } } 中实现回调,并在将数据保存到数据库时在第一个片段中调用此方法。

onDataSaved

第二片段

public interface DataSavedListener {
    void onDataSaved();
} 

First Fragment

public class Tab2 extends Fragment implements DataSavedListener {
List<Category> category;
DBController db;
ListView testlist;

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

    View h = inflater.inflate(R.layout.tab2, container, false);
    testlist = (ListView) h.findViewById(R.id.listview);
    category = getProList();
    testlist.setAdapter(new ViewAdapter());


    return h;

}

@Override
public void onDataSaved() {
    getProList()
}
}