1

好的。所以我正在制作一个小的电子书商店应用程序。我在 strings.xml 中有一个包含书籍列表的字符串数组。每个数组项包含四段数据,包括其封面图像的路径、标题、作者和流派。在下面的代码中,我为数组中的每本书创建了一个 View 对象,并将它们打印到屏幕上。我目前使用的是 Tabs+Swipe 布局,共有三个选项卡:Title、Author 和 Genre。我想要做的是按标题、作者或流派对视图数组进行排序,具体取决于选择的选项卡。任何人都知道我可以做到这一点的任何方式吗?

public static class SectionFragment extends Fragment {
    private static ScrollView outerLayout;
    private static LinearLayout innerLayout;
    private static LayoutParams params;
    private static ArrayList <String []> bookData = new ArrayList <String []> ();
    private static String [] bookList;

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

            bookData.add(bookList[i].split("\\|"));
            try {
                bookCover.setImageDrawable(Drawable.createFromStream(
                        context.getAssets().open(bookData.get(i)[0]), null));
            } catch (IOException e) {
                e.printStackTrace();
            }
            bookTitle.setText(bookData.get(i)[1]); 
            bookAuthor.setText("by " + bookData.get(i)[2]);
            bookGenre.setText(bookData.get(i)[3]);

            innerLayout.addView(bookView);
        }

        outerLayout.addView(innerLayout);
        return outerLayout;
    }
}
4

3 回答 3

0

bookList 包含整个数据,因此如果作者选择根据作者姓名重新排列您的 booklist 数组,则只需根据标准创建排序方法。

于 2012-07-30T19:11:05.930 回答
0
  1. 您必须在运行时以编程方式修改您的字符串数组。得到你的完整阵列bookList = res.getStringArray(R.array.bookList);
  2. 执行简单的字符串操作并将字符串拆分为最多 3 个数组,一个用于标题、作者和流派,您可以使用 Arrays.sort() 按字母顺序对它们进行排序。当然,你需要在这里使用一个循环和一些编程逻辑,所以当你按标题排序时,例如:bookTitleByTitle[0] 必须对应 bookAuthorByTitle[0] 和 bookGenreByTitle[0]。顺便说一句,使用 Arrays.sort() 会破坏您的原始数组,因此请复制您的数组。(不是克隆!)。
  3. 按作者和流派重复。

这将是我的方法。

于 2012-07-30T19:49:58.707 回答
0

所以我建议你创建一个 Book 类,它封装了 imageFilename、标题、作者和流派(这使得排序更容易)。

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 <String []> bookData = new ArrayList <String []> ();
private static String [] bookList;
private static ArrayList<Book> bookList = new ArrayList<Book>();

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<Book> {
    public int compare(Book a, Book b)
    {
     return a.getTitle().compareTo(b.getTitle());
    }
    });
    break;

    case(2): Collections.sort(bookList, new Comparator<Book> {
    public int compare(Book a, Book b)
    {
     return a.getAuthor().compareTo(b.getAuthor());
    }
    });
    break;

    case(3): Collections.sort(bookList, new Comparator<Book> {
    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;
}
}
于 2012-07-30T21:20:58.117 回答