2013-08-17 46 views
0

如何獲得下面給出的方法的返回值到另一個活動?如何獲得給另一個活動的方法的返回值?

// 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數據庫填充listview)中調用這個函數。它的返回值爲contactList。我如何在我的功能中調用它? 希望你明白.. :)

ADD(ViewContact):

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; 

     } 

    }} 

ERROR(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(item_view):

<?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> 
+0

爲什麼不創建另一個類,如DatabaseHandle.class,並把你的getAllContacts()給它。當你想使用這個方法時,你創建一個新的DatabaseHandle實例並調用getAllContacts()方法。 – hieuxit

+0

但我能夠稱它爲DatabaseHandler db = new DatabaseHandler(this);然後db.getAllContacts();但我怎麼能在listview中獲得返回值? – arraystack

+0

你必須這樣做:'List cont = db.getAllContacts();'然後你可以用它來填充你的列表視圖。你是否也可以在創建ListView的地方顯示代碼,並嘗試使用此列表設置適配器,這將返回。 –

回答

0

列表<聯繫> allcontacts = db.getAllContacts();

+0

任何簡報如何或爲什麼? – arraystack

相關問題