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

时间:2016-02-01 21:51:38

标签: java android inner-classes listactivity anonymous-class

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

A"添加书签"选项列在应用程序菜单中,选中后会调用3"显示对话框"提示用户填写3个字符串变量(title,url和note)的函数。

接收输入后,我希望它调用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);
}
}

1 个答案:

答案 0 :(得分:0)

我相信发生的事情是你的opOptionsItemSelected()方法执行这些代码行:

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

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

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

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