如何将下面给出的方法的返回值传递给另一个活动?
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
实际上这是 DatabasHandler 类的一个方法,我用它来获取列表视图中的所有联系人。所以我想在我的 VewContact.class 中调用这个函数(从 sqlite 数据库填充列表视图)。它的返回值为contactList。我如何在我的函数中调用它?希望你能理解.. :)
添加(查看联系人):
public class ViewContact extends Activity {
DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db.getAllContacts();
populateListView();
}
private void populateListView() {
ArrayAdapter<Contact> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.carsListView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Contact> {
public MyListAdapter() {
super(ViewContact.this, R.layout.item_view, db.getAllContacts());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
}
// Find the car to work with.
Contact contact = db.getAllContacts().get(position);
// Make:
TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
makeText.setText(contact.getID());
// Year:
TextView yearText = (TextView) itemView.findViewById(R.id.item_txtYear);
yearText.setText("" + contact.getName());
// Condition:
TextView condionText = (TextView) itemView.findViewById(R.id.item_txtCondition);
condionText.setText(""+contact.getPhoneNumber());
return itemView;
}
}}
错误(LogCat):
08-17 09:03:24.132: E/AndroidRuntime(17094): FATAL EXCEPTION: main
08-17 09:03:24.132: E/AndroidRuntime(17094): android.content.res.Resources$NotFoundException: String resource ID #0x1
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.content.res.Resources.getText(Resources.java:201)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.TextView.setText(TextView.java:2961)
08-17 09:03:24.132: E/AndroidRuntime(17094): at com.parth.targetthebudget.ViewContact$MyListAdapter.getView(ViewContact.java:51)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.AbsListView.obtainView(AbsListView.java:1593)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.ListView.measureHeightOfChildren(ListView.java:1251)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.ListView.onMeasure(ListView.java:1162)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.View.measure(View.java:8313)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.View.measure(View.java:8313)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.View.measure(View.java:8313)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.View.measure(View.java:8313)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.View.measure(View.java:8313)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.os.Handler.dispatchMessage(Handler.java:99)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.os.Looper.loop(Looper.java:130)
08-17 09:03:24.132: E/AndroidRuntime(17094): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-17 09:03:24.132: E/AndroidRuntime(17094): at java.lang.reflect.Method.invokeNative(Native Method)
08-17 09:03:24.132: E/AndroidRuntime(17094): at java.lang.reflect.Method.invoke(Method.java:507)
08-17 09:03:24.132: E/AndroidRuntime(17094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-17 09:03:24.132: E/AndroidRuntime(17094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-17 09:03:24.132: E/AndroidRuntime(17094): at dalvik.system.NativeStart.main(Native Method)
XML(项目视图):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/item_txtMake"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="32dp"
android:text="Make Shown Here"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/item_txtYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/item_txtMake"
android:layout_below="@+id/item_txtMake"
android:text="2000" />
<TextView
android:id="@+id/item_txtCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/item_txtYear"
android:layout_marginRight="16dp"
android:text="Condition shown here"
android:textSize="20dip" /></RelativeLayout>
XML(活动主要):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txtMake"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="All My Cars"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/carsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtMake"
android:layout_below="@+id/txtMake" >
</ListView>
</RelativeLayout>