1

I have the following code in which i am trying to retrieve the data that i have sent from my main activity using intents and on my next activity i am extending a fragment class (which is essential as i am using a third party library to encorporate animated charts). The problem that i am having is that my app is running without any errors, however the information is not displaying in the textboxes. Is there different syntax to "SetText" when extending a fragment class?

The code that i have on my creating a line activity(Snippet as does not allow full code:

  dateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                dateButton.setText(date.toString());

                // Creates an instance of current DateTime which represents the
                // current date time.
                DateTime dateTime = new DateTime();
                DateTimeFormatter fmt = DateTimeFormat.forPattern("E d MMM yyyy" + "\n" + " h:mm a ");
                String formattedtime = fmt.print(dateTime);
                dateButton.setText(formattedtime);

                // Plus some hours, minutes, and seconds to the original DateTime.
                DateTimeFormatter fmt2 = DateTimeFormat.forPattern("E d MMM yyyy" + "\n" + " h:mm a ");

                DateTime dateTime1 = dateTime.plusHours(timeadded);
                String endtimecalc = fmt2.print(dateTime1);
                TextView endtime = findViewById(endtimetextView);
                endtime.setText(endtimecalc);

                String spinnerSelection = String.valueOf(spinner.getSelectedItem());
                String spinnerSelection2 = String.valueOf(spinner2.getSelectedItem());
                String q = quantity.getText().toString();
                String d = duration.getText().toString();

                //INSERT DATA TO DATABASE
                boolean isInserted = myDb.insertData(
                        spinnerSelection,
                        spinnerSelection2,
                        q,
                        d,
                        formattedtime,
                        endtimecalc);

                if (isInserted == true)
                    Toast.makeText(CreateLine.this, "Data Inserted Successfully", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(CreateLine.this, "Data not Inserted", Toast.LENGTH_LONG).show();

            }
        });


        nextButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                    Cursor res = myDb.getAllData();

                    StringBuffer buffer0 = new StringBuffer();
                    StringBuffer buffer1 = new StringBuffer();
                    StringBuffer buffer2 = new StringBuffer();
                    StringBuffer buffer3 = new StringBuffer();
                    StringBuffer buffer4 = new StringBuffer();
                    StringBuffer buffer5 = new StringBuffer();
                    StringBuffer buffer6 = new StringBuffer();


                    if ( res != null && res.moveToFirst()) {
                        do {

                            buffer0.setLength(0);
                            buffer1.setLength(0);
                            buffer2.setLength(0);
                            buffer3.setLength(0);
                            buffer4.setLength(0);
                            buffer5.setLength(0);
                            buffer6.setLength(0);

                            String getid = res.getString(0);
                            String getlt = res.getString(1);
                            String getpt = res.getString(2);
                            String getqty = res.getString(3);
                            String getdur = res.getString(4);
                            String getst = res.getString(5);
                            String getet = res.getString(6);

                            buffer0.append(getid);
                            buffer1.append(getlt);
                            buffer2.append(getpt);
                            buffer3.append(getqty);
                            buffer4.append(getdur);
                            buffer5.append(getst);
                            buffer6.append(getet);
                        } while (res.moveToNext());
                    }


                Intent TransferData = new Intent(CreateLine.this, LineDetails.class);
                                    Bundle extras = new Bundle();


                extras.putString("ID", buffer0.toString());
                extras.putString("LineType", buffer1.toString());
                extras.putString("PackageType", buffer2.toString());
                extras.putString("Quantity", buffer3.toString());
                extras.putString("Duration", buffer4.toString());
                extras.putString("Starttime",buffer5.toString());
                extras.putString("endtime", buffer6.toString());
                TransferData.putExtras(extras);


                setContentView(R.layout.line_details);
                SamplerAdapter samplesAdapter = new SamplerAdapter(getSupportFragmentManager());
                ViewPager samplesPager = (ViewPager) findViewById(R.id.samplesPager);
                samplesPager.setAdapter(samplesAdapter);


            }
        });
    }

}

Code that i have on my receiving line details:

public class LineDetails extends SampleFragment {
final private float[] mTrackBackWidth = {30f, 60f, 30f, 40f, 30f};
final private float[] mTrackWidth = {30f, 30f, 30f, 30f, 30f};
final private boolean[] mClockwise = {true, true, true, false, true};
final private boolean[] mRounded = {true, true, true, true, true};
final private boolean[] mPie = {false, false, false, false, true};
final private int[] mTotalAngle = {360, 360, 320, 260, 360};
final private int[] mRotateAngle = {0, 180, 180, 0, 270};
private int mBackIndex;
private int mSeries1Index;
private int mSeries2Index;
private int mStyleIndex;

SQLiteDatabase db;
DatabaseHelper databaseHelper;
Cursor cursor;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View myInflatedView = inflater.inflate(R.layout.line_details, container, false);

    Intent ReceiveData = getActivity().getIntent();
    Bundle extras = ReceiveData.getExtras();

    String id = extras.getString("ID");
    String linetype = extras.getString("LineType");
    String packagetype = extras.getString("PackageType");
    String Quantity = extras.getString("Quantity");
    String Duration = extras.getString("Duration");
    String Starttime = extras.getString("Starttime");
    String endtime = extras.getString("endtime");

    TextView LT = (TextView)    myInflatedView.findViewById(R.id.textViewLT);
    TextView PT = (TextView)    myInflatedView.findViewById(R.id.textViewPT);
    TextView QTY = (TextView)   myInflatedView.findViewById(R.id.textViewQTY);
    TextView DUR = (TextView)   myInflatedView.findViewById(R.id.textViewDUR);
    TextView ST = (TextView)    myInflatedView.findViewById(R.id.textViewST);
    TextView ET = (TextView)     myInflatedView.findViewById(R.id.textViewET);

    LT.setText(linetype);
    PT.setText(packagetype);
    QTY.setText(Quantity);
    DUR.setText(Duration);
    ST.setText(Starttime);
    ET.setText(endtime);

    return myInflatedView;

}
Fionnuala
  • 77
  • 1
  • 10
  • How can an activity extend fragment?It should be fragment. – Sharath kumar Oct 12 '17 at 08:04
  • im not sure i thought that the extends bit means that it is using objects from another class that i have. Unfortuantely if i change the extends to AppCompatActivity the rest of the code doesnt work but the retrieval of the information does. I know that you cant extend two things as well. So what do you suggest? – Fionnuala Oct 12 '17 at 08:08
  • You cannot use Intent TransferData = new Intent(getBaseContext(), LineDetails.class); if it is fragment – Sharath kumar Oct 12 '17 at 08:09
  • is there another way i can still send that data across and retrieve it? – Fionnuala Oct 12 '17 at 08:10
  • Yes check this https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Sharath kumar Oct 12 '17 at 08:11
  • it doesnt seem that that is working either. i have updated my code above could you take a look and tell me what you think? – Fionnuala Oct 12 '17 at 10:33

2 Answers2

0

If you have multiple values then try this technique using Bundles:

Sending Intent :

    Intent intent = new Intent(MainActivity.this, Report_Fragment.class);
                    Bundle extras = new Bundle();

                    extras.putFloat("bmi",mybmi);
                    extras.putFloat("fats",myfat);
                    extras.putString("weight",myfinalweight);
                    extras.putString("coments",coments);
                    intent.putExtras(extras);
                    startActivity(intent);

Getting Intent in fragment :

  Intent intent = getActivity().getIntent();
    Bundle extras = intent.getExtras();
    Float bmi = extras.getFloat("bmi");
    Float fats = extras.getFloat("fats");
    String weight=extras.getString("weight");
    String coments=extras.getString("coments");

Replace my variables with yours!

Uzair Qaiser
  • 156
  • 1
  • 11
  • I have tried to use this method and my app is now crashing when i try to get to the next page. i have added the error stack trace above – Fionnuala Oct 12 '17 at 09:19
  • It seems to be a null pointer exception i can update my code if you need to see it? – Fionnuala Oct 12 '17 at 10:04
  • Fionnual did you try to debug your code in fragment?? – Uzair Qaiser Oct 12 '17 at 13:47
  • First try to debug your code in getstring that whether you are getting values or not then second step is check dat why values are not setting in the text view. let me know when you are done. I m here! – Uzair Qaiser Oct 12 '17 at 13:51
  • Okay @Uzair Qaiser, i have attached above in my initial post what happened when i debugged and set a breakpoint at the getstring point.. it seems the extras is null??? – Fionnuala Oct 12 '17 at 14:30
  • Ok. Now try debug On Put String Side and tell me! – Uzair Qaiser Oct 12 '17 at 14:31
  • and I want your code of getting value also, that from where your are getting text? in edit text. Please edit your question with complete both class and fragment code. – Uzair Qaiser Oct 12 '17 at 14:33
  • Okay i will completely add both codes and edit my initial post. give me a couple of mins! i just debugged and set breakpoint at.putstring(ID) and it is successfully adding the ID number to the extras – Fionnuala Oct 12 '17 at 14:35
  • There you go @Uzsir Qaiser – Fionnuala Oct 12 '17 at 14:41
  • Ok fionnuala let me check. – Uzair Qaiser Oct 12 '17 at 14:52
  • fionnuala What is sample fragment? isnt it just Fragment? Because i never seen sample fragment before. dats whu i am asking. – Uzair Qaiser Oct 12 '17 at 15:01
  • SampleFragment is a java class that i have and i need to extend that because i am using a java library that allows me to use animated charts and i need to use some of the methods from that class. unfortunately, it the charts dont show on screen without this extension – Fionnuala Oct 12 '17 at 15:07
  • However, i do know that the data retrieval does work and displays with AppCompatActivity extended so it is unfortunate that i must extend SampleFragment or the charts dont show – Fionnuala Oct 12 '17 at 15:08
  • and dat Class extends to fragment? – Uzair Qaiser Oct 12 '17 at 15:08
  • Isnt its possible that You may create functions in your SampleFragment class dat should return something in your LineDetails class? then we can extends it AppCompactActivity. – Uzair Qaiser Oct 12 '17 at 15:11
  • Yes that would be easier. its just that whenever i use a method for the chart say .setdemension and i have extended AppcompatActivity it doesnt recognise it because SampleFragment is not extended. So, how would i go about this? – Fionnuala Oct 12 '17 at 15:13
  • Ok. First tell me dat did you try below code like this: – Uzair Qaiser Oct 12 '17 at 15:18
  • https://stackoverflow.com/questions/15392261/android-pass-dataextras-to-a-fragment – Uzair Qaiser Oct 12 '17 at 15:19
  • Actually If your method returning something dat we can store it in variable then you dont need to extend your sample fragment with fragment. because its just class. Then in your getstring class, you can simpily make object of sample fragment and store the value in any stringdat can be set into text view. – Uzair Qaiser Oct 12 '17 at 15:24
  • but the return of the method from sample fragment is not what i am looking to store or retrieve it is just needed so that there is no errors – Fionnuala Oct 12 '17 at 15:26
  • Ok did you try above link? Its related to your problem. – Uzair Qaiser Oct 12 '17 at 15:28
  • where R.id.content is the view id in xml – Uzair Qaiser Oct 12 '17 at 15:30
  • going to try now although it is different from me as i am passing multiple values he is passing just one. Also i debugged and sending the data is working as the bundle is being populated with all the correct info. it is receiving and displaying that is the problem – Fionnuala Oct 12 '17 at 15:31
  • I know i know Fionnuala, But you can pass multiple values also, because its bundle, secondly may be, its the right thing to try. i just answer that question because i was also getting receiving, and setting in text. but in your case may be the problem is the fragement. dats why i am trying to help you from different methods! – Uzair Qaiser Oct 12 '17 at 15:35
  • okay i will give that a go and let you know how it goes, thanks for this! – Fionnuala Oct 12 '17 at 15:37
  • You welcome. I m here! tell me when you done! and is its 1 value then try coment your code and just try to send 1 value first time. – Uzair Qaiser Oct 12 '17 at 15:38
  • is it the first answer or second answer im following? because the second one is using the bundle and not the first – Fionnuala Oct 12 '17 at 15:43
  • its the first i think – Uzair Qaiser Oct 12 '17 at 15:56
  • they are also passing from fragment to fragment not activity to fragment – Fionnuala Oct 12 '17 at 15:57
  • Can you send me your project? – Uzair Qaiser Oct 12 '17 at 15:59
  • how can i do that? you sure? that would be great help – Fionnuala Oct 12 '17 at 16:02
  • Uzair_qaiser@outlook.com (My email id) Email your project on this id. If your project is more than 25mb then first upload it on google drive and send me the link for downloading. – Uzair Qaiser Oct 12 '17 at 16:04
  • For further details and contact. text me on this email address. – Uzair Qaiser Oct 12 '17 at 16:06
  • Not sure if i can do this as i am on work placement for a company and all their apps must be kept internal.. – Fionnuala Oct 12 '17 at 16:09
  • I can send you a few of the classes you need and the xml? – Fionnuala Oct 12 '17 at 16:26
  • m.m.m I m really sory. I can only say that its the only way i may help you.But you can contact me any time with the help of this email. :) have a good day. – Uzair Qaiser Oct 12 '17 at 16:27
  • talk to me at my email. because coment conversation is exceeding! – Uzair Qaiser Oct 12 '17 at 16:28
  • Ok Fionnuala Stay blessed – Uzair Qaiser Oct 12 '17 at 16:35
0

try it

for sending

    Fragment fragment=new LineDetails();
    Bundle extras = new Bundle();

    extras.putFloat("bmi",mybmi);
    extras.putFloat("fats",myfat);
    extras.putString("weight",myfinalweight);
    extras.putString("coments",coments);
    fragment.setArguments(extras);
    getSupportFragmentManager().beginTransaction().add(R.id.frame_container,fragment).commit();

for getting,

Float bmi = getArguments().getFloat("bmi");
Float fats = getArguments().getFloat("fats");
String weight=getArguments().getString("weight");
String coments=getArguments().getString("coments");
  • what would be my equivalent to your "R.id.frame_container"? Android studio is not recognising when i type that. – Fionnuala Oct 12 '17 at 13:09
  • Also from the code above it is running without any errors, although it seems that the "SetText" just isnt applying the strings and therefore the data is not outputting? – Fionnuala Oct 12 '17 at 13:17