簡體   English   中英

將變量從Activity傳遞到Fragment

[英]Passing a variable from Activity to Fragment

我的活動實現了一個名為MyInterface的接口,該接口具有一個名為getList的方法。 此方法獲取對象的arrayList並在最后將其返回。 我正在嘗試在另一個片段中訪問變量,並且一直在這樣做。 我研究了捆綁,意圖,包裹性,並一直在使用當前的接口路由。 如何訪問片段中的變量?

下面是我的getList方法,該方法返回稱為arrayListAndroidContacts的arraylists對象

   public ArrayList<Android_Contact> getList() {
        ArrayList<Android_Contact> arrayListAndroidContacts = new ArrayList<Android_Contact>();

        //--< get all Contacts >--
        Cursor cursor_Android_Contacts = null;
        ContentResolver contentResolver = getContentResolver();
        try {
            cursor_Android_Contacts = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        } catch (
                Exception ex
                )
        {
            Log.e("Error on contact", ex.getMessage());
        }
        if (cursor_Android_Contacts.getCount() > 0)

        {
//----< has Contacts >----
//----< @Loop: all Contacts >----
            while (cursor_Android_Contacts.moveToNext()) {
//< init >
                Android_Contact android_contact = new Android_Contact();
                String contact_id = cursor_Android_Contacts.getString(cursor_Android_Contacts.getColumnIndex(ContactsContract.Contacts._ID));
                String contact_display_name = cursor_Android_Contacts.getString(cursor_Android_Contacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//</ init >

//----< set >----
                android_contact.android_contact_Name = contact_display_name;


//----< get phone number >----
                int hasPhoneNumber = Integer.parseInt(cursor_Android_Contacts.getString(cursor_Android_Contacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {

                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI
                            , null
                            , ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?"
                            , new String[]{contact_id}
                            , null);

                    while (phoneCursor.moveToNext()) {
                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//< set >
                        android_contact.android_contact_TelefonNr = phoneNumber;
//</ set >
                    }
                    phoneCursor.close();
                }

                arrayListAndroidContacts.add(android_contact);

            }


            Collections.reverse(arrayListAndroidContacts);

            Adapter_for_Android_Contacts adapter = new Adapter_for_Android_Contacts(this, arrayListAndroidContacts);

            ListView listView_Android_Contacts = (ListView) findViewById(R.id.listview_Android_Contacts);

            listView_Android_Contacts.setAdapter(adapter);

        }
        return arrayListAndroidContacts;
    }

這是我的Fragment,當前包含接口的實例,列表視圖的適配器以及對arrayListAndroidContacts值的調用

public class Tab1Recents extends Fragment {
    MyInterface myInterface;

    public static final String MESSAGE_KEY = "message_key";

    public void onAttach(MainActivity activity) {
        super.onAttach(activity);
        try {
            myInterface = (MyInterface) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onViewSelected");
        }
    }




    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {


        Adapter_for_Android_Contacts adapter = new Adapter_for_Android_Contacts(this, arrayListAndroidContacts);

        View rootView = inflater.inflate(R.layout.tab1_recent, container, false);

        ListView recentContacts = (ListView) rootView.findViewById(R.id.recent_Android_Contacts);

        recentContacts.setAdapter(adapter);

        return rootView;
    }

    public class Adapter_for_Android_Contacts extends BaseAdapter {

        Context mContext;
        List<MainActivity.Android_Contact> mList_Android_Contacts;

        //< constructor with ListArray >
        public Adapter_for_Android_Contacts(Context mContext, List<MainActivity.Android_Contact> mContact) {
            this.mContext = mContext;
            this.mList_Android_Contacts = mContact;
        }

        public int getCount() {
            return mList_Android_Contacts.size();
        }

        public Object getItem(int position) {
            return mList_Android_Contacts.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(mContext, R.layout.contactlist_android_items, null);
            TextView textview_contact_Name = (TextView) view.findViewById(R.id.textview_android_contact_name);
            textview_contact_Name.setText(mList_Android_Contacts.get(position).android_contact_Name);
            view.setTag(mList_Android_Contacts.get(position).android_contact_Name);
            return view;
        }
    }

}

首先,您使用ContentResolver進行查詢,因此應在非UI線程中調用getList() (因此,在該方法中不得創建適配器和為ListView更新)。

然后,您可以啟動一個異步線程/任務/服務,以從活動或片段中調用getList()

一旦arrayListAndroidContacts出現,您就可以使用View.post(Runnable)來發布UI操作(創建適配器並更新ListView),或者使用BroadcastReceiverEventBus 選擇您喜歡的一種。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM