步骤:
1.在layout中写一个LinearLayout布局,并且有id
2.MainActivity中获取数据库的所有数据
3.把数据中中的数据,通过TextVew添加到LinearLayout对象
<span style="font-size:14px;"><ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_list">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
</ScrollView></span>
<span style="font-size:14px;">package com.sqf.sql;
import java.util.List;
import com.sqf.sql.dao.PersonDao;
import com.sqf.sql.entities.Person;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout llList = (LinearLayout) findViewById(R.id.ll_list);
PersonDao dao = new PersonDao(this);
List<Person> personList = dao.queryAll(); //获取表中的所有数据
if(personList != null){
TextView tv;
for(Person person:personList){
//向线性布局中添加一个textView
tv = new TextView(this);
tv.setText(person.toString());
tv.setTextSize(18);
llList.addView(tv);
}
}
}
}
</span>
<span style="font-size:14px;">package com.sqf.sql.entities;
public class Person {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public Person(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Person() {
super();
}
}
</span>
数据库操作请参考我的其他文章