如何在循环中提示用户输入文本?

时间:2013-03-10 14:37:19

标签: android android-intent

我正在编写一个Android应用程序,主要活动开始并填充联系人列表,并需要提示用户今天所有联系人的评级(promptUserForInput)并立即处理所有联系人的收到评级。我想我可以使用一个提示每个联系人的对话框,并获得用户的评价。但是代码失败,因为主线程没有等待用户完成所有用户的输入评级。

这是我的函数,我在所有联系人姓名的do while循环中的主要活动中调用。评级是一个全局变量。

double rating=0;
private synchronized void promptUserForInput(String firstName, String lastName) {

    final String fname = firstName;
    final String lName = lastName;

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    String custName = firstName + " " + lastName;
    final EditText input = new EditText(this);
    alert.setTitle(custName);
    alert.setView(input);
    Log.v("Diva: in promptUserForInput", "setting positive buton");
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            Editable res = input.getText();
            if(res == null) {
                Log.v("Diva..", "In positivebutton..befoer getting rating res is null");
            }
            rating = Double.valueOf(input.getText().toString());
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            rating=0;
        }
    });

    alert.show();        

}

promptUserForInput()的来电者如下所示。

// get list of contacts in a cursor
Cursor cursor = ManageDataBaseActivity.queryDataBase(this,     
ManageDataBaseActivity.CONTACT_INFO_TABLE);

if(cursor.getCount()>0) {

    double totalRatingForStats=0;
    cursor.moveToFirst();
    do {
        String[] colNames = cursor.getColumnNames();
        Log.v("Diva Colum names = ", colNames[0] + " " + colNames[1] + " " + colNames[2] + " " + colNames[3]);

        String firstName = cursor.getString(cursor.getColumnIndex("FirstName"));

        Log.v("Diva ..:", firstName);
        String lastName = cursor.getString(cursor.getColumnIndex("LastName"));
        String key = ManageDataBaseActivity.getDbKey(firstName, lastName, 
                                    date, ManageDataBaseActivity.CUSTOMER_DATA_TABLE);
        promptUserForInput(firstName, lastName);
        double ratingReceived = rating;

        totalRatingForStats = totalRatingForStats+ratingReceived;
        // some more processing

                        ManageDataBaseActivity.insertValueToDB(ManageDataBaseActivity.
                                CONTACT_DATA_TABLE+" ", .....);
    } while(cursor.moveToNext());           

2 个答案:

答案 0 :(得分:1)

答案简短:不要。

答案很长:在等待用户输入时,绝不应该阻止GUI程序的主线程。 相反,您应该提供一个继续按钮,该按钮会触发导致程序继续的事件。有几种方法可以实现这一点,首先想到的是信号和信号量。

我不是那么精通Android编程 - 但是在API中应该有类似的东西,可能依赖于Intents。

答案 1 :(得分:1)

在Activity的主线程中循环通常不是一个好主意。但是你可以实现像pollNext()方法那样从光标获取下一个数据集并将你的点击方法更改为:

@Override
public void onClick(DialogInterface dialog, int which) {
    // do your rating stuff

    // reads the next dataset
    pollNext();

    // shows the next dialog
    // of course, firstName and lastName must be membervariables to make this work
    promptUserForInput(firstName, lastName); 
}

背后的想法很常见,也在MVC-pattern

中使用
相关问题