Sorry für den gesamten Code. Die Zeichenfolge, die ich versuche zu übergeben, ist Inhalt. Lassen Sie mich wissen, wenn Sie weitere Informationen benötigenHallo, ich versuche herauszufinden, wie man eine Zeichenfolge von einer Klasse an eine andere übergeben
Ich bin neu in Java, also mache ich wahrscheinlich dumme Fehler. Ich habe versucht, verschiedene Methoden zu machen, um die Schnur ohne Glück zu übergeben. Wenn ich die Zeichenfolge in einer Edittextbox erscheinen lassen könnte, wäre das großartig.
Ich bin neu auf dieser Website; Entschuldigung, wenn ich Anfängerfehler mache.
Hauptklasse:
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;
}
Zielklasse:
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);
}
}
Mögliches Duplikat von [android: Wie man einen POJO zwischen Aktivitäten teilt] (http://stackoverflow.com/questions/5272276/android-how-to-share-a-pojo-between-activities) –