简体   繁体   中英

Fill array with data from cursor, Viewpager

I am trying to fill an array with data from cursor, but how? This is my code:

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

And my Adapter:

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....

with this code my app works fine, but I need to change the main code to fill the array with data from an sqlite bd . My sqlite bd have a field that saves the path of all images.

Something like this?

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

This is not really answer to your question but, I am also pulling data from the sqlite and displaying it in the view pager. In this I am trying to pull the contact info(Name and phone number). Every contact info will be shown in different screens.

Here's how I am doing it:

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

    }

}

}

And lastly, the layout:

<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>

If you want the 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>

Hope this gives you some idea about your question.

Hope this helps other viewers too..:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM