【问题标题】:Passing a variable from Activity to Fragment将变量从 Activity 传递到 Fragment
【发布时间】:2017-02-05 01:34:39
【问题描述】:

我的 Activity 实现了一个名为 MyInterface 的接口,该接口有一个名为 getList 的方法。此方法获取对象的 arrayList 并在最后返回它。我正在尝试访问另一个片段中的变量并且一直在努力这样做。我已经研究过捆绑、意图、包裹并一直在使用当前的接口路线。如何访问 Fragment 中的变量?

下面是我的 getList 方法,它返回名为 arrayListAndroidContacts

的数组列表对象
   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;
    }

这是我的片段,它当前包含我的接口实例、我的列表视图适配器和对 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;
        }
    }

}

【问题讨论】:

  • 一种方法是将列表存储在 SQLite 数据库中。
  • @Code-Apprentice 从长远来看,我怀疑这是最好的路线。有关如何在 Android/Java 框架中使用它的任何教程或线索?
  • 为什么在创建片段时不将列表放入参数中?您在 onCreateView 方法中使用列表,您不应该在方法中调用 getList(),因为查询数据应该在工作线程中。
  • 我建议谷歌。

标签: java android android-fragments interface


【解决方案1】:

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

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

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

【讨论】:

  • 我尝试将整个 getList 方法添加到我的片段中,但是 ContentResolver 是不熟悉的,即使我有必要的使用状态。知道为什么吗?
猜你喜欢
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多