Quantcast

Jump to content


Photo

Anyone familiar with android/java programming?

android java

  • Please log in to reply
8 replies to this topic

#1 Jakerz

Jakerz
  • 1764 posts

Posted 02 August 2016 - 03:22 AM

Hey there been a while! I have some spare time on my hands so decided to take up android programming as a hobby. I took a course and got most of the way through then decided to try to build an app for me and a few friends to use. I've hit a problem though.

 

My app has an expandable list view, and each item brings the user to the same page. That page has a tablayout with 3 fragments, Monday, Wed, Friday. Each fragment has a recycler view, and depending on what item was clicked, the data will change in the three recycler views. I have it in working order, but the data is all set in each fragments java class. I'm not sure how I can get it to set depending on what was clicked in the expandable list view. I have a feeling the solution is to use intent, but I have not been successfull so far. I even tried to implement Serializable, as I am trying to send an arraylist of objects, but that didn't work either. I have posted multiple questions on StackExchange to no avail.

 

Any help greatly appreciated. If you need to see code I will try and post it on here, not sure if theres a way to post code as on StackExchange though. Maybe I'll make a question on StackExchange and post the code then just link there.. thanks again!

 

Here is a link to the last question I posted on StackExchange, I've made a few changes in my attempt to get it to work, but they didn't work anyway.. thanks..

 

http://stackoverflow...776226_38679941



#2 Strategist

Strategist
  • Sadmin

  • 10012 posts


Users Awards

Posted 02 August 2016 - 03:32 AM

@sLAUGHTER knows a thing or two about android apps I think?

#3 ShadowLink64

ShadowLink64
  • 16735 posts


Users Awards

Posted 03 August 2016 - 07:52 PM

Been doing quite a bit of Android programming lately so hopefully I can help.

 

I think you might have missed their comment on StackOverflow explaining how to get the data from WorkoutDays activity to the actual fragment once you've created the Intent. Notice the use of the setArguments() function in their example code:

private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
MondayFragment mondayFragment = new MondayFragment();
mondayFragment.setArguments(value);
adapter.addPage(mondayFragment);
...

pager.setAdapter(adapter);
}

value when passed to the setArguments() function should be defined as follows (assuming you've already stored something in the intent):

Intent intent = getIntent();
Bundle value = new Bundle();
value.putString("value", intent.getStringExtra("value"));
mondayFragment.setArguments(value);

This passes a string argument called "value" directly to the fragment, which can then be retrieved using getArguments() within the Fragment's onCreateView() function:

String value = getArguments().getString("value")

Within onCreateView(), you can then figure out what data to display in the RecyclerView with a switch of some kind.



#4 Jakerz

Jakerz
  • 1764 posts

Posted 04 August 2016 - 10:05 AM

Thanks Shadow! Good to see you're still around after all these years! For some reason I wasn't able to send the value to the fragment as his answer indicates. I found a workaround though where I add the string to a bundle, then set the bundle as the arguments. Works good now!

Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);

MondayFragment mondayFragment = new MondayFragment();
mondayFragment.setArguments(args);
adapter.addPage(mondayFragment);

App is finally functional, though I have a feeling I'm using the wrong method to set the data to the fragment. Seems like a ton of data making each fragment class huge, probably would've been better to use sqlite but I couldn't figure out how to do what I need with that.

 

What made you start programming in android Shadow? Just as a hobby or?


I might as well ask before I do all this typing.. Will this much info crash the app? I wouldn't think so because it should only call the one case.. but it seems like there should be an easier way..

private ArrayList<Workout> getWedWorkout() {
       
        ArrayList<Workout> workout = new ArrayList<>();
        switch(workout3) {
            case "w29w1" : {
                Workout workouts = new Workout("Deadlift ^ Knee", "50", "3", dead50, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "60", "3", dead60, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "60", "3", dead60, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "70", "3", dead70, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "70", "3", dead70, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);

                workouts = new Workout("Incline Bench", "", "6", "00.0", false);
                workout.add(workouts);
                workouts = new Workout("Incline Bench", "", "6", "", false);
                workout.add(workouts);
                workouts = new Workout("Incline Bench", "", "6", "", false);
                workout.add(workouts);
                workouts = new Workout("Incline Bench", "", "6", "", false);
                workout.add(workouts);

                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);

                workouts = new Workout("Rackpull", "55", "4", dead55, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "65", "4", dead65, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "75", "4", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "75", "4", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);



                break;}

That is just one case statement, there will be a total of about 40 possible cases.. each will include about the same amount of data, some a little less, some a little more. It is going into a recycler view. Thanks! I don't mind doing it this way, as it works, I'm just worried about the app being too large or something, I wouldn't think it would be as I'm sure other apps are much larger.. but don't want to type each item out if it won't work in the end lol.. I'm also wondering if I should be putting all this data in a separate class, then just setting the arraylist in the case, rather then adding all the info there as well, or if I should leave it in the fragment class.. not sure if it would make a difference



#5 Nonexistent

Nonexistent
  • 681 posts


Users Awards

Posted 04 August 2016 - 12:47 PM

Not going to talk bout android because I really didn't enjoy android programming.

 

For the last question tho, if you are asking about whether the arraylist you are filling will exceed your heap given to your app, then that depends on the vendor of the device/os, and it will also depend of the size of your Workout class. But in most cases, it should be fine as long as your allocations are not leaking in a long running app.

workout.add(new Workout("Deadlift ^ Knee", "75", "3", dead75, false));

can make it easier to read.

 

 

idk how you store or manage data, but that looks like it could be a maintenance issue in the future.



#6 ShadowLink64

ShadowLink64
  • 16735 posts


Users Awards

Posted 04 August 2016 - 09:11 PM

Thanks Shadow! Good to see you're still around after all these years! For some reason I wasn't able to send the value to the fragment as his answer indicates. I found a workaround though where I add the string to a bundle, then set the bundle as the arguments. Works good now!

Good to see you too Jake! Yes, definitely been a long time. Haha, I used a Bundle above in the code, so I am glad that was the right approach. :p
 

What made you start programming in android Shadow? Just as a hobby or?

I am actually working as a contractor during nights/weekends (when not at the day job). Right now I am working on a project that an old work colleague of mine approached me with -- they wanted a fancy web application for an "industrial" purpose, along with an Android app for personnel out in the field (which has to sync with the server). I quoted on the project, project got accepted, and learned everything I needed to know about Android as I went. Finished the first version of the software, contract got renewed and now working on improvements and teaching their staff some stuff.
 

I might as well ask before I do all this typing.. Will this much info crash the app? I wouldn't think so because it should only call the one case.. but it seems like there should be an easier way..

private ArrayList<Workout> getWedWorkout() {
       
        ArrayList<Workout> workout = new ArrayList<>();
        switch(workout3) {
            case "w29w1" : {
                Workout workouts = new Workout("Deadlift ^ Knee", "50", "3", dead50, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "60", "3", dead60, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "60", "3", dead60, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "70", "3", dead70, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "70", "3", dead70, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Deadlift ^ Knee", "75", "3", dead75, false);
                workout.add(workouts);

                workouts = new Workout("Incline Bench", "", "6", "00.0", false);
                workout.add(workouts);
                workouts = new Workout("Incline Bench", "", "6", "", false);
                workout.add(workouts);
                workouts = new Workout("Incline Bench", "", "6", "", false);
                workout.add(workouts);
                workouts = new Workout("Incline Bench", "", "6", "", false);
                workout.add(workouts);

                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);
                workouts = new Workout("Dip", "", "5", "", false);
                workout.add(workouts);

                workouts = new Workout("Rackpull", "55", "4", dead55, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "65", "4", dead65, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "75", "4", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "75", "4", dead75, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);
                workouts = new Workout("Rackpull", "85", "3", dead85, false);
                workout.add(workouts);



                break;}
That is just one case statement, there will be a total of about 40 possible cases.. each will include about the same amount of data, some a little less, some a little more. It is going into a recycler view. Thanks! I don't mind doing it this way, as it works, I'm just worried about the app being too large or something, I wouldn't think it would be as I'm sure other apps are much larger.. but don't want to type each item out if it won't work in the end lol.. I'm also wondering if I should be putting all this data in a separate class, then just setting the arraylist in the case, rather then adding all the info there as well, or if I should leave it in the fragment class.. not sure if it would make a difference

That much data, even x40, should be totally fine. By default, Android grants an app some small heap space, but this can be enlarged if absolutely necessary.

#7 Jakerz

Jakerz
  • 1764 posts

Posted 05 August 2016 - 09:34 AM

Good to see you too Jake! Yes, definitely been a long time. Haha, I used a Bundle above in the code, so I am glad that was the right approach. :p
 
I am actually working as a contractor during nights/weekends (when not at the day job). Right now I am working on a project that an old work colleague of mine approached me with -- they wanted a fancy web application for an "industrial" purpose, along with an Android app for personnel out in the field (which has to sync with the server). I quoted on the project, project got accepted, and learned everything I needed to know about Android as I went. Finished the first version of the software, contract got renewed and now working on improvements and teaching their staff some stuff.
 
That much data, even x40, should be totally fine. By default, Android grants an app some small heap space, but this can be enlarged if absolutely necessary.

 

 

Oh yeah I must have missed that! I don't think the person on StackExchange added the bundle part.

 

That's awesome, good for you man! Ambitious lol..Very impressive that you have the confidence in yourself to bid a job, get accepted, and THEN learn what you need to know, don't know if I'd have the confidence in myself to do that haha. Any tips for learning? Do you just use StackExchange/android documentation/youtube for the most part to learn? That's basically what I'm doing so far and it seems to work okay, some things take a little longer to find tutorials or videos though.



#8 ShadowLink64

ShadowLink64
  • 16735 posts


Users Awards

Posted 05 August 2016 - 01:38 PM

Oh yeah I must have missed that! I don't think the person on StackExchange added the bundle part.

 

That's awesome, good for you man! Ambitious lol..Very impressive that you have the confidence in yourself to bid a job, get accepted, and THEN learn what you need to know, don't know if I'd have the confidence in myself to do that haha. Any tips for learning? Do you just use StackExchange/android documentation/youtube for the most part to learn? That's basically what I'm doing so far and it seems to work okay, some things take a little longer to find tutorials or videos though.

I did a bit of research beforehand to see what I was getting myself into, but it was a little bit of a leap of faith. :p

 

Android documentation and StackExchange are definitely my go-to resources. The Android doc is very well written, and Android Studio is a very nice IDE.



#9 Jakerz

Jakerz
  • 1764 posts

Posted 07 August 2016 - 10:06 AM

I did a bit of research beforehand to see what I was getting myself into, but it was a little bit of a leap of faith. :p

 

Android documentation and StackExchange are definitely my go-to resources. The Android doc is very well written, and Android Studio is a very nice IDE.

 

I guess once you know another language it's a bit easier to learn new ones too.. still impressive! 

 

Another quick question when you get a chance.. As you can see above my app has 3 recycler views and 3 fragments in a tablayout, and my expandable list view has many items, each of which brings the app to the tab layout. It's all working good, but when I turned the phone to landscape mode, or went to somewhere else in the app, then went back to where I was, all the checked items would be unchecked. I added this code to save the position and reload it when needed:

int firstSecondOrThird;

public MyRecyclerAdapter(Context context, ArrayList<Workout> workout, int thePosition) {
        mContext = context;
        this.workout = workout;
        this.firstSecondOrThird = thePosition;
    }

@Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.exercise.setText(workout.get(position).getExercise());
        holder.percent.setText(workout.get(position).getPercent());
        holder.reps.setText(workout.get(position).getReps());
        holder.weight.setText(workout.get(position).getWeight());
        holder.check1.setOnCheckedChangeListener(null);
        final Workout isCheck = workout.get(position);
        holder.check1.setChecked(isCheck.isCheck1());
        prefs = mContext.getSharedPreferences("checkstate", Context.MODE_PRIVATE);
        holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));
        isCheck.setCheck1(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false));

        holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheck.setCheck1(isChecked);
                prefs.edit().putBoolean(firstSecondOrThird+"checkState"+position, isChecked).apply();
            }
        });

    }

and then where I set the adapter to the fragments:

rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout(), 2));

This works good in terms of switching between the 3 fragments, unfortunately it saves it for every expandable list item view.. So if i click the first item in expandable list view,  check 3 items in the first tab, then go back to the expandable list view and hit the second item, those 3 items are still checked. The only way I can think to do this is use the same extra intent I pass with the expandable listview click that i use to set the data, and put a second switch case statement with it to set the adapter.. Obviously I'd have to create a second variable  and put it in the constructor as I did above.. Do you think that would work/be the best solution?

 

I would think there would be a simpler solution, just can't think of it.. I don't mind if it doesn't save the checkmarks when going somewhere else in the app, I just don't want them to reset every time I change my phone to landscape mode. Thanks





Also tagged with one or more of these keywords: android, java

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users