嗨,我想弄清楚如何将一个字符串从一个类传递到另一个类 [英] Hi, I am trying to figure out how to pass a string from one class to another

查看:49
本文介绍了嗨,我想弄清楚如何将一个字符串从一个类传递到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起所有代码。我试图传递的字符串是内容。

如果您需要更多信息请告诉我













这是我的主要班级 -

Sorry for all the code. The string I am trying to pass is contents.
Let me know if you need anymore info






here is my main class-

package com.app.david.booktracker;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initToolBar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    public void initToolBar() {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Book Tracker");
        setSupportActionBar(toolbar);


    }

    public void newBook(View view) {
        Intent intent = new Intent(this, NewBook.class);
        startActivity(intent);
    }

    public void showListBook(View view) {
        Intent intent = new Intent(this, ShowAllBooks.class);
        startActivity(intent);
    }


    public void scanBar(View v) {
        try {
            Intent intent = new Intent(ACTION_SCAN);
            intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
            startActivityForResult(intent, 0);
        } catch (ActivityNotFoundException e) {
            showDialog(MainActivity.this, "No Scanner Found",
                    "Download a scanner code activity?", "Yes", "No").show();
        }
    }


    private static AlertDialog showDialog(final AppCompatActivity act,
                                          CharSequence title, CharSequence message, CharSequence buttonYes,
                                          CharSequence buttonNo) {

        AlertDialog.Builder dowloadDialog = new AlertDialog.Builder(act);
        dowloadDialog
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(buttonYes,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                Uri uri = Uri.parse("market://search?q=pname:"
                                        + "com.google.zxing.client.android");

                                Intent intent = new Intent(Intent.ACTION_VIEW,
                                        uri);
                                try {
                                    act.startActivity(intent);
                                } catch (ActivityNotFoundException e) {
                                }

                            }
                        })
                .setNegativeButton(buttonNo,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                            }
                        });

        return dowloadDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if(requestCode == 0) {
            if(resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Toast.makeText(this,
                        "Content:" + contents + " Format:" + format,
                        Toast.LENGTH_LONG).show();
            }
        }
    }





    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}









这里是我试图将它传递给的类 -





Here is the class i am trying to pass it to -

package com.app.david.booktracker;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.os.Bundle;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;


public class NewBook extends AppCompatActivity {

    private final static String TAG= "group4.NewBook";

    private EditText ean;

    @Override

    protected  void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_book);
        //initToolBar();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    public void initToolBar(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Book Tracker");

        setSupportActionBar(toolbar);

        toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow);
        toolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(v.getContext(), MainActivity.class);
                        startActivity(intent);
                    }
                }
        );
    }



    public void newBook(View view)
    {
        //Get the
        Intent intent=new Intent(this, MainActivity.class);
        EditText editTextBTitle=(EditText) findViewById(R.id.editTextFirstName);
        EditText editTextBAuthor=(EditText) findViewById(R.id.editTextSurname);

        String booktitle=editTextBTitle.getText().toString();
        String bookauthor=editTextBAuthor.getText().toString();

        String contents = intent.getStringExtra("SCAN_RESULT");
        DataHolder.bookadded(booktitle, bookauthor, contents);
        Log.d(TAG, " Added: " + booktitle + "  " + bookauthor + " " + contents);

        Toast.makeText(this, "Book Added! ", Toast.LENGTH_LONG).show();
        startActivity(intent);
    }


}





我的尝试:



我是java新手所以我可能犯了愚蠢的错误。我尝试过不同的方法来传递字符串而没有运气。如果我能够将字符串显示在一个很棒的edittextbox中。



What I have tried:

I am new to java so I am probably making silly mistakes. I have tried making different methods to pass the string with no luck. If I was able to get the string to appear in an edittextbox that would be great.

推荐答案

如果不详细检查所有代码,您似乎正在尝试把它从一个意图转移到另一个意图。在您的主类中,您有以下行:

Without going through all your code in detail it would appear that you are trying to pass it from one intent to another. In your main class you have the line:
String contents = intent.getStringExtra("SCAN_RESULT");



BuT我看不到相应的 putExtra 将数据放入其中的调用。我假设你应该在main中做什么,然后NewBook活动可以使用 getStringExtra 来读取值。请参阅 Intent | Android开发者 [ ^ ]。


BuT I cannot see the corresponding putExtra call that puts the data in there. I assume that is what you should be doing in main and then the NewBook activity can use getStringExtra to read the value. See Intent | Android Developers[^].


这篇关于嗨,我想弄清楚如何将一个字符串从一个类传递到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆