2013-03-10 3 views
0

저는 주 활동이 시작되어 연락처 목록을 채우고 모든 연락처 (promptUserForInput)에 대한 현재 등급을 묻는 메시지를 표시하고 모든 연락처의받은 평가를 즉시 처리해야한다는 안드로이드 애플 리케이션을 작성하고 있습니다. 모든 대화 상대를 알리는 대화 상자를 사용할 수 있고 사용자의 평점을받을 수 있다고 생각했습니다. 그러나 주 스레드가 사용자가 모든 사용자의 등급 입력을 마칠 때까지 기다리지 않아 코드 아래에서 오류가 발생합니다.사용자가 루프에서 텍스트 입력을 요구하는 방법?

다음은 모든 연락처 이름에 대해 do while 루프의 주 활동에서 호출하는 내 함수입니다. rating은 전역 변수입니다.

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());   

답변

1

짧은 대답 :하지 마십시오.

긴 대답 : 사용자 입력을 기다리는 동안 GUI 프로그램의 메인 스레드를 차단해서는 안됩니다. 대신 계속 버튼을 제공하면 프로그램이 계속 진행되는 이벤트가 실행됩니다. 이것을 달성하는 데는 여러 가지 방법이 있습니다. 먼저 마음에 떠오르는 것은 신호와 세마포어입니다.

저는 안드로이드 프로그래밍에 능숙하지는 않지만 인텐트에 의존적 인 API와 비슷한 것이 있어야합니다.

1

활동의 주 스레드에서 루핑하는 것은 일반적으로 좋은 생각이 아닙니다. 하지만 당신은 커서에서 다음 데이터 집합을 유도 할 수있는 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

에 사용되는
관련 문제