我有一个TextView
,它将显示数据库中的所有内容。由于我不知道如何根据数据库生成TextView
的数量,我想知道是否有任何方法在一个项目字符串后设置可见行。例如,
List of items
book1 love yourself
total of 3
book2 love you
total of 2
正如你所看到的,如果我将它全部显示在一个TextView
中,很难确定单独的书籍,所以我想知道我能做些什么来分开它们。
答案 0 :(得分:0)
public ArrayList<String> getAllBooks(){
ArrayList<String> alArrayList = new ArrayList<String>();
Database db = getWritableDatabase();
Cursor cursor = db.query("BOOKS", null, null, null, null, null, null );
if (cursor!=null && cursor.moveToFirst()) {
do{
String book = cursor.getString(cursor.getColumnIndex("book_name"));
alArrayList.add(book);
}while(cursor.moveToNext());
db.close();
}
return alArrayList;
}
public void setBooks(){
ArrayList<String> bookNames = new YourDatabseName(this, version).getAllBooks();
LinearLayout layout = (LinearLayout) findViewById(R.id.bookslayout);
LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView heading = new TextView(this);
heading.setText("List of items");
heading.setLayoutParams(textViewLayoutParams);
layout.addView(textView);
if(bookNames.size()>0){
for(int i = 0; i<=bookNames.size();i++){
TextView book= new TextView(this);
book.setText("Book "+ (i+1) +" "+bookNames.get(i).toString());
book.setLayoutParams(textViewLayoutParams);
layout.addView(textView);
}
}
TextView footer= new TextView(this);
footer.setText("Total of "+bookNames.size());
footer.setLayoutParams(textViewLayoutParams);
layout.addView(textView);
}
<LinearLayout
android:id="@+id/bookslayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>