0

我在尝试在列表视图中选择项目时遇到问题。我的活动有一个选项卡式布局,其中一个布局中有一个 ListView。

以下是我填充 Listview 的方式: 这是我在 XML 中声明 ListView 的方式:

Category_list_display.xml

<?xml version="1.0" encoding="utf-8"?>
<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">
</ListView>

下面是我用于填充 ListView 的数组:

category_values.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="category_options">
        <item >Operating Systems</item>
        <item >Programming</item>
    </string-array>

</resources>    

下面是java代码:

    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 <= 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<?> 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

        }
    }

当我尝试运行上述代码时,我在 logcat 中得到以下信息:

    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)

我相信错误是由于适配器无法获得位置而出现的。我没有任何明确的适配器,因为我通过 XML 直接将数组链接到 ListView。我想以这种方式保留代码。有没有办法解决这个问题并将选定的值传递给具有新意图的新活动?

我将不胜感激任何帮助/建议。

4

1 回答 1

0

我能够通过执行以下操作来解决我的问题:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final String[] links = getResources().getStringArray(R.array.tut_titles);
    String link = links[position];
    Intent i = new Intent(Intent.ACTION_VIEW);  
    i.setData(Uri.parse(link));  
    startActivity(i);          
}
于 2012-09-24T16:29:47.433 回答