80,351
社区成员
发帖
与我相关
我的任务
分享
public class Book {
private String b_num;
private String ISBN;
private String borrowed;
private String ordered;
private String introduction;
public Book(String b_num, String ISBN, String borrowed, String ordered,
String introduction) {
this.b_num = b_num;
this.ISBN = ISBN;
this.borrowed = borrowed;
this.ordered = ordered;
this.introduction = introduction;
}
public Book() {
}
public String getB_num() {
return b_num;
}
public void setB_num(String b_num) {
this.b_num = b_num;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
public String getBorrowed() {
return borrowed;
}
public void setBorrowed(String borrowed) {
this.borrowed = borrowed;
}
public String getOrdered() {
return ordered;
}
public void setOrdered(String ordered) {
this.ordered = ordered;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
}
public class BookDao {
private DBUtil helper;
public BookDao(Context context)
{
helper=new DBUtil(context);
}
public List<Book> findall()
{
SQLiteDatabase db=helper.getReadableDatabase();
List<Book> books=new ArrayList<Book>();
String sql="select * from book_informathion";
Cursor cursor=db.rawQuery(sql, null);
while (cursor.moveToNext()) {
String b_num=cursor.getString(0);
String ISBN=cursor.getString(1);
String borrowed=cursor.getString(2);
String ordered=cursor.getString(3);
String introduction=cursor.getString(4);
Book book=new Book(b_num, ISBN, borrowed, ordered, introduction);
books.add(book);
System.out.println(books);
}
cursor.close();
db.close();
return books;
}
}
public class self_query extends Activity {
private ListView lv;
private List<Book> books;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.self_query);
BookDao dao=new BookDao(this);
books=dao.findall();
lv=(ListView) findViewById(R.id.lv_selfview);
lv.setAdapter(new myadapter());
//listView=(ListView) findViewById(R.id.lv_selfview);
}
private class myadapter extends BaseAdapter
{
private static final String tag = "myadapter";
//控制listview中的显示条目
@Override
public int getCount() {
return books.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(tag, "返回位置對象"+position);
TextView tView=new TextView(getApplicationContext());
tView.setTextSize(15);
tView.setTextColor(Color.BLACK);
//得到某个位置对应的book对象
Book book=books.get(position);
tView.setText(book.toString());
return tView;
}
}
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.i(tag, "返回位置對象"+position);
TextView tv1=new TextView(getApplicationContext());
tv1.setTextSize(15);
tv1.setTextColor(Color.BLACK);
//得到某个位置对应的book对象
Book book=books.get(position);
tv1.setText(book.getB_num().toString());
tv1.setText(book.getISBN().toString());
tv1.setText(book.getBorrowed().toString());
tv1.setText(book.getOrdered().toString());
tv1.setText(book.getIntroduction().toString());
System.out.println("图书编号:"+book.getB_num()+"图书是否外借:"+book.getBorrowed()+"图书介绍:"+book.getIntroduction()+"ISBN:"+book.getISBN()+"图书是否已借出:"+book.getOrdered());
return tv1;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout03"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffcc"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="@drawable/title"
>
<TextView
android:text="@string/app_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:layout_marginLeft="0dip"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:gravity="center"
>
</TextView>
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:layout_marginTop="4dip"
android:layout_marginBottom="4dip"
android:background="@drawable/navigator"
>
<TextView
android:text="@string/query_result"
android:layout_height="wrap_content"
android:layout_width="100dip"
android:textSize="18sp"
android:layout_marginLeft="10dip"
android:textColor="#FFFFFF"
android:gravity="center"
android:background="@drawable/hilight"
/>
</LinearLayout>
<ListView
android:id="@+id/lv_selfview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>