1

有谁知道标签中的内容在切换到时是如何更新的?换句话说,当我更改选项卡时,我想要内容,在我更改为刷新或重新创建的选项卡中。

我有这个代码作为 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”选项卡在每次切换到内容时重新创建内容?有没有简单的命令可用?

4

1 回答 1

3

当您切换选项卡时,将调用“onResume”方法(在活动中,NedlastetBussruter)。在该方法中,您可以“重新加载”您的数据。所以添加 onResume 方法。此外,您可能需要调用 yourAdapter.notifyDataSetChanged();

于 2011-01-20T15:29:09.490 回答