为什么我无法填充listView并返回主菜单

时间:2018-02-08 18:06:17

标签: android android-studio

import static com.example.adimov.learneveryday.MainActivity.messagebox;  

public class NewMessage extends AppCompatActivity implements OnClickListener {

Button save;
Button cancel;
EditText message;
String currentmessage;

ArrayList<String> ListFeeder = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newmessage);

    save = (Button)findViewById(R.id.save);
    cancel = (Button)findViewById(R.id.cancel);
    message = (EditText)findViewById(R.id.message);

    message.requestFocus();                                                                         // Tezi dva reda mi vikat klaviaturata avtomatiqno kogato natisna butona za nova belejka
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

}

@Override
public void onClick(View v){

    if(v == save){
        currentmessage = message.getText().toString().trim();
        ListFeeder.add(currentmessage);
        messagebox.setAdapter((ListAdapter) ListFeeder);
        goToMainMenu();
    }
}
public void goToMainMenu() {
    Intent intent = new Intent (this, MainActivity.class);
    startActivity(intent);
}

最后7行代码是两个问题都发生的地方,首先我不能将EditText中的文本添加到ListView,然后它不会返回主菜单,这两个问题彼此独立,我查了一下。

2 个答案:

答案 0 :(得分:0)

你需要在onCreate()中执行类似view.setOnClickListener(this)的操作,实现OnClickListener但是你没有在视图上设置监听器。 添加:save.setOnClickListener(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newmessage);

    save = (Button)findViewById(R.id.save);
    cancel = (Button)findViewById(R.id.cancel);
    message = (EditText)findViewById(R.id.message);

save.setOnClickListener(this);// add this

    message.requestFocus();                                                                         // Tezi dva reda mi vikat klaviaturata avtomatiqno kogato natisna butona za nova belejka
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

}

答案 1 :(得分:0)

 @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newmessage);

save = (Button)findViewById(R.id.save);
cancel = (Button)findViewById(R.id.cancel);
message = (EditText)findViewById(R.id.message);
 save.setOnClickListener(this);// add this
}   
 @Override
 public void onClick(View v){
 if(v.getId() == R.id.save){ //Here is another correction
    currentmessage = message.getText().toString().trim();
    ListFeeder.add(currentmessage);
    messagebox.setAdapter((ListAdapter) ListFeeder);
    goToMainMenu();
 }
}