我有两项活动
第一项活动 -
第二项活动 -
我希望这些事情发生,但我不知道我的代码存在问题。我的ListView不显示任何内容。
这是我的代码:
第一项活动
public class History extends AppCompatActivity {
Button translate;
Button next;
EditText enterText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
enterText = findViewById(R.id.editText);
next = findViewById(R.id.btnNext);
translate = findViewById(R.id.buttonTranslate);
View.OnClickListener addlistener = new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(enterText.getWindowToken(), 0);
if(enterText != null)
{
String input = enterText.getText().toString();
Intent i= new Intent(History.this,MainActivity.class);
i.putExtra("myHistory",input);
enterText.setText("");
}else
{
Toast.makeText(getBaseContext(),"Input Field is empty",Toast.LENGTH_SHORT).show();
}
}
};
translate.setOnClickListener(addlistener);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(History.this,MainActivity.class);
startActivity(intent);
}
});
第二项活动
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> toDelete;
ArrayList<String> itemList = new ArrayList<String>();
SharedPreferences.Editor editor ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemList);
toDelete = new ArrayList<>();
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
final SharedPreferences transHistory = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
final Set<String> tHistory = new HashSet<String>();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("myHistory");
itemList.add(value);
tHistory.addAll(itemList);
}
editor = transHistory.edit();
editor.putStringSet("history",tHistory);
editor.apply();
adapter.notifyDataSetChanged();
}
答案 0 :(得分:0)
你做了语法错误: -
String input = enterText.getText().toString();
Intent i= new Intent(History.this,MainActivity.class);
i.putExtra("myHistory",input);
enterText.setText("");
这样做: -
String input = enterText.getText().toString();
Intent i= new Intent(History.this,MainActivity.class);
i.putExtra("myHistory",input);
enterText.setText("");
startActivity(i);
并在第二个活动中执行此操作: -
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> toDelete;
ArrayList<String> itemList = new ArrayList<String>();
SharedPreferences.Editor editor ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("myHistory");
itemList.add(value);
tHistory.addAll(itemList);
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemList);
toDelete = new ArrayList<>();
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
final SharedPreferences transHistory = getSharedPreferences("SaveHistory",Context.MODE_PRIVATE);
final Set<String> tHistory = new HashSet<String>();
editor = transHistory.edit();
editor.putStringSet("history",tHistory);
editor.apply();
adapter.notifyDataSetChanged();