Im using the Horizontal MPAndroid chart to display income/Expense and the chart works for the most. I can change the information displayed although I can only change it if I do it in OnViewCreated. Nothing at all happens if I try doing it from the activity in which the fragment is displayed and I have absolutely no idea why. Although I am not 100% sure if I am setting the data the right way.
public class BudgetFragment extends Fragment{
private HorizontalBarChart mainChart;
private BarData data;
private BarDataSet dataset1;
private BarDataSet dataset2;
private int expenseSum = 0;
private int incomeSum = 0;
public MainActivityBudgetFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.budget_fragment, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mainChart = (HorizontalBarChart) view.findViewById(R.id.mainBudgetChart);
ArrayList<BarEntry> entries1 = new ArrayList<>();
ArrayList<BarEntry> entries2 = new ArrayList<>();
entries1.add(new BarEntry(10000, 5));
entries2.add(new BarEntry(10000, 5));
dataset1 = new BarDataSet(entries1, "income");
dataset2 = new BarDataSet(entries2, "expense");
//X-axis labels
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("income"); xVals.add("expense");
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(dataset1);
dataSets.add(dataset2);
//Add to chart
data = new BarData(xVals, dataSets);
mainChart.setData(data);
//Description and animation
mainChart.setDescription(""); // set the description
mainChart.setScaleYEnabled(false);
mainChart.setTouchEnabled(false);
mainChart.animateY(2000);
setDataExpense(200);//(This works fine)
setDataIncome(200); //(This works fine)
}
public void updateDataExpense(){
Log.e("updateTag", "Updated expense");
dataset2.removeEntry(1);
data.addEntry(new BarEntry(expenseSum, 1), 1);
dataset2.setColor(getResources().getColor(R.color.orange));
mainChart.notifyDataSetChanged(); // let the chart know it's data changed
mainChart.invalidate(); // refresh
}
public void updateDataIncome(){
Log.e("updateTag", "Updated Income");
dataset1.removeEntry(0);
data.addEntry(new BarEntry(newIncome, 0), 0);
dataset1.setColor(getResources().getColor(R.color.green));
mainChart.notifyDataSetChanged(); // let the chart know it's data changed
mainChart.invalidate(); // refresh
}
//(These do not work when called outside OnViewCreated)
private void setDataExpense(int sum){
expenseSum = (expenseSum + sum);
Log.d("ResumeTag", "expense set at " + expenseSum);
updateDataExpense();
}
private void setDataIncome(int sum){
incomeSum = (incomeSum + sum);
Log.d("ResumeTag", "income set at " + incomeSum);
updateDataIncome();
}
}
Let me know if I forgot anything important. I do not have much experience in asking questions on Stackoverflow.
Thank you for your help!
//Chris
Please try this :
public void updateDataIncome() {
Log.e("updateTag", "Updated Income");
dataset1.removeEntry(0);
data.addEntry(new BarEntry(newIncome, 0), 0);
dataset1.setColor(getResources().getColor(R.color.green));
data.notifyDataChanged(); // NOTIFIES THE DATA OBJECT
mainChart.notifyDataSetChanged(); // let the chart know it's data changed
mainChart.invalidate(); // refresh
}
Your question does say what action is performed for which you expect the data to be updated. On whatever action you want the data to be refreshed, you should have one of the listener and then call your functions that populates the data that you want to be refreshed.
For example in this code on selection of a value in Spinner, onItemSelected(..) gets invoked & within it we are calling the populate function that refreshes the data. This is partial code but you can find a complete example of using Adapter & OnItemSelectedListener. Hope this helps you.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, monthList);
// Setting the array adapter containing country list to the spinner widget
spinnerMonth.setAdapter(adapter);
AdapterView.OnItemSelectedListener monthSelectedListener = new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> spinner, View container,
int position, long id) {
Log.d("Logger:MainAct"," onItemSelected:Entry::::");
tvMonth.setText("Chart for "+strMonth[position]);
populateChartByMonth(strMonth[position]);
//Toast.makeText(getApplicationContext(), "Success : " + strMonth[position], Toast.LENGTH_SHORT).show();
Log.d("Logger:MainAct"," onItemSelected:Exit::::");
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Log.d("Logger:MainAct"," onNothingSelected:Entry/Exit");
}
};
entries1.add(new BarEntry(10000, 5));
This line x value is 1 and Y value is 10000
entries1.add(new BarEntry(5,10000));
Related
I've written a game where the user inputs the number of player and every player gets an own tab with an empty table.
Therefore I used a PagerAdapterClass (extends FragmentStatePagerAdapter) and a viewpager.
So every player has the same fragmentView.
Now the user can put variables into the table, bu everytime I switch between the tabs, the input gets lost.
Well, i 'fixed' that problem by adding this to my pageradapter:
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
But it's more stopping the viewpager from destroying than actually saving the data.
My main goal is to really save that stuff in that table.
I already tried https://stackoverflow.com/a/17135346/11956040 but i cannot get mContent because i cannot get the reference of the fragment, because all fragments are not created on their own but all at the same time (or something like that).
I also don't know how to set a Tag.
This way: https://stackoverflow.com/a/18993042/11956040
doesn't work for me.
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Toolbar toolbar = findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
...
//numPlayer = num of tabs
SectionsPagerAdapter adapter = new SectionsPagerAdapter(numPlayer, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(adapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
if(numPlayer >= 5) {
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
}
}
PagerAdapter:
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private int tabNum;
public SectionsPagerAdapter(int tabNum, FragmentManager fm) {
super(fm);
this.tabNum = tabNum;
}
#Override
public PlaceholderFragment getItem(int position) {
return PlaceholderFragment.newInstance(position);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
int playerNum = position + 1;
return "Spieler " + playerNum;
}
#Override
public int getCount() {
// Show 2 total pages.
return tabNum;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
}
Fragment:
public static PlaceholderFragment newInstance(int index) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle bundle = new Bundle();
bundle.putInt("player", index);
fragment.setArguments(bundle);
return fragment;
}
There must be a solution but I cannot find it or cannot implement it.
Pls help.
Solved my problem this way:
define 2 dimensional ArrayList for rows and columns and counter for columns:
private ArrayList<ArrayList<Integer>> columnArray;
private int column;
onCreateView (for fragments) set column = 0 and add one entry with an empty list to columnArray
and set the first rowList on column index of columnArray:
pointArray.add(column, new ArrayList<Integer>());
final ArrayList<Integer> rowList = pointArray.get(column);
fill the empty rowListwith 0 (maybe it also works in an other way, but I made it this way to have on empty EditTexts a 0 and can easily replace them)
define View.OnFocusChangeListener for all EditTexts like this:
/*I dont know if I could set column final in general,
but you need to set a final int because you call this value in an inner class*/
final int pos = column
for (int i = 0; i <= getEditTexts(pos).size() - 1; i++) {
EditText editTexts = getEditTexts(pos).get(i);
final String editTextsTag = editTexts.getTag().toString();
View.OnFocusChangeListener listener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, final boolean b) {
if (view.getTag().toString().equals(editTextsTag) && !b) {
//fills rowList
addEntries(pos, rowList);
//adds rowList to columnArray
columnArray.set(pos, rowList);
//save the columnsArray or use it
saveData(columnArray);
}
}
};
editTexts.setOnFocusChangeListener(listener);
define method which collects data from each cell, depending on column position (pos), add it to rowList
for example:
private void addEntries(int pos, ArrayList<Integer> rowList) {
for(int i = 0; i <= 16; i++) {
//this requires EditText_label, i made them dynamically
String edit_label = "edit_" + pos + i;
EditText editText = table.findViewWithTag(edit_label);
String mEditTextString = editText.getText().toString();
try {
int thisValue = Integer.parseInt(mEditString);
rowList.set(j, thisValue);
} catch (NumberFormatException e) {
//maybe you do not need this, but I need it for something else
int thisValue = 0;
rowList.set(j, thisValue);
}
}
}
define a method for saving the columnArray. I used an interface to give it to parent Activity: Here you can find how I made it
Otherwise you can convert the columnArray to a String and save it in a database.
NOTE
I made it with column value set beacuse I increase the value for every column I add during runtime using a method. If you just have one column, you dont need to set it. Just use 0 instead of pos, column
I want to display a list of match objects (match = two users having liked each other) in a recycler view with the help of an adapter.
This is my activity which is meant to display those matches:
public class MatchesActivity extends AppCompatActivity {
// variables:
private RecyclerView mMatchesRecyclerView;
private RecyclerView.Adapter mMatchItemAdapter;
private RecyclerView.LayoutManager mMatchesLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matches);
mMatchesRecyclerView = findViewById(R.id.matches_recyclerView);
mMatchesRecyclerView.setNestedScrollingEnabled(false);
mMatchesRecyclerView.setHasFixedSize(true);
// set layout manager & pass it to the recycler view:
mMatchesLayoutManager = new LinearLayoutManager(MatchesActivity.this);
mMatchesRecyclerView.setLayoutManager(mMatchesLayoutManager);
// set match adapter & pass it to the recycler view:
mMatchItemAdapter = new MatchItemAdapter(getMatchesList(), MatchesActivity.this);
mMatchesRecyclerView.setAdapter(mMatchItemAdapter);
// add test items to the recycler view:
Match testMatch = new Match("abcdefgh");
matchesList.add(testMatch);
mMatchItemAdapter.notifyDataSetChanged();
Log.d("MatchesActivity", "TEST LIST: " + matchesList.toString());
}
private ArrayList<Match> matchesList = new ArrayList<Match>();
private List<Match> getMatchesList() {
Log.d("MatchesActivity", "getMatchesList function: " + matchesList.toString());
return matchesList;
}
}
And this is my adapter which is supposed to inflate the relevant layout & populate it with relevant object data:
public class MatchItemAdapter extends RecyclerView.Adapter<MatchViewholder> {
private List<Match> mMatchesList;
private Context mViewContext;
public MatchItemAdapter(List<Match> matchesList, Context context) {
this.mMatchesList = matchesList;
this.mViewContext = context;
Log.d("MatchItemAdapter", "Constructor: " + mMatchesList.toString());
}
// inflate the layout:
#Override
public MatchViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_matches, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutView.setLayoutParams(lp);
MatchViewholder matchViewholder = new MatchViewholder(layoutView);
Log.d("MatchItemAdapter", "onCreateViewHolder: " + mMatchesList.toString());
return matchViewholder;
}
// populate each row within the layout:
#Override
public void onBindViewHolder(MatchViewholder holder, int position) {
Log.d("MatchItemAdapter", "onBindViewHolder: " + mMatchesList.toString());
holder.mMatchID.setText(mMatchesList.get(position).getMatchID());
}
#Override
public int getItemCount() {
return 0;
}
}
The Match class currently only takes matchID parameter which is string. An object is created with a default image and this matchID string.
At the moment, I have no real match objects from database ready, so I wanted to check that the recycler view along with adapter are working as expected before i move on to that later.
However, when I go to Matches Activity, it is empty, showing nothing at all. As you can see from the MatchesActivity onCreate method, I created a test Match object with matchID = "abcdefgh" and then added that to the matchesList. So I am expecting the "abcdefgh" text to be passed to the adapter and to be shown in the MatchesActivity.
My log statements indicate that the Match object has been created and added to the list successfully, however, getMatchesList() function returns an empty list which is then used in the Adapter constructor too, (I think this is) causing Activity not show anything.
I am relatively new to Android and Java development, especially recycler view and adapters, but from what I gathered it seems to be as if the
mMatchItemAdapter.notifyDataSetChanged();
is not working properly as everything seems to be fine up until that point. Any help would be appreciated!
You're returning 0. What you should do instead is return the length of the mMatchesList list.
#Override
public int getItemCount() {
return mMatchesList.size();
}
I have seen this question answered a few times, however none of the fixes have worked for me, so i'm reaching out.
I have built an app that features the Diolor Swipeable Cards Library (here) and now am trying to implement Course Card Filters.
Essentially when a user clicks a course filter we want to change the data that is being fed to the adapter.
Currently I am trying to update the data and calling notifyDataSetChanged() on the adapter, expecting the cards to refresh to show the new data set, however am finding that it is not refreshing at all.
Any help with this would be hugely appreciated.
All code below is from my Main Activity.
I declare the data set that i will be feeding to the adapter at the top of the activity:
ArrayList<CourseCardModel> courseCardModelList;
then in my onCreate() method I instantiate the adapter, attach it to the view, and call a generateCourseCards() method which populates the courseCardModelList with objects pulled from a firebase database.
// Set up and assign card adapter
ca = new CustomCardAdapter(CardsActivity.this, android.R.layout.simple_list_item_1, generateCourseCards());
flingContainer.init(CardsActivity.this, ca);
generateCourseCards() method
private ArrayList<CourseCardModel> generateCourseCards() {
Toast.makeText(getApplicationContext(), "Retrieving Courses", Toast.LENGTH_LONG).show();
courseCardModelList = new ArrayList<CourseCardModel>();
dbref = FirebaseDatabase.getInstance().getReference().child("courses");
// Retrieve the course data from Firebase db and cast as Course object
dbref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
Log.e("Count " ,"" + snapshot.getChildrenCount());
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
c = postSnapshot.getValue(Course.class);
System.out.println(c.getCourseName());
CourseCardModel model = new CourseCardModel();
model.setCourse(c);
courseCardModelList.add(model);
}
Collections.shuffle(courseCardModelList);
ca.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e("The read failed: ", databaseError.getMessage());
}
});
return courseCardModelList;
}
Attempt to update the dataset (a simple shuffle for the time being) and refresh the cards
// Shuffle the collection and refresh the cards
Collections.shuffle(courseCardModelList);
ca.notifyDataSetChanged();
EDIT: added adapter code
public class CustomCardAdapter extends ArrayAdapter {
private TextView courseName, uniName, entryStandards, courseDuration, studyMode, qualification,
studentSatisfaction, gradProspects, t1, t2, t3, t4, t5, t6;
ArrayList<CourseCardModel> items;
View v;
LayoutInflater vi;
public CustomCardAdapter(Activity context, int resource, ArrayList<CourseCardModel> courses) {
super(context, resource, courses);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
v = convertView;
if (v == null) {
v = vi.inflate(R.layout.course_card_inner_template, parent , false);
}
CourseCardModel c = (CourseCardModel) getItem(position);
if (c != null) {
courseName = (TextView) v.findViewById(R.id.courseCardCourseName);
uniName = (TextView) v.findViewById(R.id.courseCardUniName);
entryStandards = (TextView) v.findViewById(R.id.courseCardEntryStandards);
courseDuration = (TextView) v.findViewById(R.id.courseCardCourseDuration);
studyMode = (TextView) v.findViewById(R.id.courseCardStudyMode);
qualification = (TextView) v.findViewById(R.id.courseCardQualification);
studentSatisfaction = (TextView) v.findViewById(R.id.courseCardStudentSatisfaction);
gradProspects = (TextView) v.findViewById(R.id.courseCardGraduateProspects);
t1 = (TextView) v.findViewById(R.id.cardTV1);
t2 = (TextView) v.findViewById(R.id.cardTV2);
t3 = (TextView) v.findViewById(R.id.cardTV3);
t4 = (TextView) v.findViewById(R.id.cardTV4);
t5 = (TextView) v.findViewById(R.id.cardTV5);
t6 = (TextView) v.findViewById(R.id.cardTV6);
v.setBackgroundResource(R.drawable.newcard);
courseName.setText(c.getCourse().getCourseName());
uniName.setText(c.getCourse().getUniversity());
entryStandards.setText(c.getCourse().getEntryStandards());
courseDuration.setText(c.getCourse().getCourseDuration());
studyMode.setText(c.getCourse().getStudyMode());
qualification.setText(c.getCourse().getQualification());
studentSatisfaction.setText(c.getCourse().getStudentSatisfaction().toString() + " / 5");
gradProspects.setText(c.getCourse().getGradProspects() + " / 100");
}
if(position ==0)
{
//float alpha = (float) 0.8;
//v.setAlpha(alpha);
courseName.setVisibility(View.VISIBLE);
}
else if (position == 1){
// Prepare the View for the animation
v.setVisibility(View.VISIBLE);
float alpha = (float) 0.8;
float alpha2 = (float) 0.3;
courseName.setAlpha(alpha2);
uniName.setAlpha(alpha2);
entryStandards.setAlpha(alpha2);
courseDuration.setAlpha(alpha2);
studyMode.setAlpha(alpha2);
qualification.setAlpha(alpha2);
studentSatisfaction.setAlpha(alpha2);
gradProspects.setAlpha(alpha2);
t1.setAlpha(alpha2);
t2.setAlpha(alpha2);
t3.setAlpha(alpha2);
t4.setAlpha(alpha2);
t5.setAlpha(alpha2);
t6.setAlpha(alpha2);
v.setAlpha(alpha);
}
else {
v.setVisibility(View.INVISIBLE);
}
return v ;
}
public void updateData(ArrayList<CourseCardModel> courseCardModels) {
this.items = courseCardModels;
notifyDataSetChanged();
}
}
Problem is in this method.
public void updateData(ArrayList<CourseCardModel> courseCardModels) {
this.items = courseCardModels;
notifyDataSetChanged();
}
here you are giving another array reference to your adapter.
Just rewrite as below.
public void updateData(ArrayList<CourseCardModel> courseCardModels) {
this.items.clear();
this.items.addAll(courseCardModels);
notifyDataSetChanged();
}
Without adapter class provided my first guess would be that you messed the references up. Maybe you are shuffling the data that is not referenced from the adapter. Once you share your adapter's code, I'll update my answer.
== EDIT ==
Avoid referencing some external collection of data from adapter, and updating that referenced data. Updating adapter/list data should be done using adapter's interface and methods such as add(), addAll() or remove() It might happen that (parent) adapter makes clone/copy of your data and in that case updating external/referenced collection is not doing any good.
You're extending an ArrayAdapter which holds his own array of models (the array passed to the constructor). If you would like to update the items, do something like this:
ca.clear();
for (CourseCardModel object : courseCardModelList) {
ca.insert(object, ca.getCount());
}
ca.notifyDataSetChanged();
Or you can override the getItem method and return an item from your items array.
And another option would be extending BaseAdapter instead of the ArrayAdapter.
I want to start an explicit intent , Here is the code
public class TabView extends ListActivity {
List<String> list1strings = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tview);
ListView listview1 = (ListView) findViewById(R.id.list1);
TabHost th = (TabHost) findViewById(android.R.id.tabhost);
th.setup();
list1strings.add("Programming Language");
list1strings.add("Database");
list1strings.add("Java Technologies");
list1strings.add("Web Technologies");
list1strings.add("Application/Web Server");
list1strings.add("operating Systems");
list1strings.add("PHP framework");
list1strings.add("PHP CMS");
listview1.setAdapter(new ArrayAdapter<String>(TabView.this,
android.R.layout.simple_list_item_1, list1strings));
th.addTab(th.newTabSpec("Tab1").setIndicator("Training")
.setContent(R.id.tab1));
TabSpec specs = th.newTabSpec("Tab2");
specs.setContent(R.id.tab2);
specs.setIndicator("Workshops");
th.addTab(specs);
specs = th.newTabSpec("Tab 3");
specs.setContent(R.id.tab3);
specs.setIndicator("Reach Us");
th.addTab(specs);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String getpos = list1strings.get(position);
try {
Class<?> ourclass = Class.forName("in.brainwave.industrialtraining." + getpos);
Intent ourIntent = new Intent(TabView.this, ourclass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
"list1string" here have data and I want to start an intent when an item on ListView is clicked , but the problem is that class name cannot have space and "String getpos = list1strings.get(position);" returns the string from ArrayList which contains space , Is there any way so that I can start an Intent when any item is clicked.
Please guide me .
You can just initialize a second array/list with the classes of each item, so when you select item N you get the class at the same position. For example:
ArrayList<String> listItems = new ArrayList<String>();
ArrayList<Class> classes = new ArrayList<Class>();
listItems.add("Programming");
classes.add(YourProgrammingActivity.class);
// (...)
listItems.add("PHP CMS");
classes.add(YourPHPCMSActivity.class);
And then to start the right activity in the listener:
Class activityToStart = classes.get(position);
Intent ourIntent = new Intent(TabView.this, activityToStart);
startActivity(ourIntent);
As you can guess, each class you put in that list should be an activity (and be declared in AndroidManifest), otherwise your app will crash.
Also if this is a fixed list, replacing ArrayList with arrays is much less verbose:
String[SIZE] listItems = {"Programming", /* (...) */, "PHP CMS"};
Class[SIZE] classes = {YourProgrammingActivity.class, /* (...) */, YourPHPCMSActivity.class };
(...)
Class activityToStart = classes[position];
From an Object Oriented Perspective you can encapsulate this information in one class and do a for loop to do it in an automatic manner. Let me show you (and remember there are multiple possible options, in doing this).
Keep in mind that this is more or less pseudocode that looks exactly to Java but I programed from memory, so there could be errors and typos!
First of all let met tell you that there are some things that are done in a "bad" way. For example this piece of code will never work:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String getpos = list1strings.get(position);
try {
Class<?> ourclass = Class.forName("in.brainwave.industrialtraining." + getpos);
Intent ourIntent = new Intent(TabView.this, ourclass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Because you are storing Strings which are titles! Not even Java class names. The best way is to create a class that encapsulate the information you need. This class keeps all information you need to provide to the different components.
public class TabEntry {
// In here you save the real class to be invoked in the listener
private Class mEntryClass;
// You store the title of the tab
private String mTabTitle;
// In here you store the content tab id like R.id.tab3
private int mContentId;
// Add more fields as necessary
...
public TabEntry(Class clazz, String tabTitle, int contentId) {
this.mClass = clazz;
this.mTabTitle = tabTitle;
}
public Class getEntryClass() {
return this.mEntryClass;
}
public String getTabTitle() {
return this.mTabTitle;
}
// Add more getters as needed
}
So right now, you have created a class that encapsulates the information you need to create your tabs. Now in the code of the TabActivity:
public class TabView extends ListActivity {
List<TabEntry> mListTabEntries = new ArrayList<TabEntry>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tview);
ListView listview1 = (ListView) findViewById(R.id.list1);
TabHost th = (TabHost) findViewById(android.R.id.tabhost);
th.setup();
// Initalize your list with your tabs entries, this should be done outside this method
// to don't clutter your code
mListTabEntries.add(new TabEntry(Activity1.class, "Title", R.id.text1));
mListTabEntries.add(new TabEntry(Activity2.class, "Title2", R.id.text2));
listview1.setAdapter(new ArrayAdapter<String>(TabView.this,
android.R.layout.simple_list_item_1, getTabsStrings(mListTabEntries)));
// Create a for loop to iterate each entry in your list, so you dont have to do it
// manually
for (TabEntry entry : mListTabEntries) {
th.addTab(th.newTabSpec(entry.getTabTitle())
.setIndicator(entry.getIndictor())
.setContent(entry.getContent());
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TabEntry entry = list1strings.get(position);
Intent ourIntent = new Intent(TabView.this, entry.getTabClass());
startActivity(ourIntent);
}
private String[] getTabsStrings(List<TabEntry> mEntries) {
String[] titlesString = new String[mEntries.size()];
for(int i = 0; i < mEntries.length(); i++) {
titlesString[i] = mEntries.get(i);
}
return titlesString;
}
}
At this point this code should work (but IMHO it not the prettiest one). So we can do better (if you want learn more about code, you can read Clean Code or Effective Java, both books are great to improve your skills with programming).
If you want to learn more about how we can make this code more pretty and succinct let me know! :)
I am making an application for the Android platform in Eclipse, and I need help with something. :)
What I want for one part to do is create an arraylist from items that are checked in another ListView. I figured out how to make that and here is the code:
public class MusicList extends Activity {
private ListView lvCheckBox;
private String[] arr = { "Depeche Mode", "The Prodigy", "Rammstein",
"Manilla Road", "Led Zeppelin", "AC/DC", "Massive Attack",
"Skrillex", "Deadmau5" };
ArrayList<String> arrList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.musiclist);
lvCheckBox = (ListView) findViewById(R.id.lvCheckBox);
lvCheckBox.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lvCheckBox.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, arr));
arrList = new ArrayList<String>();
lvCheckBox.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(arrList.contains(lvCheckBox.getItemAtPosition(arg2).toString()))
{
arrList.remove(lvCheckBox.getItemAtPosition(arg2).toString());
}
else
{
arrList.add(lvCheckBox.getItemAtPosition(arg2).toString());
}
Collections.sort(arrList);
String strText = "";
for(int i=0 ; i<arrList.size(); i++)
strText += arrList.get(i) + ",";
Toast.makeText(MusicList.this, "Item Clicked: "+ strText, Toast.LENGTH_SHORT).show();
}
});
}
}
BUT!
Now I need to save that arraylist (like some "user settings" (actually the music which the user likes)) even when the app closes completely and when I come back to the list screen previously checked items need to be checked again (bear in mind I will be adding a lot more musicians to the starting list!!). So, anyone knows how to do that? :)
That's all, thanks in advance, and also I am new here so sorry if I messed something up :(
Android has a SharedPreferences class that will allow you to save information to the App's "cache".
So, you'd be able to save and retrieve the list information.
http://developer.android.com/reference/android/content/SharedPreferences.html