- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用标签栏做一个 Activity ,我有 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 类。这个类有一个 ListView 。当我点击列表项时,我想在 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")));
}
}
最佳答案
public class Tab2 extends Activity
Tab2
扩展 Activity,而不是 ListActivity
。
要让它正常工作,请在 R.layout.tab2
中找到 ListView
小部件:
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 .
关于android - 无法在选项卡 Activity 上设置 OnListItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18323960/
在我的应用程序中,我有一个扩展 ListActivity 并使用 ListView 的类。我的 onListItemClick 方法只是不想工作。在该方法内部,它必须确定所单击的项目是“锻炼 A”还是
我尝试将 SherlockListFragment 与自定义适配器一起使用,但 onListItemClick 方法不起作用。我根本无法点击列表。有人可以帮我修复我的 onListItemClick
我不认为这个问题是由我的 ListActivity 子类引起的。我认为这与我的 BaseAdapter 子类有关: package com.mohit.gtodo; import com.mohit.
我将 onListItemClick 与 simple_list_item_1 结合使用 public class DrinkCategoryActivity extends ListActivity
我正在尝试放置 onListItemClick,但是当我启动应用程序时,onListItemClick 没有任何反应。谁能帮我这个? 这是我的代码: @Override public void
您好,我目前在 Android 市场上的一些应用程序上遇到错误: 这是堆栈跟踪: java.lang.NullPointerException at ****.****.MainActi
我收到此错误 - 无法解析方法 'onListItemClick(android.widget.ListView,android.view.View,int,long)'我试过使用 onClickLi
我试图从数据库 onListItemClick 的“threadid”列中获取一行的值,但是在我单击的每个列表项上,我都会获取上一行的值,当我单击列表的第一项时,我的应用程序崩溃,我不知道问题出在哪里
public class List_Items extends ListActivity{ private ArrayList> mylist = new ArrayList>(); publi
我正在处理一个 ListView ,当您单击一个项目时,它会显示一个带有 2 个选项的警报对话框,在他们选择一个选项后,它会将他们带回 ListView ,在那里他们可以再次选择另一个选项。出于某种原
你能解释一下这个方法的参数吗?我不明白他们。 http://developer.android.com/reference/android/app/ListActivity.html#setListA
当我点击项目时,我的 onListItemClick 永远不会被调用,类是扩展 fragment 而不是列表 fragment ,因为我在这个 fragment 中有其他 View 项目不是列表,那么
我正在将一个数组加载到 ListView 中,当我单击列表项时,我想在同一 Activity 中的 TextView 中显示该项目。该列表当前正在加载,但当单击某个项目时程序崩溃。有什么想法吗? im
未调用函数 onListItemClick: 列表 Activity package it.devapp.listview; import java.util.ArrayList; import co
我正在为名为 row.xml 的 listView 列表项使用自定义布局 这是我的 row.xml 文件: 在上面的布局中,onListItemCli
现在我正在尝试创建一个 android onListItemClick 方法。但每次我这样做时都会遇到大量语法错误,指出预期的分号。通常,当我遇到这样的语法错误时,我知道只需添加一个分号,但在这种情况
我在 ListActivity 中随机收到 nullpointerException(我无法一致地重现该错误)。 StackTrace 在第一行 super.onListItemClick() 上显示
在我的应用程序中,我有一个列表。该列表包括一些文本和一个复选框。在列表的 Xml 定义中,如果我输入 android:focusable="true" 然后我可以单击它并将其状态从选中更改为未选中,反
最后编辑我在这里找到解决方案的问题:http://code.google.com/p/android/issues/detail?id=3414#c27 原帖我有一个 ListView(实际上是一个
我是新来的,所以如果这看起来像是一个愚蠢的问题,请多多包涵。我目前正在使用 onListItemClick 来检索所选 ListItem 的 ID。我对 int position 和 long id
我是一名优秀的程序员,十分优秀!