2015-01-09 3 views
0

인덱스 값을 얻으려면 어떻게해야합니까? id가 아니라 커서 위치에서 0을 기준으로 한 위치 문자열 값이 있습니까?커서 인덱스 가져 오기

정치는 인덱스 2가 0 인 인덱스를 찾는 가치입니다. 표 URL을 ID, 제목, "URL" 1, 테스트, "testurl" 2, TEST2, "testurl2" 5, "정치 물건", "politicurl" 문자열 URL = "정치";

IDD는

내가 루프를 사용합니까 인덱스 또는 위치를 문자열 URL에 대한 커서를 조회하고 검색 하시겠습니까? this with 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); 
       } 
       } 
+0

단순히 'pos = i;'가 아니어야합니까? (또한 문자열 비교가 잘못되어 작동하지 않을 것입니다.) – njzk2

+0

또한'pos = i'를 할당 한 후에 반환해야한다고 생각합니다. – Blackbelt

+0

맞아요, 맞아요. 고맙습니다. – Koenraad72

답변

0

잘 작동하지만 끔찍한 것처럼 보입니다. 이 안드로이드 탭 조각 스크립트를 작동 시키려고했습니다 :

그리고 나서 탭 탐색에 반응하기 위해 하이퍼 링크를 HTML 페이지에서 얻으려고합니다. 은 웹 뷰에서 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); 
    } 

}