I'm having some problem using a ListActivity inside a TabHost. I only want to use one class to show different lists (which I manage), but the problem开发者_开发百科 is that when I click on a new tab, the list doesn't get updated/populated. I found one way to solve it, and that was to create duplicated classes (Listclass1.java, Listclass2.java...). Really not great coding practice.
Here is some of my tabhost code:
intent = new Intent().setClass(this, ListCreator.class);
intent.putExtra("NAME", "abilities");
this.addTab("", R.drawable.abilities, intent);
intent = new Intent().setClass(this, ListCreator.class);
intent.putExtra("NAME", "map");
this.addTab("", R.drawable.map_rules, intent);
And here is the Listclass:
package no.heroclix.rules;
import heroclix.Rules.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ListCreator extends ListActivity {
private String NAME;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
NAME = getIntent().getExtras().getString("NAME");
Log.d("List", NAME);
final String[] rules;
if(NAME.equals("abilities")) rules = getResources().getStringArray(R.array.abilities_names);
else if(NAME.equals("map")) rules = getResources().getStringArray(R.array.map_rules_names);
else if(NAME.equals("objects")) rules = getResources().getStringArray(R.array.objects_names);
else if(NAME.equals("ata")) rules = getResources().getStringArray(R.array.ata_names);
else if(NAME.equals("feats")) rules = getResources().getStringArray(R.array.feats_names);
else rules = getResources().getStringArray(R.array.bfc_names);
setListAdapter(new ArrayAdapter<String>(this, R.layout.listrow, rules));
ListView lw = getListView();
lw.setTextFilterEnabled(true);
lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
Intent myIntent = new Intent(getApplicationContext(), ListInfo.class);
myIntent.putExtra("NAME", NAME);
myIntent.putExtra("POSITION", position);
myIntent.putExtra("SIZE", rules.length);
startActivityForResult(myIntent, 0);
}
});
}
}
Any clues?
Edit: I should mention that this worked for a while (not using duplicated classes), but after I did some change (that I have removed) it doesn't work anymore.
If I understand correctly, you don't really need to extend ListActivity, and you're trying to do something partially like Is it realizable on Android?
How about TabHost.OnTabChangeListener?
"Interface definition for a callback to be invoked when tab changed". You could use that to update the tabs upon switching between them.
精彩评论