2017-02-02 28 views
0

我從我的Activity派生了一個ArrayList數據,該數據列表在一個名爲getList()的方法中實現了一個接口。
我有必要的適配器來連接數據並安排顯示。我也使用我的解決方案中的選項卡,併爲每個創建碎片。每個選項卡都包含一個ListView,我希望將ListView的不同形式的數據ArrayList傳遞給它。在另一個選項卡/片段中爲ListView使用ArrayAdapter

我已經研究過沒有成功的捆綁,參數,可下載路線。目前實現我的任務的範例是使用接口訪問不同片段中的數據。

我已經做了很多的閱讀和嘗試了很多路線,但我正在努力用ListViews填充我的Activity中的數據。

我以前將數據傳遞給我在activity_main.xml中添加的ListView,但結果是它覆蓋了其他選項卡/ Fragments上的其他顯示,因此無論點擊哪個選項卡,該列表仍然是相同。

我希望能夠點擊不同的選項卡,並根據選項卡名稱(即:按字母順序排列,按時間順序排列)按照我希望的順序查看來自Activity的數據列表。任何幫助真誠讚賞。

下面是我的代碼

public class MainActivity extends AppCompatActivity implements MyInterface{ 

    private Tab1Recents fragment; 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link FragmentPagerAdapter} derivative, which will keep every 
* loaded fragment in memory. If this becomes too memory intensive, it 
* may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
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); 
    getList(); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    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 ArrayList<Android_Contact> getList() { 
    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.sort(arrayListAndroidContacts, new Comparator<Android_Contact>() { 
      @Override 
      public int compare(Android_Contact name2, Android_Contact name1) { 
       return name1.android_contact_Name.compareTo(name2.android_contact_Name); 
      } 
     }); 
     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; 
} 


public class Adapter_for_Android_Contacts extends BaseAdapter { 

    Context mContext; 
    List<Android_Contact> mList_Android_Contacts; 

    //< constructor with ListArray > 
    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); 
} 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
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; 
    } 
} 

一個例子片段看起來像之下,而其他選項卡/片段只是功能在其中一個ListView這

public class Tab1Recents extends Fragment { 
     MyInterface myInterface; 

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

     View rootView = inflater.inflate(R.layout.tab1_recent, container, false); 
     return rootView; 
    } 
} 

活動XML圖片。

當前的結果在預覽或運行中是沒有ListView的。所有其他的XML標籤是可見和可操作性,但沒有任何誇大其列表視圖的是什麼,我想實現

layout

+0

爲什麼你的ViewPager包含一個ListView? –

回答

0

當前結果預覽列表視圖不運行或全部...其他的XML標籤......沒有任何的列表視圖的充氣這就是我想實現

您需要的片段佈局內的ListView控件,所以tab1_recent.xml,例如,沒有內部的ViewPager的activity_main.xml

ViewPager需要一個像ListView一樣的適配器來顯示數據。就像你在XML中有這個一樣。你不應該期望要顯示的列表佈局的內部的TextView的......

<ListView> 
    <TextView /> 
</ListView> 

至於從活動加載數據的片段......爲什麼呢?每個片段是否包含完全相同的列表以顯示在每個列表視圖的適配器中?

只需加載片段中的數據。如果你必須從活動中獲取數據,那麼你可以有類似的東西

private List data; 
// someAdapter 
// someListView 

public void onAttach(Context c) { 
    super.onAttach(c); 

    // needing to cast the context is a bad-design 
    if (c instanceof MainActivity) { 
     data = ((MainActivity) c).getList(); 
     if (data != null) { 
      // someAdapter = new adapter(data); 
      // someListView.setAdapter(someAdapter); 
     } 
    } 
} 
+0

片段也都有列表視圖。 ViewPager中的ListView是因爲這是我能夠接近理想結果的一種方式。這些碎片以不同的排列方式保存相同的數據,這是通過使用比較器完成的。從片段派生數據不會更有效,因爲我仍然必須與另一個片段共享數據。 – NVA

+0

好吧,就像我說的,你可以調用getActivity()。getList(),但是這對我來說似乎是一個糟糕的設計 –

+0

這樣做的最真實的方式是什麼? – NVA

相關問題