java - 从 Intent 中检索信息,然后将其设置到 Fragment 内的 TextView - Android

标签 java android android-fragments android-intent

我有以下代码,其中我尝试使用 Intent 检索从我的主要 Activity 发送的数据,并且在下一个 Activity 中我正在扩展一个 fragment 类(这很重要,因为我正在使用第三方库来合并动画图表)。我遇到的问题是我的应用程序运行时没有任何错误,但信息未显示在文本框中。扩展 fragment 类时“SetText”是否有不同的语法?

我创建线路 Activity 的代码( fragment 不允许完整代码:

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


            }
        });
    }

}

我的接收线路详细信息中的代码:

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;

}

最佳答案

如果您有多个值,请使用 bundle 尝试此技术:

发送 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);

在 fragment 中获取 Intent :

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

用你的替换我的变量!

关于java - 从 Intent 中检索信息,然后将其设置到 Fragment 内的 TextView - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46704403/

相关文章:

java - 为什么相机在 Android 5 中无法使用?

android - 如何只在特定的 API 级别上执行代码

java - 编辑; Android 应用程序 SQLite 异常不同的模拟器

java - Eclipse ADT 自动为 Activity 创建 fragment 。为什么?

android - 更改 View 寻呼机高度以将内容与 fragment 的内容高度一起换行

java - 如何使用 java.util.Date(的值)设置 java.time.LocalDate?

java - 使用 Java 反射动态创建 JSF 表单

java - 远程调试 Java - Solr

android - 我无法通过 MediaPlayer Android 从 URL 播放音频

Android - getTargetFragment 和 setTargetFragment - 它们的用途是什么