如何将哈希图中的值用作应用程序不同片段上的文本?

时间:2019-04-02 16:23:14

标签: java android

我和我的团队正在创建一个应用程序,该程序显示今天,明天和整周的早餐,午餐和晚餐菜单选项。我们将周菜单放入哈希图中,其键为星期几。我们如何在“今天”选项卡,“明天”选项卡和“星期”选项卡中显示该字符串?我已经包括了单独的选项卡和主要活动的代码。

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Dining Hall Menu";
    private LinkedHashMap<String, String> menu = new LinkedHashMap<>();
    private SectionsPageAdapter mSectionsPageAdapter;
    private ViewPager mViewPager;


    public void readJsonStream(InputStream in) throws IOException {
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        try {
            readMessagesArray(reader);
        } finally {
            reader.close();
        }
    }

    public void readMessagesArray(JsonReader reader) throws IOException {
        int count = 0;
        reader.beginArray();
        while (reader.hasNext()) {
            readMessage(reader, count);
            count++;
        }
        reader.endArray();
    }

    public void readMessage(JsonReader reader, int count) throws IOException {
        String day = null;
        String food = null;
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("Monday") || name.equals("Tuesday") || name.equals("Wednesday")
                    || name.equals("Thursday") || name.equals("Friday")
                    || name.equals("Saturday") || name.equals("Sunday")) {
                day = name;
                food = reader.nextString();
                if (count == 0) {
                    menu.put(day, food);
                } else if (count == 1) {
                    menu.put(day, menu.get(day) + food);
                } else {
                    menu.put(day, menu.get(day) + food);
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            InputStream in = getAssets().open("Week11.json");
            readJsonStream(in);
        }
        catch(IOException e) {
            e.printStackTrace();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: Starting");

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        setupTabIcons(tabLayout);

    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment(), "Today");
        adapter.addFragment(new Tab2Fragment(), "Tomorrow");
        adapter.addFragment(new Tab3Fragment(), "Week");
        viewPager.setAdapter(adapter);
    }

public class Tab1Fragment extends Fragment {
    private static final String TAG = "Today";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab1_fragment, container, false);
        setHasOptionsMenu(true);
        return view;
    }
}

public class Tab2Fragment extends Fragment {
    private static final String TAG = "Tomorrow";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab2_fragment, container, false);
        setHasOptionsMenu(true);
        return view;
    }
}

public class Tab3Fragment extends Fragment {
    private static final String TAG = "Week";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab3_fragment, container, false);
        setHasOptionsMenu(true);
        return view;
    }
}

0 个答案:

没有答案