Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems in getting selected ListView item
    primarykey
    data
    text
    <p>I am facing issues while trying to get the item selected in the listview. My activity has a tabbed layout with a ListView in one of the layouts. </p> <p>Following is how I have populated the Listview : This is how I declare the ListView in XML :</p> <p>Category_list_display.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/category_view" android:layout_width="match_parent" android:layout_height="match_parent" android:entries="@array/category_options"&gt; &lt;/ListView&gt; </code></pre> <p>Below is the array that I use for populating the ListView :</p> <pre><code>category_values.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string-array name="category_options"&gt; &lt;item &gt;Operating Systems&lt;/item&gt; &lt;item &gt;Programming&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt; </code></pre> <p>Below is java code :</p> <pre><code> package com.cheats; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.view.View; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; public class LandingPage extends SherlockActivity implements ActionBar.TabListener { private String[] tab_options = { "Categories", "Bookmarks", "Downloads" }; private ListView category_tab_list; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.splash_screen); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); for (int i = 0; i &lt;= 2; i++) { ActionBar.Tab tab = getSupportActionBar().newTab(); tab.setText("Tab " + i); tab.setTabListener(this); tab.setText(tab_options[i]); getSupportActionBar().addTab(tab); } } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add("New Update").setIcon(android.R.drawable.ic_menu_view) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.add("Search") .setIcon(R.drawable.ic_search) .setActionView(R.layout.collapsible_edittext) .setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); return true; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub int nTabSelected = tab.getPosition(); switch (nTabSelected) { case 0: setContentView(R.layout.category_list_display); category_tab_list = (ListView) findViewById(R.id.category_view); category_tab_list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; myAdapter, View myView, int myItemInt, long mylng) { int selectedPosition = myAdapter.getSelectedItemPosition(); Toast.makeText(getApplicationContext(), myItemInt, Toast.LENGTH_SHORT).show(); } }); break; case 1: setContentView(R.layout.splash_screen); break; case 2: setContentView(R.layout.splash_screen); break; } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } } </code></pre> <p>I get the following in the logcat when I try to run the above code :</p> <pre><code> 08-21 15:23:56.319: E/AndroidRuntime(1330): FATAL EXCEPTION: main 08-21 15:23:56.319: E/AndroidRuntime(1330): android.content.res.Resources$NotFoundException: String resource ID #0x0 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.content.res.Resources.getText(Resources.java:201) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.widget.Toast.makeText(Toast.java:258) 08-21 15:23:56.319: E/AndroidRuntime(1330): at com.cheats.LandingPage$1.onItemClick(LandingPage.java:72) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.widget.ListView.performItemClick(ListView.java:3513) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.os.Handler.handleCallback(Handler.java:587) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.os.Handler.dispatchMessage(Handler.java:92) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.os.Looper.loop(Looper.java:123) 08-21 15:23:56.319: E/AndroidRuntime(1330): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-21 15:23:56.319: E/AndroidRuntime(1330): at java.lang.reflect.Method.invokeNative(Native Method) 08-21 15:23:56.319: E/AndroidRuntime(1330): at java.lang.reflect.Method.invoke(Method.java:507) 08-21 15:23:56.319: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-21 15:23:56.319: E/AndroidRuntime(1330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-21 15:23:56.319: E/AndroidRuntime(1330): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>I believe that the errors are coming because of the adapter not able to get the position. I do not have any explicit adapter as I am linking the array to ListView directly via XML. I want to keep the code this way. Is there a way I can get this resolved and the pass the selected value to a new activity with a new intent ?</p> <p>I would be grateful for any help/suggestions. </p>
    singulars
    1. This table or related slice is empty.
    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