[英]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),或者使用BroadcastReceiver
或EventBus
。 选择您喜欢的一种。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.