タブが切り替えられたときにタブ内のコンテンツがどのように更新されるか知っている人はいますか? つまり、タブを変更すると、コンテンツが必要になり、タブで更新または再作成するように変更されます。
私は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」タブが切り替えられるたびにコンテンツを再作成するにはどうすればよいですか? 簡単なコマンドはありますか?