ListView不显示来自另一个活动的值传递

时间:2018-03-14 09:47:24

标签: android listview sharedpreferences

我有两项活动

第一项活动 -

  1. 用于获取editText
  2. 中的字符串值
  3. 将值传递给第二个活动
  4. 第二项活动 -

    1. 从第一项活动中获取价值
    2. 将值放在ArrayList中并在listview中显示
    3. 将值放入集合并存储在共享首选项中。
    4. 我希望这些事情发生,但我不知道我的代码存在问题。我的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();
      }
      

1 个答案:

答案 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();