2011-01-20 111 views
1

有誰知道標籤中的內容在被切換到時如何更新? 換句話說,當我更改選項卡時,我想要內容,在選項卡中更改爲刷新或重新創建。如何更新tabChange(Android)上的內容

我有這樣的代碼作爲TabActivity:

package com.example.HelloTabWidget; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 

public class Bussruter extends TabActivity implements OnTabChangeListener { 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Resources res = getResources(); // Resource object to get Drawables 
    TabHost tabHost = getTabHost(); // The activity TabHost 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, NedlastetBussruter.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("Nedlastet bussruter").setIndicator("Nedlastet bussruter", 
         res.getDrawable(R.drawable.ic_tab_nedlastet)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

    // Do the same for the other tabs 
    intent = new Intent().setClass(this, AlleBussruter.class); 
    spec = tabHost.newTabSpec("Alle bussruter").setIndicator("Alle bussruter", 
         res.getDrawable(R.drawable.ic_tab_alle)) 
        .setContent(intent) 
        ; 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(2); 
    tabHost.setOnTabChangedListener(this); 
} 

@Override 
public void onTabChanged(String tabId) { 
    TabHost th = getTabHost(); 
    th.getCurrentTabView().destroyDrawingCache();//i know this is wrong :S 
    th.getCurrentTabView().buildDrawingCache();//and this is also wrong.... 
} 
} 

現在讓我們說,我改變到具有碼的標籤,「NedlastetBussruter」:

package com.example.HelloTabWidget; 

import java.io.File; 
import java.util.ArrayList; 

import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.ActivityNotFoundException; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.AdapterView.OnItemLongClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class NedlastetBussruter extends ListActivity { 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String[] listName = this.getListNameNedlastet(); 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, listName)); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     if(!listName[0].equals("Ingen bussruter er lastet ned.")){ 
     lv.setOnItemClickListener(this.createClickListenerNedlastet()); 
     lv.setOnItemLongClickListener(this.createLongClickListener()); 
     lv.setClickable(true); 
     lv.setLongClickable(true); 
     }else{ 
     lv.setClickable(false); 
     lv.setLongClickable(false); 
     } 
     /*new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
       Toast.LENGTH_SHORT).show(); 
      } 
     });*/ 
     /*TextView textview = new TextView(this); 
     textview.setText("This is the Artists tab"); 
     setContentView(textview);*/ 
    } 
private OnItemLongClickListener createLongClickListener() { 
    return 
    new OnItemLongClickListener() { 
    public boolean onItemLongClick(AdapterView<?> parent, final View view, 
     final int position, long id) { 
    if(isAvailable(position)){ 
    try{ 
     new AlertDialog.Builder(parent.getContext()) 
     .setMessage("Vil du slette bussruten?") 
     .setCancelable(false) 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 

      deleteFile(view, position); 
       StorageHandler.availability[position] = false; 
      } 
     }) 
     .setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
      dialog.cancel(); 
      } 
     }) 
     .show(); 
    }catch(Exception e){ 
     toastText("ERROR: "+e.getMessage(), Toast.LENGTH_LONG); 
    } 
    return true; 
    }else return false; 
    } 
    }; 
} 
private OnItemClickListener createClickListenerNedlastet(){ 
    return 
    new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
     int position, long id){ 

     boolean temp = StorageHandler.availability[position]; 
     if(temp == true){ 
     if(Environment.getExternalStorageState().equals(
     Environment.MEDIA_MOUNTED)){ 
     File file = new File(StorageHandler.sdcardUrl+ StorageHandler.uniqueBussFilename[position]); 

       if (file.exists()) {//open the file 
        Uri path = Uri.fromFile(file); 
        Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(path, "application/pdf"); 
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        try { 
         startActivity(intent); 
        } 
        catch (ActivityNotFoundException e) { 
         toastText("No Application Available for viewing PDF", Toast.LENGTH_LONG); 
        } 
       }else{ 
        toastText("The file "+file.getAbsolutePath()+" was not found.", Toast.LENGTH_LONG); 
       } 
     }else{ 
     toastText("sdkort er ikke tilgjengelig.", Toast.LENGTH_LONG); 
     } 
     } 
    }; 
    }; 
} 
private void toastText(String t, int l){ 
    final String text = t; 
    final int length = l; 
    Toast.makeText(getApplicationContext(), text, length).show();  

} 
private void deleteFile(View view, int index){ 
    try{ 
    File f = new File(StorageHandler.sdcardUrl + StorageHandler.uniqueBussFilename[index]); 
    f.delete(); 
    }catch(Exception e){ 
    toastText("ERROR: "+e.getMessage(), Toast.LENGTH_LONG); 
    } 
} 
private boolean isAvailable(int position){ 
    File f = new File(StorageHandler.sdcardUrl + StorageHandler.uniqueBussFilename[position]); 
    if(f.exists()){ 
    return true; 
    } 
    return false; 
} 
private String[] getListNameNedlastet() { 
    this.checkAvaliablePdfs(); 
    ArrayList<String> al = new ArrayList<String>(); 

    for(int i = 0;i<StorageHandler.uniqueId.length-1;i++){ 
    if(StorageHandler.availability[i]){ 
    //String br = bussRuter[i]; //used for debugging... 
    al.add(StorageHandler.uniqueId[i] + " - " + StorageHandler.bussRuter[i]); 
    } 
    } 
    String[] output = new String[al.size()]; 
    if(output.length == 0){ 
    output = new String[]{"Ingen bussruter er lastet ned."}; 
    }else{ 
    for(int i=0;i < al.size();i++){ 
    output[i] = al.get(i); 
    } 
    } 
    return output; 
} 
private void checkAvaliablePdfs(){ 
    for(int i = 0;i<StorageHandler.uniqueBussFilename.length;i++){ 
    File f = new File(StorageHandler.sdcardUrl + StorageHandler.uniqueBussFilename[i]); 
    if(f.exists()){ 
    StorageHandler.availability[i] = true; 
    } 
    } 
} 
} 

我怎樣才能讓「nedlastet bussruter」選項卡在每次切換到內容時重新創建內容?有沒有簡單的命令?

回答

3

切換標籤頁時,將調用「onResume」方法(在活動NedlastetBussruter中)。在該方法中,您可以「重新加載」您的數據。所以添加onResume方法。 也可能需要調用yourAdapter.notifyDataSetChanged();

+0

我應該在哪裏添加onResume方法?在nedlastetBussruter類? – 2011-01-20 17:24:45