将变量从Activity传递到Fragment

时间:2017-02-05 01:34:40

标签: java android android-fragments interface

My Activity实现了一个名为MyInterface的接口,该接口有一个名为getList的方法。此方法获取对象的arrayList并在结尾处返回它。我试图访问另一个片段中的变量,并一直在努力这样做。我已经研究了捆绑,意图,parcelables并且一直在使用当前的接口路由。如何访问片段中的变量?

下面是我的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;
    }

这是我的片段,它目前包含我的界面实例,我的listview适配器和对 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;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

首先,使用ContentResolver进行查询,因此应在非UI线程中调用getList()(因此,ListView的适配器和更新的创建不得在此方法中。

然后,您可以启动异步线程/任务/服务,以从活动或片段中调用getList()

一旦 arrayListAndroidContacts 到来,您可以使用View.post(Runnable)发布UI操作(创建适配器并更新ListView),或使用BroadcastReceiver,或使用EventBus 。选择一个你喜欢的。