我想用标签栏做一个活动,我有5个标签菜单。 MainActivity包括像这样的Tabhost;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
String tab_title[] = { "Tab 1", "Tab 2", "Tab 3", "Tab 4", "Tab 5" };
int tab_drawables[] = { R.drawable.menu_bookmark,
R.drawable.menu_filemanager, R.drawable.menu_download,
R.drawable.menu_sharepage, R.drawable.menu_about };
Object tab_act[] = { Tab1.class, Tab2.class, Tab3.class, Tab4.class, Tab5.class };
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
TabSpec tab_spec;
for (int i = 0; i < tab_act.length; i++) {
tab_spec = tabHost.newTabSpec(tab_title[i]);
tab_spec.setIndicator(tab_title[i],
getResources().getDrawable(tab_drawables[i]));
tab_spec.setContent(new Intent(this, (Class<?>) tab_act[i]));
tabHost.addTab(tab_spec);
}
tabHost.setCurrentTab(0);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MainActivity.this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
当我点击Tab2时,将调用Tab2.java类。这个类有一个列表视图。当我点击列表项时,我想在youtube上播放音乐。我的代码正在运行,但OnListItemClick方法不起作用。
public class Tab2 extends Activity{
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
Context context ;
Bitmap bitmap;
public static String title[] = { "song 1", "song 2", "song 3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tab2);
super.onCreate(savedInstanceState);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for (int i = 0; i < title.length; i++) {
try {
rd = new RowData(i, title[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this,
R.layout.activity_list_view, R.id.title, data);
}
private class RowData {
protected int mId;
protected String mTitle;
RowData(int id, String title) {
mId = id;
mTitle = title;
}
@Override
public String toString() {
return mId + " " + mTitle;
}
}
public void onListItemClick(ListView parent, View v, int position, long id) {
if (position == 0) {
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=QK8mJJJvaes")));
}
else if (position == 1) {
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=HWyEEj2pSt0")));
}
else if (position == 2){
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=HWyEEj2pSt0")));
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
RowData rowData = getItem(position);
if (null == convertView) {
convertView = mInflater
.inflate(R.layout.activity_list_view, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private ImageView i11 = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if (null == title) {
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
}
}
}
我对布局没有任何问题,我确定这一点,所以不需要显示我的xml文件。 :)
更新
我使用以下代码更改Tab2类;
public class Tab2 extends Activity implements AdapterView.OnItemClickListener{
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
Context context ;
Bitmap bitmap;
public static String title[] = { "song 1", "song 2", "song 3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.tab2);
super.onCreate(savedInstanceState);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for (int i = 0; i < title.length; i++) {
try {
rd = new RowData(i, title[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this,
R.layout.activity_list_view, R.id.title, data);
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
private class RowData {
protected int mId;
protected String mTitle;
RowData(int id, String title) {
mId = id;
mTitle = title;
}
@Override
public String toString() {
return mId + " " + mTitle;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
RowData rowData = getItem(position);
if (null == convertView) {
convertView = mInflater
.inflate(R.layout.activity_list_view, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private ImageView i11 = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if (null == title) {
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=4jHrdLUFA-8")));
}
else if (position == 1) {
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=-HMfKUtYHog")));
}
else if (position == 2){
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=7beBw8uQOR8")));
}
}
}
答案 0 :(得分:1)
公共类Tab2扩展了Activity
Tab2
扩展了活动,而不是ListActivity
。
要使其正常运行,请在ListView
中找到R.layout.tab2
小部件:
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for (int i = 0; i < title.length; i++) {
try {
rd = new RowData(i, title[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this,
R.layout.activity_list_view, R.id.title, data);
ListView lv = (ListView) findViewById(R.id.your_listiew_id);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
将班级定义更改为:
public class Tab2 extends Activity implements AdapterView.OnItemClickListener {
并将onListItemClick(ListView parent, View v, int position, long id)
更改为:
@Override
public void onItemClick((AdapterView<?> parent, View view, int position, long id)
您的代码存在的问题是您从未将OnItemClickListener
分配给ListView
。如果您想使用onListItemClick (ListView l, View v, int position, long id)
,那么Tab2
应该扩展ListActivity
,而不是Activity
。要了解原因:Link。