如何在Android片段中显示MySQL数据库中的数据行?

时间:2016-06-21 18:21:26

标签: java android mysql android-fragments

我正在为我的学校建立一个食物菜单应用程序。我将每周将进餐计划纳入MySQL数据库。我的应用程序有一个标签视图,每个片段代表一周中的某一天。

我希望能够在相应的片段上显示相应的日期菜单。

MainActivity.java

    package me.anshsehgal.lunchmenu;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;

import android.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;  //make all the variables

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

        toolbar = (Toolbar)findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        tabLayout = (TabLayout)findViewById(R.id.tabLayout);
        viewPager = (ViewPager)findViewById(R.id.viewPager); //reference them
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.addFragments(new HomeFragment(),"M");
        viewPagerAdapter.addFragments(new MondayFragment(),"Tue");
        viewPagerAdapter.addFragments(new TuesdayFragment(),"W");
        viewPagerAdapter.addFragments(new WednesdayFragment(),"Thu");
        viewPagerAdapter.addFragments(new ThursdayFragment(),"F");   //adding fragments to view page adapter


        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_layout,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        RelativeLayout main_view = (RelativeLayout)findViewById(R.id.anshLayout);

        switch (item.getItemId()){
            case R.id.action_settings:
                if(item.isChecked())
                    item.setChecked(false);
                else
                    item.setChecked(true);
                Intent i = new Intent(this,Settings.class);
                startActivity(i);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
}

}

MondayFragment.java

package me.anshsehgal.lunchmenu;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class MondayFragment extends Fragment {


    public MondayFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_monday, container, false);
    }

}

应用的截图 Sample of the layout

Sample entry in table

1 个答案:

答案 0 :(得分:0)

不建议直接在Android应用程序中访问我的sql。您需要构建rest API,它将以压缩方式(JSON)公开所需的数据。您可以使用任何编程语言(如Java / C#/ Scala / Node)来公开休息端点。

如果您尝试存储特定于用户的数据,则可以使用sql lite数据库