【问题标题】:Why aren't my results being added the first time?为什么我的结果不是第一次添加?
【发布时间】:2016-02-01 21:51:38
【问题描述】:

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

A "Add Bookmark" option is listed in the app menu, and when selected it calls 3 "show dialog" functions which prompt the user to fill 3 string variables (title, url, and 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);
}
}

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    我还建议将其设为一个对话框。三个对话框可能很烦人。如果用户在第一个或第二个对话框中取消会发生什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 2013-09-20
      • 2020-09-26
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多