i have a tabbed layout with fragments on each page. on the first page there is a user input box and button that when inputted and clicked gives a toast. i would like to store the string of data in my mysql database and populate the list view on another fragment with data stored in the table from mysql. Was wondering how to go about this exactly?
main activity class
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from activity_main.xml setContentView(R.layout.activity_main); // Locate the viewpager in activity_main.xml ViewPager viewPager = (ViewPager) findViewById(R.id.pager); // Set the ViewPagerAdapter into ViewPager viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); } public void btnShout(View v) { //allows for label to be changed to shouted once button is pressed EditText txtInput = (EditText) findViewById(R.id.txtInput); TextView lblShout = (TextView) findViewById(R.id.lblShout); lblShout.setText("Shouted! "); //allows for toast to be displayed once button is clicked Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0); toast.makeText(MainActivity.this, txtInput.getText() + " Has Been Shouted.", toast.LENGTH_SHORT).show(); } here is my activity main xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.PagerTabStrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:paddingBottom="10dp" android:paddingTop="10dp" android:textColor="#000000" /> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </android.support.v4.view.ViewPager> viewpageradapter class
public class ViewPagerAdapter extends FragmentStatePagerAdapter { final int PAGE_COUNT = 3; // Tab Titles private String tabtitles[] = new String[] {"Home","Shouts","Shouts" }; Context context; public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return PAGE_COUNT; } public Fragment getItem(int position) { switch (position) { // Open Fragment home.java case 0: FragmentHome fragmenthome = new FragmentHome(); return fragmenthome; // Open Fragment shouters.java case 1: FragmentShouts fragmentshouts = new FragmentShouts(); return fragmentshouts; case 2: FragmentShouts fragmentShouts = new FragmentShouts(); return fragmentShouts; } return null; } @Override public CharSequence getPageTitle(int position) { return tabtitles[position]; } here is my tab with the user input (home fragment class)
public class FragmentHome extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Get the view from fragment home.xml View view = inflater.inflate(R.layout.fragmenthome, container, false); return view; } here is fragment home xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="New Shout" android:id="@+id/lblShout" android:layout_marginBottom="134dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txtInput" android:layout_alignTop="@+id/lblShout" android:layout_alignParentStart="true" android:layout_marginTop="41dp" android:layout_alignParentEnd="true" android:hint="Enter New Shout..." /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SHOUT" android:id="@+id/button" android:layout_below="@+id/txtInput" android:layout_centerHorizontal="true" android:onClick="btnShout" /> </RelativeLayout> here is the fragment with the listview (shouts fragment class)
public class FragmentShouts extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Get the view from fragment shouts.xml View view = inflater.inflate(R.layout.fragmentshouts, container, false); return view; } here is the fragment shouts xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:contextClickable="true"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
No comments:
Post a Comment