Android 获取游标的索引

Android 获取游标的索引,android,sqlite,cursor,Android,Sqlite,Cursor,如何从光标获取索引值?不是id而是从零开始的位置 使用字符串值 政治是我必须寻找的指数,指数2从零开始计数。 表URL id,标题,“url” 1,测试,“testurl” 2,test2,“testurl2” 5、“政治题材”、“政治URL” 字符串url=“politics” idd要查询光标中的字符串url并检索索引或位置 我使用for循环吗?用这个 cursor.getString(cursor.getColumnIndexOrThrow(“url”) 我是在含糊其辞吗?对不起,我只是不

如何从光标获取索引值?不是id而是从零开始的位置 使用字符串值

政治是我必须寻找的指数,指数2从零开始计数。 表URL id,标题,“url” 1,测试,“testurl” 2,test2,“testurl2” 5、“政治题材”、“政治URL” 字符串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);
                }
                }
int pos=0;
cursor.moveToFirst();

对于(inti=0;i来说,它很有效,但看起来很可怕。 我试图让这个android选项卡片段脚本正常工作:

然后还可以获取html页面中的超链接,以响应选项卡导航。 使用Web视图上的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);
    }

}
class SamplePagerAdapter扩展了PagerAdapter{
Cursor=TabActivity.Cursor;
/**
*@返回要显示的页数
*/
@凌驾
public int getCount(){
返回TabActivity.cursor.getCount();
}
/**
*@return true,如果从{@link#instanceItem(android.view.ViewGroup,int)}返回的值是
*与添加到{@link android.support.v4.view.ViewPager}的{@link android.view.view}相同的对象。
*/
@凌驾
公共布尔值isViewFromObject(视图,对象o){
返回o==视图;
}
//开始包括(pageradapter_getpagetitle)
/**
*返回{@code position}处项目的标题。这与此方法的作用一样重要
*返回是{@link SlidingTabLayout}中显示的内容。
*
*在这里,我们使用位置值构造一个,但对于实际应用,标题应该是
*参考项目的内容。
*/
@凌驾
公共字符序列getPageTitle(int位置){
字符串Year=“0”;
光标。移动位置(位置);
年份=cursor.getString(cursor.getColumnIndexOrThrow(“标题”);
回归年;
}
//结束\包括(pageradapter \ getpagetitle)
/**
*实例化{@linkandroid.view.view},它应该显示在{@code position}上
*从应用程序资源中展开布局,然后更改文本视图以表示位置。
*/
@凌驾
公共对象实例化项(视图组容器,int位置){
//利用我们的资源扩大新的布局
视图=getActivity().GetLayoutFlater().充气(R.layout.pager_项,容器,false);
//将新创建的视图添加到ViewPager
container.addView(视图);
光标。移动位置(位置);
字符串urlData=cursor.getString(cursor.getColumnIndexOrThrow(“url”));
WebView title=(WebView)view.findviewbyd(R.id.item\u title);
title.getSettings().setJavaScriptEnabled(true);
title.setWebViewClient(新WebViewClient(){
@凌驾
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url)
{
光标移动位置(0);
int i=0;
int pos=0;

虽然(我想那不是简单的
pos=i;
?(还有,你的字符串比较是错误的,不起作用)而且我认为你应该在分配
pos=i
后返回,是的,你是对的,我正在胡闹,但是已经修复了。谢谢支持。