如何存储用户输入并从mysql数据库

时间:2016-03-25 20:23:04

标签: android mysql xml listview android-fragments

我有一个标签式布局,每页都有片段。在第一页上有一个用户输入框和按钮,当输入和点击时给出一个祝酒词。我想将数据字符串存储在我的mysql数据库中,并使用mysql中存储在表中的数据填充另一个片段的列表视图。想知道如何准确地解决这个问题吗?

主要活动类

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

}

这是我的活动主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类

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

这是我的选项卡,其中包含用户输入(主片段类)

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;

}

这里是片段主页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>

这是带有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;
}

这里是片段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>

0 个答案:

没有答案