2014-02-07 42 views
2

我想用游标中的数据填充数组,但是如何?这是我的代码:用游标数据填充数组,Viewpager

public class Tab3Up extends Activity { 

    private HipotecaDbAdapter dbAdapter; 
     private Cursor cursor; 
     private long id ; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.imageslide); 
     ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra); 
     ViewPager myPager = (ViewPager) findViewById(R.id.view_pager); 
     myPager.setAdapter(adapter); 
     myPager.setCurrentItem(0); 

     dbAdapter = new HipotecaDbAdapter(this); 
      dbAdapter.abrir(); 
      cursor = dbAdapter.getRegistro(id); 
      } 
    private int imageArra[] = { R.drawable.foto1, R.drawable.foto2, 
       R.drawable.foto3, R.drawable.foto4, }; 

我的适配器:

public class ViewPagerAdapter extends PagerAdapter { 

    Activity activity; 
    int imageArray[]; 

    public ViewPagerAdapter(Activity act, int[] imgArra) { 
     imageArray = imgArra; 
     activity = act; 
    } 

    public int getCount() { 
     return imageArray.length; 
    } 
      //MORE CODE HERE.... 

与此代码我的应用程序工作正常,但我需要改变主代码从一个sqlite bd填充数据的阵列。我的sqlite bd有一个保存所有图像路径的字段。

这样的事情?

ArrayList<Integer> array=new ArrayList<Integer>(); 
     if (cursor.moveToFirst()) { 
      do { 
       array.add(cursor.getInt(cursor.getColumnIndex(HipotecaDbAdapter.C_COLUMNA_FOTO1))); //<< pass column index here instead of i 

      } while (cursor.moveToNext()); 
     } 

     //fotos[0] = (Integer[])array.toArray(new Integer[array.size()]); 
+0

究竟是什么问题? 你无法从光标获取数组? 您无法将此数组发送到ViewPagerAdapter? – anber

+0

两者,我不知道如何处理数组,首先我需要从游标发送数据到数组,然后到viewpageradapter。最后的代码是我在这最后一次尝试 – Napster

回答

3

这是不是真的回答你的问题,但是,我也从源码中提取数据,并在视图寻呼机显示它。在这我试图拉动联系信息(姓名和电话号码)。每个联系信息将显示在不同的屏幕上。

以下是我正在做它:

Step 1: in my on create method, I am setting my adapter that I have made to display specific content. 

public class ViewTest extends Activity { 

    private List<String> testContactName = new ArrayList<String>(); 
    private List<String> testContactNumber = new ArrayList<String>(); 
    MyAdapter mAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.viewpager_layout); 

     ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); 
     mAdapter = new MyAdapter(); 
     mViewPager.setAdapter(mAdapter); 

     try { 
      getAllContacts(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 


    e.printStackTrace(); 
    } 

} 



    Step 2 : Just getting all contacts from the database and adding the values to my arraylist. 

public void getAllContacts() { 
    Cursor cursor = getContentResolver().query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      null, 
      null, 
      null, 
      ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME 
        + " COLLATE LOCALIZED ASC"); 

    // Looping through the contacts and adding them to arrayList 
    while (cursor.moveToNext()) { 
     String name = cursor 
       .getString(cursor 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     String phoneNumber = cursor 
       .getString(cursor 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

     testContactName.add(name); 
     mAdapter.notifyDataSetChanged(); 
     Log.i("Name: " + "" + "---", name); 
     testContactNumber.add(phoneNumber); 
     mAdapter.notifyDataSetChanged(); 
     Log.i("Number: " + "" + "---", phoneNumber); 
    } 

    cursor.close(); 
} 

    Step 3: Defining my adapter. 
private class MyAdapter extends PagerAdapter { 

    public MyAdapter() { 

     super(); 

    } 

    @Override 
    public int getCount() { 

     return testContactName.size(); 

    } 

    @Override 
    public boolean isViewFromObject(View collection, Object object) { 

     return collection == ((View) object); 
    } 

    @Override 
    public Object instantiateItem(View collection, int position) { 

     LayoutInflater inflater = (LayoutInflater) collection.getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View view = inflater.inflate(R.layout.viewpager_items, null); 

     TextView contactName = (TextView) view.findViewById(R.id.labelText); 
     TextView contactNumber = (TextView) view 
       .findViewById(R.id.answerText); 

     try { 
      contactName.setText(testContactName.get(position)); 
      contactNumber.setText(testContactNumber.get(position)); 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     ((ViewPager) collection).addView(view, 0); 
     return view; 

    } 

    @Override 
    public void destroyItem(View collection, int position, Object view) { 
     ((ViewPager) collection).removeView((View) view); 
     mAdapter.notifyDataSetChanged(); 

    } 

} 

}

最后,布局:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></android.support.v4.view.ViewPager> 

如果你想viewpager_items.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/background" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/background_dark" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/labelText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="30dp" 
     android:background="@android:color/background_dark" 
     android:gravity="left" 
     android:text="Loading..." 
     android:textColor="#FFFFFF" 
     android:textSize="24sp" 
     android:textStyle="bold" 
     android:typeface="sans" /> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="3dp" 
     android:background="@android:color/background_dark" /> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/answerText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="30dp" 
      android:gravity="left" 
      android:padding="5dp" 
      android:text="Loading.." 
      android:textColor="#FFFFFF" 
      android:textSize="24sp" /> 
    </ScrollView> 

</LinearLayout> 

希望这给你一些我关于你的问题。

希望这可以帮助其他观众.. :)

+0

这非常有帮助。但是当我添加几个'TextViews'和'Buttons'时,滑动速度变慢了。任何想法? –

+0

@PranjalCholadhara它的确如此,但这是旧的。我认为有很多新的库可以处理sqlite,例如Realm:https://realm.io/docs/java/latest/以及Recycler视图和CursorAdapter的实现。我已经准备好了代码,但不幸的是,根据这个问题的需要,我没有太多时间来重新创建这些东西。但是,这两个图书馆是值得检查的。.. :) – mike20132013