2012-04-23 4 views
2

하나의 활동을 다른 활동으로 전달하려고하는데 진행하는 동안 진행 대화 상자를 표시하고 싶습니다. 주로 두 번째 활동이 정보를 처리 할 때. 나는 그것을 읽고 올바른 방법은 asynctask 것으로 보인다. 아니면 다른 방법이 있습니까? 내가 사용하려고진행/활동 사이의 진행 대화 상자

 public class DissertationActivity extends ListActivity { 
/** Called when the activity is first created. */ 

public ArrayList<String> book_Array = new ArrayList<String>(); 
ArrayAdapter<String> adapter; 

String href = ""; 
String href1 = ""; 
String search_Word = ""; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 



    Bundle extras = getIntent().getExtras(); 
    search_Word = extras.getString("query1"); 

    adapter = new ArrayAdapter<String>(this, R.layout.list_item_1, 
      book_Array); 
    setListAdapter(adapter); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    try { 
     Document doc = null; 
     Document guestLink = null; 

     guestLink = Jsoup.connect("https://aulib.abdn.ac.uk:443/F").get(); 
     Element link = guestLink.select("p > a").first(); 
     href1 = link.attr("href"); 
     href = href1.substring(0, href1.length() - 2); // removes -0 from 
                 // the 

     // href_Array.add(href); //adds href to the array because string 
     // wont add to the public var. 
     doc = Jsoup.connect(
       href + "&request=" + search_Word 
         + "&find_code=WRD&adjacent=N&x=0&y=0").get(); 
     // System.out.println(doc); 
     Elements headings = doc.select("td:eq(3)"); 
     // System.out.println(headings); 
     for (Element heading : headings) { 
      // System.out.println(heading.text()); 
      String j = heading.text(); 

      book_Array.add(j); 

     } 

    } catch (IOException e) { 
     e.printStackTrace(); 

    } 

    book_Array.remove(0); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(1); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(2); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove("Search"); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(" | "); 
    adapter.notifyDataSetChanged(); 
    book_Array.remove(0); 
    adapter.notifyDataSetChanged(); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> a, View v, int position, 
       long id) { 
      // Context context = getApplicationContext(); 
      int query = position; 
      // String text = book_Array.get(position); 
      // int duration = Toast.LENGTH_SHORT; 
      // Toast toast = Toast.makeText(context, 
      // String.valueOf(position), //shows the postion in the array 
      // list 
      // duration); 
      // toast.show(); 

      Intent intent = new Intent(DissertationActivity.this, 
        FullDetailsActivity.class); 
      intent.putExtra("href", href); 
      intent.putExtra("query1", (int) query); 
      intent.putExtra("search_Word", search_Word); 

      startActivity(intent); 
     } 
    }); 

} 

} 

:이 제 2 차 활동 활동 한

public class SearchActivity extends Activity { 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.search); 

    final EditText edittext = (EditText) findViewById(R.id.edittext); 
    edittext.setOnKeyListener(new OnKeyListener() { 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // If the event is a key-down event on the "enter" button 
      if ((event.getAction() == KeyEvent.ACTION_DOWN) 
        && (keyCode == KeyEvent.KEYCODE_ENTER)) { 
       // Perform action on key press 
       String query = edittext.getText().toString(); 
       // gets the text and makes sure its a string 
       Intent intent = new Intent(SearchActivity.this, 
         DissertationActivity.class); 
       intent.putExtra("query1", query); 
       startActivity(intent); 

       return true; 
      } 
      return false; 
     } 
    }); 

    final Button button = (Button) findViewById(R.id.searchButton); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      String query = edittext.getText().toString(); 
      // gets the text and makes sure its a string 

      Intent intent = new Intent(SearchActivity.this, 
        DissertationActivity.class); 
      intent.putExtra("query1", query); 
      startActivity(intent); 

     } 
    }); 

} 
    } 

:

 this.pd = ProgressDialog.show(this, "Working..", "Downloading Data...", 
      true, false); 

하지만 그건 작동하지 않았다

여기 내 코드입니다.

어떻게하면 활동 사이에 진행 대화 상자가 표시됩니까?

도움 주셔서 감사합니다.

+1

왜 AsyncTask를 사용하지 않습니까? 구현할 세 가지 방법이 있습니다 : 'onPreExecute' - 진행률 막대를 보여줍니다; 'onPostExecute' - 진행률 막대를 제거합니다; 'doInBackground' -해야 할 일; – vtuhtan

+0

단지 팁으로, 설명하는 줄 앞에 주석을 달면 코드를 읽기가 더 쉬울 것입니다. 그렇게하면 사람들은 머리 속에있는 언어를 파싱하기 전에하려고하는 것을 알게 될 것입니다. – gobernador

+0

대화 상자를 어디에서 보셨습니까? 문제는 대화 상자가 표시되지 않는 것입니까? – dreamtale

답변

0

ProgressDialog.show를 호출하면 UI 스레드가 차단됩니다. 따라서 메소드가 반환 될 때까지 진행 대화 상자/막대가 표시되지 않습니다. 따라서 메소드 내에서 실행할 스레드를 만들 수 있습니다. 이렇게하면 기본 UI 스레드를 차단하지 않습니다. 샘플 코드 -

ProgressDialog spinnerDialog = ProgressDialog.show( 
Placeholder.this, "","Your text ", true); 
new Thread(new Runnable() { 
public void run() { 
    //your method code 
    return; 
} 
}).start(); 
+0

코드를 사용해 보았습니다. 올바른 위치에 있는지 확실하지 않습니다. 진행 대화 상자를 표시하지만 몇 초 후에 응용 프로그램이 중단됩니다. 나는 넣었다 ProgressDialog spinnerDialog = ProgressDialog.show ( \t \t \t \t DissertationActivity.this, "", "Your text", true); \t \t \t \t 새로운 Thread (새의 Runnable() { \t \t \t \t 공공 무효 실행() { 라인 (48)에 setListAdapter 이상적으로 작동해야 맞죠? – Tbuermann

+0

가. 당신은 또한 잠을 추가 할 수 있습니다 직후 명령문은 진행 대화 상자를 표시 할 시간을 표시하는 run 메소드의 첫 번째 행으로 사용됩니다. 메소드 코드는 나중에 따릅니다. – 22kar

관련 문제