为什么我的结果没有在第一次添加?



我正在尝试制作一个必须使用ListActivity的应用程序,并使用用户可能添加的书签填充列表。

应用程序菜单中列出了"添加书签"选项,选择后它会调用 3 个"显示对话框"函数,提示用户填写 3 个字符串变量(标题、URL 和注释)。

收到输入后,我希望它调用addBookmark(),它将字符串添加到书签类对象,然后通过ArrayAdapter将对象添加到列表中。

截至目前,当我编译并单击菜单下的"添加书签"按钮时,该应用程序似乎乱序不通,并立即使用我用于初始化字符串的文本填充列表,并且在第二次单击按钮之前不会添加用户输入。我的预期输出是在第一次单击"添加书签"按钮时不显示任何内容,并在用户完成键入并添加后立即输出用户输入。我的代码如下

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import java.util.ArrayList;

public class BookNote extends ListActivity{
private ArrayAdapter<Bookmark> adapter;
private String title = "Example Title",url = "www.ExampleURL.com",note = "Example Note about Website";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<Bookmark> bookmarkList = new ArrayList<>();
    adapter = new ArrayAdapter<Bookmark>(this,android.R.layout.simple_list_item_1,bookmarkList);
    setListAdapter(adapter);
}
//Show dialog and editText for user to assign title to bookmark.
public void showTitleDialog(){
    AlertDialog.Builder titleBuilder = new AlertDialog.Builder(this);
    titleBuilder.setTitle("Enter Title");
    final EditText titleET = new EditText(this);
    titleET.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
    titleBuilder.setView(titleET);
    titleBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
             title = titleET.getText().toString();
        }
    });
    titleBuilder.show();
}
//Show dialog and editText for user to assign url to bookmark.
public void showURLDialog(){
    AlertDialog.Builder urlBuilder = new AlertDialog.Builder(this);
    urlBuilder.setTitle("Enter URL");
    final EditText urlET = new EditText(this);
    urlET.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
    urlBuilder.setView(urlET);
    urlBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            url = urlET.getText().toString();
        }
    });
    urlBuilder.show();
}
//Show dialog and editText for user to assign note to bookmark.
public void showNoteDialog(){
    AlertDialog.Builder noteBuilder = new AlertDialog.Builder(this);
    noteBuilder.setTitle("Enter Note");
    final EditText noteET = new EditText(this);
    noteET.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
    noteBuilder.setView(noteET);
    noteBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            note = noteET.getText().toString();
        }
    });
    noteBuilder.show();
}
//Create new bookmark object filled with user entries and add to ArrayAdapter.
private void addBookmark(){
    Bookmark bookmark = new Bookmark(title,url,note);
    adapter.add(bookmark);
}
@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_book_note, menu);
    return true;
}
@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_add) {
        showTitleDialog();
        showURLDialog();
        showNoteDialog();
        addBookmark();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
我相信

正在发生的事情是您的opOptionsItemSelected()方法正在执行以下代码行:

    //noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
    showTitleDialog();
    showURLDialog();
    showNoteDialog();
    addBookmark();
    return true;
}

您似乎希望应用程序将等到对话框关闭后再转到addBookmark()方法。 不会的。 该应用程序将显示每个对话框,然后立即执行addBookmark()

在每个

对话框中都定义了onClick侦听器。 应将addBookmark()方法放在其中一个方法中,以便在用户输入其信息执行。

我还建议把它变成一个对话。 三个对话框可能很烦人。 如果用户在第一个或第二个对话期间取消,会发生什么情况?

最新更新