获取游标索引

Get index of cursor

如何获取索引值?不是 id,而是游标中基于零的位置 使用字符串值?

政治是我必须寻找索引的值,该索引是从零开始计数的索引 2。 Tableurls 编号,标题,"url" 1、测试,"testurl" 2、测试 2、"testurl2" 5、"Political stuff"、"politicurl" 字符串 url="politics";

idd 喜欢在游标中查询字符串 url 并检索索引或位置

我是否使用 for 循环?有了这个 cursor.getString(cursor.getColumnIndexOrThrow("url"));

我是不是含糊其辞了?对不起,我只是在闲聊。 感谢任何帮助。

我是这么想的,没有找到 0;

                int pos=0;
                cursor.moveToFirst();
                for(int i=0; i<cursor.getCount(); i++){
                if(url==cursor.getString(cursor.getColumnIndexOrThrow("url"))){
                pos=cursor.getInt(i);
                cursor.move(i);
                }
                }

很好用,但看起来很糟糕。 我试图让这个 android 选项卡片段脚本工作:

然后还可以在 html 页面中获取超链接以响应选项卡导航。 在 webview 上使用 shouldOverrideUrlLoading 函数: 它按原样工作:但我敢肯定它是一个丑陋的 hack ;) 无论如何感谢您的帮助。

class SamplePagerAdapter extends PagerAdapter {

    Cursor cursor = TabActivity.cursor;
    /**
     * @return the number of pages to display
     */
    @Override
    public int getCount() {
        return TabActivity.cursor.getCount();
    }

    /**
     * @return true if the value returned from {@link #instantiateItem(android.view.ViewGroup, int)} is the
     * same object as the {@link android.view.View} added to the {@link android.support.v4.view.ViewPager}.
     */
    @Override
    public boolean isViewFromObject(View view, Object o) {
        return o == view;
    }

    // BEGIN_INCLUDE (pageradapter_getpagetitle)
    /**
     * Return the title of the item at {@code position}. This is important as what this method
     * returns is what is displayed in the {@link SlidingTabLayout}.
     * <p>
     * Here we construct one using the position value, but for real application the title should
     * refer to the item's contents.
     */
    @Override
    public CharSequence getPageTitle(int position) {
        String Year="0";
        cursor.moveToPosition(position);
        Year = cursor.getString(cursor.getColumnIndexOrThrow("title"));
        return Year;
    }
    // END_INCLUDE (pageradapter_getpagetitle)

    /**
     * Instantiate the {@link android.view.View} which should be displayed at {@code position}. Here we
     * inflate a layout from the apps resources and then change the text view to signify the position.
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // Inflate a new layout from our resources
        View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, container, false);
        // Add the newly created View to the ViewPager
        container.addView(view);

        cursor.moveToPosition(position);
        String urlData = cursor.getString(cursor.getColumnIndexOrThrow("url"));

        WebView title = (WebView) view.findViewById(R.id.item_title);
        title.getSettings().setJavaScriptEnabled(true);
        title.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                cursor.moveToPosition(0);
                int i=0;
                int pos=0;
                while(i<cursor.getCount()){
                    cursor.moveToPosition(i);
                    if(url.equals("file:///android_asset/" + cursor.getString(cursor.getColumnIndexOrThrow("url")))){
                        pos=i;
                    }
                    i++;
                }
                mViewPager.setCurrentItem(pos);
                return true;
            }
        });
        title.loadUrl("file:///android_asset/"  + urlData + ".html" );
        return view;
    }

    /**
     * Destroy the item from the {@link android.support.v4.view.ViewPager}. In our case this is simply removing the
     * {@link android.view.View}.
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}