java - 用光标、Viewpager 中的数据填充数组

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

最佳答案

这并不是您问题的真正答案,但是,我还从 sqlite 中提取数据并将其显示在 View 分页器中。在此我尝试提取联系信息(姓名和电话号码)。每个联系信息都会显示在不同的屏幕上。

这是我的做法:

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>

希望这能让您对您的问题有所了解。

希望这也能帮助其他观众......:)

关于java - 用光标、Viewpager 中的数据填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21622705/

相关文章:

java - 如何在Web Service Rest中获取数组JSON?

java - 从结果集中创建复杂对象

sqlite - SQLite语法错误

database - Flutter SQLite 没有这样的列

java - 结合快速排序和中值选择算法

java - 在 Play 框架和安全社交中获取绝对 URL

javascript - 比较 2 个可观察数组并使用 knockout 将匹配值插入第三个数组

C语言动态数组realloc导致Heap block错误信息

Java/Android 从xml中获取数组

java - 如何返回字符串数组