2016-07-04 54 views
0

我想顯示一個alertdialog並從用戶那裏獲得一些輸入,然後在輸入asynctask中處理輸入,但alertdialog在輸入和asynctask開始執行之前變爲解除。如何在asynctask開始之前顯示alertdialog

我該怎麼辦?請幫幫我。這是我的代碼;

private String[] newspapers,newspapersUrl,newspapersPath; 
private String[] choosed,choosedUrl,choosedPath; 
private boolean[] checked; 

private int perNewspaper; 
private boolean returned = false; 

private AlertDialog.Builder builder; 


private FetchingNewsByChoice fetchingNewsByChoice; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    returned = AlertDialogCreation(); 

    if(returned) 
    { 
     fetchingNewsByChoice = new FetchingNewsByChoice((AppCompatActivity)MainActivity.this,choosed,choosedPath,perNewspaper); 
     fetchingNewsByChoice.execute(); 
    } 


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

@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_main, 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_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

//***** Alert Dialog *****// 
public boolean AlertDialogCreation() 
{ 
    //***** File Control And Creating An Dialog Interface if this is first usage *****// 
    File file = getApplicationContext().getFileStreamPath("First.txt"); 

    if(!file.exists()) 
    { 
     writeToFile("First.txt", "0",true); 
    } 

    if(readFromFile("First.txt",false)[0].equals("0")) 
    { 
     newspapers = readFromFile("Choicable Newspaper.txt",true); 
     newspapersUrl = readFromFile("Choicable Url.txt",true); 
     newspapersPath = readFromFile("Choicable Path.txt",true); 

     checked = new boolean[newspapers.length]; 

     builder = new AlertDialog.Builder(this,R.style.AppCompatAlertDialogStyle); 

     builder.setTitle("Lütfen Güncel Haberlerini Takip Etmek İstediğiniz Siteleri Seçiniz.."); 
     builder.setItems(newspapers, null); 

     builder.setPositiveButton("Tamam", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       int count = 0; 

       for (int a = 0; a < newspapers.length; a++) { 
        if (checked[a]) { 
         count++; 
         writeToFile("Choosed.txt", newspapers[a], false); 
         writeToFile("ChoosedUrl.txt", newspapersUrl[a], false); 
         writeToFile("ChoosedPath.txt", newspapersPath[a], false); 
        } 

        if (a == (newspapers.length - 1) && count == 0) { 
         Toast.makeText(getApplicationContext(), "En az bir gazete seçmek zorundasınız..", Toast.LENGTH_LONG).show(); 
         builder.show(); 
        } 
       } 
      } 
     }); 

     builder.setMultiChoiceItems(newspapers, checked, new DialogInterface.OnMultiChoiceClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i, boolean isChecked) { 

       if (isChecked) { 
        checked[i] = true; 
       } else { 
        checked[i] = false; 
       } 
      } 
     }); 

     try 
     { 
      builder.show(); 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
     } 


     writeToFile("First.txt", "1", true); 

     choosed = readFromFile("Choosed.txt",false); 
     choosedUrl = readFromFile("ChoosedUrl.txt",false); 
     choosedPath = readFromFile("ChoosedPath.txt",false); 

     if(choosed != null) 
     { 
      perNewspaper = (int) Math.ceil(newspapers.length/choosed.length); 

      if(perNewspaper > 15) 
      { 
       perNewspaper = 14; 
      } 
     } 

     return true; 
    } 
    else if(readFromFile("First.txt",false)[0].equals("1")) 
    { 
     newspapers = readFromFile("Choicable Newspaper.txt",true); 
     newspapersUrl = readFromFile("Choicable Url.txt",true); 
     newspapersPath = readFromFile("Choicable Path.txt",true); 

     choosed = readFromFile("Choosed.txt",false); 
     choosedUrl = readFromFile("ChoosedUrl.txt",false); 
     choosedPath = readFromFile("ChoosedPath.txt",false); 

     perNewspaper = (int) Math.ceil(newspapers.length/choosed.length); 

     if(perNewspaper > 15) 
     { 
      perNewspaper = 14; 
     } 

     return true; 
    } 
    else 
    { 
     return false; 
    } 
    //***** File Control And Creating An Dialog Interface if this is first usage *****// 
} 

//*****Files*****// 
private void writeToFile(String fileName,String data,boolean isPrivate) 
{ 
    try 
    { 
     OutputStreamWriter outputStreamWriter; 

     if(isPrivate) 
     { 
      outputStreamWriter = new OutputStreamWriter(getApplicationContext().openFileOutput(fileName, MODE_PRIVATE)); 
     } 
     else 
     { 
      outputStreamWriter = new OutputStreamWriter(getApplicationContext().openFileOutput(fileName, MODE_APPEND)); 
     } 

     outputStreamWriter.write(data+"\r\n"); 

     outputStreamWriter.close(); 
    } 
    catch (IOException e) 
    { 

    } 
} 


public String[] readFromFile(String fileName,boolean isAsset) 
{ 
    String[] ret=null; 

    try 
    { 
     InputStream inputStream; 

     if(isAsset) 
     { 
      inputStream = getApplicationContext().getAssets().open(fileName); 
     } 
     else 
     { 
      inputStream = getApplicationContext().openFileInput(fileName); 
     } 


     if (inputStream != null) 
     { 

      InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 


      String receiveString; 

      StringBuilder stringBuilder = new StringBuilder(); 

      while ((receiveString = bufferedReader.readLine()) != null) { 

       stringBuilder.append(receiveString).append(","); 
      } 

      inputStream.close(); 
      ret = stringBuilder.toString().split(","); 

      inputStream.close(); 
     } 
    } 
    catch (FileNotFoundException e) 
    { 

    } 
    catch (IOException e) 
    { 

    } 

    return ret; 
} 
+0

你能告訴我們一些你的代碼嗎?你應該調用你的alertdialog並且在調用你的asynctask之前影響值,以防你在asynctask中調用alertdialog。 – theyouishere

回答

0

我假設你有一個setpositivebutton點擊收聽。 用所需的輸入啓動您的聽衆中的asynk任務。

+0

非常感謝你!!我上了好幾個小時。再次感謝它太多了。 –

+0

不客氣:) – Memme

0
  1. 首先用參數編寫你自己的函數。
  2. 調用該函數內的異步任務。
  3. 現在通過將表單值作爲參數傳遞給該函數來調用該函數,然後在完成對話框的變暗之後。
相關問題