0
我想爲我的學校建立一個食物菜單應用程序。我將每週都進入一個MySQL數據庫。我的應用程序有一個選項卡式視圖,每個片段代表一週中的某一天。如何在Android片段中顯示來自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);
}
}
應用的截圖