所以我建议你创建一个封装了imageFilename,title,author和genre的Book类(这使得排序更容易)。
public class Book{
private String imageName;
private String title;
private String author;
private String genre;
public Book(String image, String title1, String author1, String genre1)
{
imageName = image;
title = title1;
author = author1;
genre = genre1;
}
public String getImageName()
{
return this.imageName;
}
public String getTitle()
{
return this.title;
}
public String getAuthor()
{
return this.author;
}
public String getGenre()
{
return this.genre;
}
}然后在SectionFragment类中使用以下代码。这应该是我能想到的。据我所知,排序逻辑等是正确的。唯一可能出现的问题可能是由于switch语句中的参数(getArguments.getInt(SectionFragment.ARG_SECTION_NUMBER))
哦!另外,当你发布getItem(i)的代码时,你发布了这个:
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
@Override
public Fragment getItem(int i) {
Fragment fragment = new SectionFragment();
// Bundle args = new Bundle();
// args.putInt(SectionFragment.ARG_SECTION_NUMBER, i + 1);
// fragment.setArguments(args);
return fragment;
}确保取消注释最后三行,因为这些在我在下面提供的代码中的以下switch语句中很重要(以确定我们正在处理哪个片段。)
public static class SectionFragment extends Fragment {
private static ScrollView outerLayout;
private static LinearLayout innerLayout;
private static LayoutParams params;
private static ArrayList bookData = new ArrayList ();
private static String [] bookList;
private static ArrayList bookList = new ArrayList();
public SectionFragment () { }
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
params = new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
outerLayout = new ScrollView (context);
outerLayout.setLayoutParams(params);
innerLayout = new LinearLayout (context);
innerLayout.setLayoutParams(params);
innerLayout.setOrientation(LinearLayout.VERTICAL);
bookList = res.getStringArray(R.array.bookList);
for(int i = 0; i < bookList.length; i++)
{
String filename = bookList[i].split("\\|")[0];
String title = bookList[i].split("\\|")[1];
String author = bookList[i].split("\\|")[2];
String genre = bookList[i].split("\\|")[3];
Book book = new Book(filename, title, author, genre);
bookList.add(book);
}
switch(getArguments.getInt(SectionFragment.ARG_SECTION_NUMBER))
{
// I am assuming the first fragment is Title
case(1): Collections.sort(bookList, new Comparator {
public int compare(Book a, Book b)
{
return a.getTitle().compareTo(b.getTitle());
}
});
break;
case(2): Collections.sort(bookList, new Comparator {
public int compare(Book a, Book b)
{
return a.getAuthor().compareTo(b.getAuthor());
}
});
break;
case(3): Collections.sort(bookList, new Comparator {
public int compare(Book a, Book b)
{
return a.getGenre().compareTo(b.getGenre());
}
});
break;
}
for (int i = 0; i < bookList.length; i++) {
View bookView = new View (context);
bookView = inflater.inflate(R.layout.grid_cell, null);
ImageView bookCover = (ImageView) bookView.findViewById(R.id.bookCover);
TextView bookTitle = (TextView) bookView.findViewById(R.id.bookTitle);
TextView bookAuthor = (TextView) bookView.findViewById(R.id.bookAuthor);
TextView bookGenre = (TextView) bookView.findViewById(R.id.bookGenre);
try {
bookCover.setImageDrawable(Drawable.createFromStream(
context.getAssets().open(bookList.get(i).getImageName()), null));
} catch (IOException e) {
e.printStackTrace();
}
bookTitle.setText(bookList.get(i).getTitle());
bookAuthor.setText("by " + bookList.get(i).getAuthor());
bookGenre.setText(bookList.get(i).getGenre());
innerLayout.addView(bookView);
}
outerLayout.addView(innerLayout);
return outerLayout;
}
}