我试图从另一个类中获取一个字符串,并在MainActivity中使用它。该字符串在OnItemClickListener内部的OnClickListener中初始化。 我搜索了一些解决方案,但是使用Intent的方法在这里不起作用,因为我没有在活动之间进行切换,而是希望MainActivity中的String。我尝试使用SharedPreferences的方法,但是没有用。 这是带有字符串的类的代码
TimeForm
public class TimeForm extends AppCompatActivity{
public ArrayList<String> arrayList;
ListView dayListView;
ArrayAdapter<String> arrayAdapter;
private static List<String> fromArrayList;
ArrayList<String> untilArrayList;
public String currentDay;
EditText fromEditText;
EditText untilEditText;
EditText multiLineEditText;
ListView itemsListView;
Button submitButton;
EditText actionEditText;
private FirebaseFirestore db;
private static final String TAG = "TimeForm";
public String CurrentDay;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time_form);
//Variables==============================================================
dayListView = (ListView)findViewById(R.id.dayListView);
arrayList = new ArrayList<String>();
final TextView dayTextView = (TextView)findViewById(R.id.dayTextView);
submitButton = (Button)findViewById(R.id.submitButton);
fromEditText = (EditText)findViewById(R.id.fromEditText);
multiLineEditText = (EditText)findViewById(R.id.actionEditText);
untilEditText = (EditText)findViewById(R.id.untilEditText);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
fromArrayList = new ArrayList<String>();
untilArrayList = new ArrayList<String>();
final ArrayAdapter fromArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, fromArrayList);
ArrayAdapter untilArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, untilArrayList);
itemsListView = (ListView)findViewById(R.id.itemsListView);
actionEditText = (EditText)findViewById(R.id.actionEditText);
////////////////////////////////////////////////////////////////////////////////
arrayList.add("Monday");
arrayList.add("Tuesday");
arrayList.add("Wednesday");
arrayList.add("Thursday");
arrayList.add("Friday");
arrayList.add("Saturday");
arrayList.add("Sunday");
final GlobalClass globalClass = (GlobalClass) this.getApplicationContext();
dayListView.setAdapter(arrayAdapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
dayListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
TextView enterActionTextView = (TextView)findViewById(R.id.enterActionTextView);
EditText actionEditText = (EditText)findViewById(R.id.actionEditText);
currentDay = arrayList.get(position);
TextView enterTimeTextView = (TextView)findViewById(R.id.enterActionTextView);
dayTextView.setText(currentDay);
submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String fromText = fromEditText.getText().toString();
final String untilText = untilEditText.getText().toString();
final String actionText = multiLineEditText.getText().toString();
if(fromText.matches("") || untilText.matches("") || actionText.matches("")){
Toast.makeText(getApplicationContext(), "Please fill the whole Form", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(TimeForm.this, "Saved", Toast.LENGTH_SHORT).show();
globalClass.setTestString(actionText);
fromEditText.setText("");
untilEditText.setText("");
multiLineEditText.setText("");
}
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.nevigation_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent reportBugIntent = new Intent(TimeForm.this, com.anastasispap.schedulemaker.ReportBug.class);
Intent mainActivityIntent = new Intent(TimeForm.this, MainActivity.class);
switch (item.getItemId()){
case R.id.action_file:
startActivity(timeFormIntent);
}
if(item.getItemId() == R.id.action_reportbug){
startActivity(reportBugIntent);
}else if(item.getItemId() == R.id.action_home){
startActivity(mainActivityIntent);
}
return super.onOptionsItemSelected(item);
}
}
我要获取的字符串是fromText,toilText和actionText。 我希望这些字符串在MainActivity中获得它们。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView itemsListView = (ListView) findViewById(R.id.itemsListView);
final TextView itemsTextView = (TextView)findViewById(R.id.itemsTextView);
final ArrayList<String> itemsArrayList = new ArrayList<String>();
final TextView dayTextView = (TextView)findViewById(R.id.dayTextView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemsArrayList);
Date currentTime = Calendar.getInstance().getTime();
dayTextView.setText(new SimpleDateFormat("EEEE MM yyyy", Locale.ENGLISH).format(currentTime.getTime()));
Button button = (Button)findViewById(R.id.button2);
final GlobalClass globalClass = (GlobalClass) this.getApplicationContext();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, globalClass.getTestString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nevigation_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent timeFormIntent = new Intent(MainActivity.this, TimeForm.class);
Intent reportBugIntent = new Intent(MainActivity.this, ReportBug.class);
Intent mainActivityIntent = new Intent(MainActivity.this, MainActivity.class);
switch (item.getItemId()){
case R.id.action_file:
startActivity(timeFormIntent);
}
if(item.getItemId() == R.id.action_reportbug){
startActivity(reportBugIntent);
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
您可以尝试为包含字符串的主要活动创建接口。
public interface IMainActivity {
void fromText(String fromText);
void untilText(String untilText);
void actionText(String actionText);
}
然后将实现的实例传递到TimeForm中(在这里Dagger会很好)。
答案 1 :(得分:-1)
您可以使用全局类来存储字符串。并在任何地方和您的代码中使用该字符串。您可以使用getter和setter来访问它。
public class GlobalClass extends Application {
private String yourString;
public String getYourString() {
return yourString;
}
public void setYourString(String yourString) {
this.yourString = yourString;
}
}
在清单文件的应用程序标记中定义
<application
android:name=".GlobalClass"
并在您使用的活动的onCreate
功能中像这样使用它。
globalClass = (GlobalClass) this.getApplicationContext();
如果不是活动,(使用参数传递活动的上下文)
globalClass = (GlobalClass) context.getApplicationContext();
然后,您可以使用getter和setter来访问您的字符串。 设置值,
String fromString = "some value";
globalClass.setYourString(fromString)
获取值
String fromString = globalClass.getYourString();
您可以使用此GlobalClass
存储所有字符串以及您需要从多个activities
和classes
访问的所有其他值