简体   繁体   中英

Retrieving Info from an Intent, then Setting it to a textview inside a Fragment - Android

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;

}

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!

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM