Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Chibueze Anakor
504 PointsHandling User Input
I'm a high school student trying to develop a student planner app, but I have no idea what to do after I've finished with the tabs. I have no idea of how to handle user input and display it in a ListView or ExpandableListView. I'm willing to share my code with anyone who will help me.
7 Answers
Ben Jakuben
Treehouse TeacherHi Chibueze,
The upcoming "Build a Self-Destructing Message App" project should help you with getting input and displaying things in a ListView. It's coming out next week. In the mean time, perhaps we can help you with some specifics. Do you want to paste in your code and ask some targeted questions? What kind of user input do you need to collect? The most basic item is text from the EditText widget.
Chibueze Anakor
504 PointsOkay, but my code doesn't have anything for the user input. All it has is the code for the tabs.
Here goes nothing:
MainActivity.java:
package com.rokanainteractive.eagenda;
import com.rokanainteractive.eagenda.R; import com.rokanainteractive.eagenda.adapter.TabsPagerAdapter;
import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private TabsPagerAdapter mAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
private String[] tabs = {"Homework", "Classes", "Scores"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
Chibueze Anakor
504 PointsHomeworkFragment.java:
package com.rokanainteractive.eagenda;
import com.rokanainteractive.eagenda.R;
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
public class HomeworkFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_homework, container, false);
return rootView;
}
}
Chibueze Anakor
504 PointsClassesFragment.java:
package com.rokanainteractive.eagenda;
import com.rokanainteractive.eagenda.R;
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
public class ClassesFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_classes, container, false);
return rootView;
}
}
Chibueze Anakor
504 PointsScoresFragment.java:
package com.rokanainteractive.eagenda;
import com.rokanainteractive.eagenda.R;
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
public class ScoresFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_scores, container, false);
return rootView;
}
}
Chibueze Anakor
504 PointsTabsPagerAdapter.java:
package com.rokanainteractive.eagenda.adapter;
import com.rokanainteractive.eagenda.ClassesFragment; import com.rokanainteractive.eagenda.HomeworkFragment; import com.rokanainteractive.eagenda.ScoresFragment;
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter{ public TabsPagerAdapter(FragmentManager fm) { super(fm); }
public Fragment getItem(int index) {
switch (index) {
case 0:
// Homework fragment activity
return new HomeworkFragment();
case 1:
// Classes fragment activity
return new ClassesFragment();
case 2:
// Scores fragment activity
return new ScoresFragment();
}
return null;
}
public int getCount() {
// Show 3 total pages.
return 3;
}
}
Chibueze Anakor
504 PointsDue to formatting problems, I can't add the XML files. I'm trying to get the user to input information about each homework assignment and class that they have. I want the end result to be that the homework tab has all the assignments in a ListView, with the upcoming ones separated from the late and completed ones, the classes tab will have 2 bottom tabs: the "Upcoming Classes" sub-tab and the "All Classes" sub-tab. The "Upcoming Classes" sub-tab is self-explanatory, but I also want the users to have the option of changing the day from a school day into a holiday or inclement weather day and the schedule will adjust accordingly. The "All Classes" sub-tab is self-explanatory as well, being just a list of all of the classes the user has. The "Scores" tab shows an ExpandableListView of all the user's grades in each class. When the user taps on the class score, it'll show the scores for each assignment.