android中如何创建字符串数组对象数组对象,Android:按三个字符串数组之一对对象列表进行排序...

这篇博客介绍如何创建一个Book类来存储书籍的图片、标题、作者和类型信息,并在SectionFragment中根据传入的参数对书籍进行排序。通过创建一个Comparator并结合switch语句,实现了按标题、作者和类型的排序。博客还展示了如何在GridView中显示书籍信息,并从资源文件加载数据。
摘要由CSDN通过智能技术生成

所以我建议你创建一个封装了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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值