Note that there are some explanatory texts on larger screens.

plurals
  1. POCan not set OnListItemClick on Tab Activity
    primarykey
    data
    text
    <p>I want to do an activity with tab bar, and i have 5 tab menus. The MainActivity includes Tabhost like that;</p> <pre><code>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 &lt; 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&lt;?&gt;) 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); } </code></pre> <p>}</p> <p>When i click on Tab2, Tab2.java class will be called. This class has a listview. I want to play music on youtube when i click on list item. My code is working but OnListItemClick method is not working. </p> <pre><code>public class Tab2 extends Activity{ private LayoutInflater mInflater; private Vector&lt;RowData&gt; 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&lt;RowData&gt;(); for (int i = 0; i &lt; 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&lt;RowData&gt; { public CustomAdapter(Context context, int resource, int textViewResourceId, List&lt;RowData&gt; 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; } } } </code></pre> <p>}</p> <p>I have no problem with the layouts, i m sure about that so no need to show my xml files. :)</p> <p><strong>Updated</strong></p> <p>I change Tab2 class with following codes;</p> <pre><code>public class Tab2 extends Activity implements AdapterView.OnItemClickListener{ private LayoutInflater mInflater; private Vector&lt;RowData&gt; 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&lt;RowData&gt;(); for (int i = 0; i &lt; 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&lt;RowData&gt; { public CustomAdapter(Context context, int resource, int textViewResourceId, List&lt;RowData&gt; 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&lt;?&gt; 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"))); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload