列表视图中的 onItemClickListener 无法与 Android 中的 TabActivity 一起使用

发布于 2024-11-02 16:37:50 字数 4246 浏览 0 评论 0原文

我制作了一个具有各种选项卡的应用程序。每个选项卡都有不同的 ListActivity。当我使用 TabWidget 作为启动器启动应用程序并单击选项卡时,会显示项目列表,但 onClick 不起作用。但是当我不使用 TabActivity 时,应用程序工作正常。以下是我的代码。

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(), NewsDetails.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>

任何人都可以帮助使用选项卡视图运行应用程序

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(), NewsDetails.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远山浅 2024-11-09 16:37:50

您应该使用 onListItemClick() 而不是 onItemClick()

编辑: 进一步说明...

您正在使用 ListActivity,它有一个名为 onListItemClick(...) 的方法

,而不是在 ListActivity 的 ListView 上调用 onSetItemClickListener(...),只需按如下方式重写 onListItemClick(...)...

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
    }
}

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文