使用来自游标,Viewpager的数据填充数组

时间:2014-02-07 08:25:35

标签: java arrays sqlite android-viewpager cursor

我正在尝试使用游标中的数据填充数组,但是如何?这是我的代码:

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()]);

1 个答案:

答案 0 :(得分:3)

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

以下是我的表现:

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>

希望这可以让您对自己的问题有所了解。

希望这也有助于其他观众...... :)