2012-10-11 112 views
0

我想顯示一個ProgressDialog時按下我的列表視圖中的任何項目,而Detail類在背景中轉動,我知道我必須使用asyncTask,但我無法弄清楚如何,任何人都可以幫助我請?ProgressDialog Android AsyncTAsk

這是我的片段在那裏我去顯示ProgressDialog

public class FeedPlayerIndexFragment extends SherlockFragment{  
String url="http://www.toto.com/index.php?format=feed&type=rss"; 
ArrayList<Article> feeds ; 
AndroidSaxFeedParser feedParser= new AndroidSaxFeedParser(url); 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    feeds=feedParser.parse(); 
    View view=inflater.inflate(R.layout.main, container, false); 
    CustomItemAdapter_News lfa = new CustomItemAdapter_News(getActivity().getApplicationContext(), feeds); 
    ((ListView)view.findViewById(R.id.listFeed)).setAdapter(lfa); 

    ((ListView)view.findViewById(R.id.listFeed)).setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> arg0, View v, int position,long id) { 
      Bundle b = new Bundle(); 
      b.putInt("position", position); 
      b.putString("url", url); 
      Intent intent=new Intent(getActivity().getApplicationContext(), DetailActivity.class); 
      intent.putExtras(b); 
      startActivity(intent); 
     } 
    }); 
    return view; 
} 
} 

這是我的詳細活動嘩嘩應該打開後臺

public class DetailActivity extends Activity{ 
String descriptionFinale = null,imageFinale = null,url; 
int position; 
TextView description_,titre,date; 
ImageView image_; 
ArrayList<Article> feeds ; 

WebView contentWebView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.detail); 
    Bundle b = getIntent().getExtras(); 
    position=b.getInt("position"); 
    url=b.getString("url"); 
    AndroidSaxFeedParser feedParser= new AndroidSaxFeedParser(url); 
    feeds=feedParser.parse(); 


    date=(TextView) findViewById(R.id.date); 
    contentWebView= (WebView)findViewById(R.id.contentWebView); 
    titre=(TextView) findViewById(R.id.titre); 

    date.setText(feeds.get(position).getTitle()); 
    GetDescriptionWebView.getDescriptionImage(position, feeds,contentWebView); 
    titre.setText("Postulé le: "+GetDateFormat.getDate(position, feeds)); 

} 

} 
+0

具有u解決您的問題或需要解決的幫助??? –

+0

我需要幫助:(( –

+0

你想在你的DetailActivity類中獲得進度對話框??? –

回答

1

你可以試試這個代碼

public class DetailActivity extends Activity 
{ 
    String descriptionFinale = null,imageFinale = null,url; 
    int position; 
TextView description_,titre,date; 
ImageView image_; 
ArrayList<Article> feeds ; 

WebView contentWebView; 


ProgressDialog dialog; 

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

    feeds=new Arraylist<Article>; 
    contentWebView= (WebView)findViewById(R.id.contentWebView); 
    date=(TextView) findViewById(R.id.date); 
    titre=(TextView) findViewById(R.id.titre); 
    Bundle b = getIntent().getExtras(); 
    position=b.getInt("position"); 
    url=b.getString("url"); 
    AndroidSaxFeedParser feedParser= new AndroidSaxFeedParser(url); 


    new MyFetchTask().execute(); 



} 

public class MyFetchTask extends AsyncTask<Object, Object, Object> 
{ 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 

     dialog=ProgressDialog.show(DetailActivity.this, "", "Loading..."); 

    } 


    @Override 
    protected Object doInBackground(Object... params) { 
     // TODO Auto-generated method stub 

     feeds=feedParser.parse(); 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Object result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 

     dialod.dismiss(); 
     date.setText(feeds.get(position).getTitle()); 
     GetDescriptionWebView.getDescriptionImage(position, feeds,contentWebView); 
     titre.setText("Postulé le: "+GetDateFormat.getDate(position, feeds)); 

    } 

} 

} 
+0

這是有用的,但我有11片段,我需要調用所有片段的細節活動類! –

0

這是一個asynctask希望這將有助於

class NewCouponsTask extends AsyncTask<String, Void, ArrayList<NewCoupons>> 

{ 
NewCouponsScreen parentActivity; 

public NewCouponsTask(NewCouponsScreen parent) { 
    // TODO Auto-generated constructor stub 
    parentActivity = parent; 
} 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
} 
@Override 
protected ArrayList<NewCoupons> doInBackground(String... params) { 

    URL url=null; 
    ArrayList<NewCoupons> couponslist=null; 
    try { 
     url = new URL(ApplicationLinks.newCouponsLink); 

    InputSource is = new InputSource(url.openStream()); 
    is.setEncoding("UTF-8"); 
    SaxFeedParser saxParser = new SaxFeedParser(ApplicationLinks.newCouponsLink); 
    Log.e("Error",ApplicationLinks.newCouponsLink); 
    couponslist=saxParser.parse(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return couponslist; 
} 

@Override 
protected void onPostExecute(ArrayList<NewCoupons> result) { 
    // TODO Auto-generated method stub 
    parentActivity.fetchNewCoupons(result); 
     } 

} 

您可以 onPreExecute()函數 加載 進度和現在停止在 onPostExecute()函數