繁体   English   中英

为什么不第一次添加我的结果?

[英]Why aren't my results being added the first time?

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

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

收到输入后,我希望它调用addBookmark(),它将把字符串添加到Bookmark类对象,然后通过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()方法。 不会的 该应用程序将显示每个对话框,然后立即执行addBookmark()

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

我也建议将其设为一个对话框。 三个对话框可能很烦人。 如果用户在第一个或第二个对话框中取消操作会怎样?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM