gpt4 book ai didi

java - 嗨,我想弄清楚如何将字符串从一个类传递到另一个类

转载 作者:行者123 更新时间:2023-12-02 03:42:04 25 4
gpt4 key购买 nike

对所有代码感到抱歉。我试图传递的字符串是内容。如果您需要更多信息,请告诉我

我是java新手,所以我可能会犯一些愚蠢的错误。我尝试过使用不同的方法来传递字符串,但没有成功。如果我能够让字符串出现在编辑文本框中,那就太好了。

我是这个网站的新手;抱歉,如果我犯了菜鸟错误。

主类:

package com.app.david.booktracker; 


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

目标类别:

package com.app.david.booktracker;

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

}

最佳答案

如果希望能够访问类外部的属性,首选方法是将它们初始化为私有(private)属性并具有“getter”方法。像这样的简单方法:

public static String getContents () {
return contents;
}

会起作用的。但是,您必须将变量初始化为类的属性,如下所示:

private static String contents;

它必须在类中的任何函数之外声明(即在public static class MainActivityextents AppCombatActivity {之后)。不要忘记将最初初始化它的行从 Stringcontents 更改为 contents

不过,我不禁注意到,您正在 NewBook 类中初始化一个同名的字符串。这是您尝试插入原始内容的地方吗?如果是这样,只要您已完成上述所有操作,您所需要做的就是调用 MainActivity.getContents(),它应该返回您正在查找的字符串。

关于java - 嗨,我想弄清楚如何将字符串从一个类传递到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36728266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com