2017-02-05 10 views
0

My Activityは、getListというメソッドを持つMyInterfaceというインタフェースを実装しています。このメソッドはオブジェクトのarrayListを取得し、最後にそれを返します。私は別の断片の変数にアクセスしようとしており、そのために苦労しています。私は、バンドル、インテント、パーセルブルを調べ、現在のインターフェイスルートを使用しています。私のフラグメント内の変数にアクセスするにはどうすればいいですか?以下はアクティビティからフラグメントへの変数の受け渡し

のArrayListのオブジェクトを返す私のGETLIST方法は、これは、現在のインターフェイスの私のインスタンスが含まれている私の断片、ListViewコントロールとの呼び出しのための私のアダプタである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; 
     } 
    } 

} 
+0

SQLiteデータベースにリストを格納する方法もあります。 –

+0

@ Code-Apprentice長期的に見ると、これは最善のルートと思われます。どのようなチュートリアルや、Android/Javaフレームワーク内でそれを使用する方法についてのリード? – NVA

+0

フラグメントを作成するときにリストを引数に入れないのはなぜですか? onCreateViewメソッドでリストを使用すると、クエリデータがワーカースレッド内に存在する必要があるため、メソッド内でgetList()を呼び出すべきではありません。 – AssIstne

答えて

0

まず、あなたが照会するContentResolverを使用するので、の値は、UI以外のスレッドで呼び出される必要があります(ListViewのアダプタと更新の作成は、このメソッド内にあってはいけません)。

次に、非同期スレッド/タスク/サービスを開始して、アクティビティまたはフラグメントからgetList()を呼び出すことができます。が来る

一度arrayListAndroidContacts、あなたは(アダプタと更新リストビューを作成する)UI操作を投稿、またはBroadcastReceiverを使用するか、またはEventBusを使用するView.post(Runnable)を使用することができます。あなたが好むものを選んでください。

+0

私は断片化した部分にgetListメソッド全体を追加しようとしましたが、必要なステージメントを持っていてもContentResolverは慣れていません。理由を知っている? – NVA

関連する問題