gpt4 book ai didi

android - 无法在选项卡 Activity 上设置 OnListItemClick

转载 作者:行者123 更新时间:2023-11-30 03:25:20 25 4
gpt4 key购买 nike

我想用标签栏做一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com