2017-01-31 10 views
0

私のMainActivityでは、私はデータの配列リストを派生します。ここでのトリックは、このデータコレクションをリストビューで並べ替えようとしていることです。例えば、選択されているタブ(アルファベット順、年代順など)に応じてさまざまな方法でそれを行うコードがあります。Androidがアクティビティ、フラグメント、リストビュー、タブを使用している間にデータを渡す

以下は私の主な活動です。 fp_get_Android_Contacts()メソッドにあなたの時間を節約するために

public class MainActivity extends AppCompatActivity { 

    private SectionsPagerAdapter mSectionsPagerAdapter; 

    private ViewPager mViewPager; 
    private FloatingActionButton fab; 
    private final int PICK = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     fp_get_Android_Contacts(); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(Intent.ACTION_PICK, 
         ContactsContract.Contacts.CONTENT_URI); 
       // calling OnActivityResult with intenet And Some conatct for Identifie 
       startActivityForResult(intent, PICK); 
      } 
     }); 
    } 

    public class Android_Contact { 
     public String android_contact_Name = ""; 
     public String android_contact_TelefonNr = ""; 
     public int android_contact_ID = 0; 
    } 

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

     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) 

     { 

      while (cursor_Android_Contacts.moveToNext()) { 

       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)); 

       android_contact.android_contact_Name = contact_display_name; 

       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)); 

         android_contact.android_contact_TelefonNr = phoneNumber; 

        } 
        phoneCursor.close(); 
       } 

       arrayListAndroidContacts.add(android_contact); 

      } 
      Collections.reverse(arrayListAndroidContacts); 
      ListView listView_Android_Contacts = (ListView) findViewById(R.id.listview_Android_Contacts); 

      Adapter_for_Android_Contacts adapter = new Adapter_for_Android_Contacts(this, arrayListAndroidContacts); 

      listView_Android_Contacts.setAdapter(adapter); 
     } 
    } 

    public class Adapter_for_Android_Contacts extends BaseAdapter { 

     Context mContext; 
     List<Android_Contact> mList_Android_Contacts; 

     public Adapter_for_Android_Contacts(Context mContext, List<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; 
     } 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 

        Tab2AZ tab2 = new Tab2AZ(); 
        return tab2; 
       case 1: 
        Tab1Recents tab1 = new Tab1Recents(); 
        return tab1; 
       case 2: 
        Tab3Location tab3 = new Tab3Location(); 
        return tab3; 
       case 3: 
        Tab4Groups tab4 = new Tab4Groups(); 
        return tab4; 
       default: 
        return null; 
      } 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 4; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "A-Z"; 
       case 1: 
        return "RECENT"; 
       case 2: 
        return "LOCATION"; 
       case 3: 
        return "TAGS"; 
      } 
      return null; 
     } 
    } 
} 

は、主な活動のXMLであるリストビューでコンテンツを置くために、アダプタを使用して、ArrayListのグラブ。その結果、すべてのタブが同じビューを視覚的に表示します。 (MainActivityのListviewがフラグメントのリストビューをカバーしているので)私は実際にfp_get_Android_Contacts()メソッドから取得したコンテンツを一度に1つのフラグメントに表示するようにしようとしています。私は、バンドル、パーセルブル、インテント、そして最近のインターフェースを使って調べましたが、成功した実装は、私の経験レベルを考えれば達成するのは難しいでしょう。私は多くの研究をして、すでに多くのことを試していたので、何かを読むための参照ではなく、特定のアプローチに感謝します。

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View rootView = inflater.inflate(R.layout.tab1_recent, container, false); 
     return rootView; 
    } 
} 

答えて

1

これらのタブには複数のタブとそれに対応するフラグメントが必要です。右 ? 私はあなたのコードでそれを見つけることができません。 すべてのフラグメントについて、別々の.xmlファイルを生成し、各フラグメントに別々のリストビューを作成し、それに応じて各フラグメントのデータを渡します。そして、あなたは完了です。

たとえば、アルファベット順:フラグメントAlphabetSortFragmentを作成し、同じものに対して.xmlファイルとその中のリストビューまたはリサイクルビューを作成します。

+0

私は各フラグの対応するリストビューとxmlファイルを持っていますが、フラグとそれに続くリストビューにデータを渡す方法がわかりません。 – NVA

+0

素晴らしい!これでリストビューごとに別々のアダプタを用意する必要があります。3つのフラグメントと3つの対応するアダプターがあるとします。 **例:** ADAPTER_A、ADAPTER_B、ADAPTER_C。必要に応じて3つの異なるarrayListを作成し、対応するアダプタに渡します。そして、はい、あなたは**あなたの** MainActivity.java **でこれを行う必要があります。私が明確でないかどうか私に教えてください。私は現在、私のノートパソコンを持っていない、または私はあなたに全体のスニペットを与えているだろう。それを試してみてください。 –

+0

fp_get_Android_Contacts()メソッドは、arraylistを生成します。 "あなたの要件に応じてarraylist"を作成することで、あなたが何を意味するか分かりません。その断片を繰り返すことを意味し、断片の要件に合わせて変更を加えますか?どういうわけか、プロセスを単純化するためにelse文があるべきであると私は想像しています。助けてくれてありがとう。 – NVA

1

あなたMainActivityは、このインターフェイスに

public class MainActivity extends AppCompatActivity implements FragmentListener { 
//global field 
private List<Android_Contact> arrayListAndroidContacts; 
... 
//your code 
... 
//override methods of interface 
@Override 
void setArrayListAndroidContacts(List<Android_Contact> contacts){ 
this.arrayListAndroidContacts = contacts; 
} 

@Override 
List<Android_Contact> getArrayListAndroidContacts(){ 
return this.arrayListAndroidContacts; 
} 

を実装してみましょうあなたのフラグメント

public class YourFragment extends Fragment{ 

private FragmentListener mListener; 

... 
//in onCreateView 
//this will give you list 
mListener.getArrayListAndroidContacts(); 
.... 

@Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof FragmentListener) { 
      mListener = (FragmentListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement FragmentListener"); 
     } 
    } 
} 

もう一つの方法は、単にMainActivityで、あなたにarrayListAndroidContactsの公共ゲッターセッターを作成しているインタフェース

public interface FragmentListener { 
    void setArrayListAndroidContacts(List<Android_Contact> contacts); 
    List<Android_Contact> getArrayListAndroidContacts(); 
} 

を作成します。フラグメントを使用するには(MainActivity getActivity()).getArrayListAndroidContacts() ist。私は再利用のためにインタフェースメソッドを好むだろう。

ビューの変更を行わずにフィルタ選択(アルファ、クロノでソート)でデータを並べ替えたい場合は、なぜviewpagerを作成しますか?

Mainactivityのfloatingmenu/toolbarsidemenu、recyclerview(任意のリストビュー)、およびcustomadapterにフィルタメニューアクションを追加するだけです。フィルタ選択の順序に応じて、連絡先リストとnotifydatasetchangedがすべての作業を行います。

お気に入り、ビジネスなどの連絡先のカテゴリを1つの画面に表示する場合は、タブに移動します。

提案: - より良いソースコードのメンテナンスのため、カスタムBaseAdapterコードをMainActivityから分離してください。

関連する問題