我已经检查this question on SO我基本上有同样的问题了。
不同之处在于我没有在我的项目中使用适配器类,我只有一个填充了我的片段的 TabActivity.java (android studio默认值)。 / p>
我已经按照问题答案中的所有步骤进行了操作,但是我不理解第三步。
3.适配器代码(在我的情况下是Adapter_Contacts.java)
我应该把这段代码放在哪里,因为在我的情况下我不使用任何适配器类
这是我在tabactivity.java中的片段适配器
public class SectionsPagerAdapter extends FragmentPagerAdapter{
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position){
case 0:
return new BrowseTitle();
case 1:
return new ChatHistory();
default:
break;
}
return null;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
这是我在tabactivity中填充的片段之一
public class BrowseTitle extends Fragment {
private DatabaseReference mRootRef;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> list_of_thread = new ArrayList<>();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
//Overriden method onCreateView
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_browse_title, container, false);
//this is the logic
mRootRef = FirebaseDatabase.getInstance().getReference().child("tdesc");
listView = (ListView) v.findViewById(R.id.titleList);
arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1,list_of_thread){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view =super.getView(position, convertView, parent);
TextView textView=(TextView) view.findViewById(android.R.id.text1);
//changin text color
textView.setTextColor(Color.BLACK);
return view;
}
};
listView.setAdapter(arrayAdapter);
public void beginSearch(String query) {
Log.e("QueryFragment", query);
arrayAdapter.getFilter().filter(query);
}
}
有没有办法可以正确实现?