获取游标索引

时间:2015-01-09 14:32:47

标签: android sqlite cursor

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

政治是我必须寻找索引的价值,因为索引2从零开始计算。 表网址 id,title," url" 1,测试,"测试" 2,test2," testurl2" 5,"政治事物"," politicurl" 字符串网址="政治";

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

我使用for循环吗?有了这个 cursor.getString(cursor.getColumnIndexOrThrow(" URL&#34));

我是否含糊不清?对不起,这只是一个noodeling。 任何帮助表示赞赏。

Iam想到了这一点,无济于事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);
                }
                }

1 个答案:

答案 0 :(得分:0)

它确实有效,但看起来很糟糕。 我试图让这个android选项卡片段脚本工作:

然后还要在html页面中获取超链接以对选项卡导航作出反应。 使用webview上的shouldOverrideUrlLoading函数: 它按原样工作:但它确实是一个丑陋的黑客;) 无论如何,谢谢你的帮助。

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

}
相关问题