开发者

onItemClickListener in listview not working with TabActivity in Android

I have made an application which has various tabs. Each tab has a different ListActivity. When i start the application with the TabWidget as Launcher and click on a tab the list of items appear but onClick does not work. but when i don't use the TabActivity the application works fine. following is my code.

TabWidget.java

public class InfralineTabWidget extends TabActivity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = (TabHost)getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, TopNewsActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("topNews").setIndicator("Top News", res.getDrawable(R.drawable.tab_news)).setContent(intent);
    tabHost.addTab(spec);
    }
}

TopNewsActivity.java

public class TopNewsActivity extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


    String xml = XMLfunctions.getTopNewsXML();
    Document doc = XMLfunctions.XMLfromString(xml);

    int numResults = XMLfunctions.numResults(doc);

    if((numResults <= 0)){
        Toast.makeText(TopNewsActivity.this, "No Result Found", Toast.LENGTH_LONG).show();  
        finish();
    }

    NodeList nodes = doc.getElementsByTagName("result");

    for (int i = 0; i < nodes.getLength(); i++) {                           
        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);
        map.put("id", XMLfunctions.getValue(e, "id"));
        map.put("title", XMLfunctions.getValue(e, "title"));
        mylist.add(map);            
    }       

    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "title"}, new int[] { R.id.item_title});

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
            Intent i = new Intent(view.getContext(), NewsDe开发者_开发技巧tails.class);
            i.putExtra("content_id", o.get("id"));
            i.putExtra("title", o.get("title"));
            startActivity(i);

        }
        });
    }
}

Main.xml

android:id="@android:id/tabhost"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:padding="5dp">

    <TabWidget

        android:id="@android:id/tabs"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

    <FrameLayout

        android:id="@android:id/tabcontent"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:padding="5dp" />

    <TextView  

            android:id="@+id/item_title"

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceMedium"

        android:padding="2dp"

        android:textSize="20dp" />

    <TextView  

        android:id="@+id/item_subtitle"

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:padding="2dp"

        android:textSize="13dp" />

</LinearLayout>

Can anyone help in running the application with tab view


You should be using onListItemClick() instead of onItemClick()

EDIT: Further explanation...

You are using ListActivity which has a method called onListItemClick(...)

Instead of calling onSetItemClickListener(...) on the ListActivity's ListView, simply override onListItemClick(...) as follows...

public class MyListActivity extends ListActivity {

    // onCreate() etc here

    @Override
    public void onListItemClick(ListView parent, View view, int position, long id) {
        // Handle list item clicks here
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜