gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:26:18 25 4
gpt4 key购买 nike

我正在尝试用游标中的数据填充一个数组,但是怎么做呢?这是我的代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com