我在onCreate函数中创建了一个带有表(student)的数据库(mydb),然后使用单击按钮动态输入值。现在我想通过单击按钮从表学生中检索所有数据并将其显示到的列表视图
public class MainActivity extends Activity
{
String name, phone;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase("mydb", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(dbname VARCHAR, dbphone VARCHAR);");
}
public void btnaddtodb(View v)
{
EditText edtxtname = (EditText)findViewById(R.id.edtxtname);
EditText edtxtphone = (EditText)findViewById(R.id.edtxtphone);
name=edtxtname.getText().toString();
phone=edtxtphone.getText().toString();
db.execSQL("INSERT INTO student values('"+name+"','"+phone+"');");
edtxtname.setText("");
edtxtphone.setText("");
}
}
答案 0 :(得分:1)
尝试这样做......它将LinearLayout中表格中的所有值显示为列表
try{
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
Cursor allrows = mydb.rawQuery("SELECT * FROM "+ TABLE, null);
System.out.println("COUNT : " + allrows.getCount());
Integer cindex = allrows.getColumnIndex("BOOK_DATE");
Integer cindex1 = allrows.getColumnIndex("TRIP_DATE");
Integer cindex2 = allrows.getColumnIndex("LOCATION");
TextView t = new TextView(MybookingsActivity.this);
t.setText("========================================");
//Linear.removeAllViews();
Linear.addView(t);
if(allrows.moveToFirst()){
do{
LinearLayout id_row = new LinearLayout(MybookingsActivity.this);
LinearLayout book_date_row = new LinearLayout(MybookingsActivity.this);
LinearLayout trip_date_row= new LinearLayout(MybookingsActivity.this);
LinearLayout location_row= new LinearLayout(MybookingsActivity.this);
LinearLayout feedback_row= new LinearLayout(MybookingsActivity.this);
final TextView id_ = new TextView(MybookingsActivity.this);
final TextView book_date = new TextView(MybookingsActivity.this);
final TextView trip_date = new TextView(MybookingsActivity.this);
final TextView location = new TextView(MybookingsActivity.this);
final TextView sep = new TextView(MybookingsActivity.this);
final Button feedback = new Button(MybookingsActivity.this);
final String ID = allrows.getString(0);
String BOOK_DATE= allrows.getString(1);
String TRIP_DATE= allrows.getString(2);
String LOCATION= allrows.getString(3);
id_.setTextColor(Color.RED);
id_.setPadding(20, 5, 0, 5);
book_date.setTextColor(Color.RED);
book_date.setPadding(20, 5, 0, 5);
trip_date.setTextColor(Color.RED);
trip_date.setPadding(20, 5, 0, 5);
location.setTextColor(Color.RED);
location.setPadding(20, 5, 0, 5);
System.out.println("BOOK_DATE " + allrows.getString(cindex) + " TRIP_DATE : "+ allrows.getString(cindex1)+ " LOCATION : "+ allrows.getString(cindex2));
System.out.println("ID : "+ ID + " || BOOK_DATE " + BOOK_DATE + "|| TRIP_DATE : "+ TRIP_DATE+ "|| LOCATION : "+LOCATION);
id_.setText("ID : " + ID);
id_row.addView(id_);
Linear.addView(id_row);
book_date.setText("BOOK_DATE : "+BOOK_DATE);
book_date_row.addView(book_date);
Linear.addView(book_date_row);
trip_date.setText("TRIP_DATE : " + TRIP_DATE);
trip_date_row.addView(trip_date);
Linear.addView(trip_date_row);
location.setText("LOCATION : " + LOCATION);
location_row.addView(location);
Linear.addView(location_row);
feedback.setText("Feedback");
feedback.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MybookingsActivity.this,FeedbackActivity.class);
intent.putExtra("id", ID);
startActivity(intent);
}
});
feedback_row.addView(feedback);
Linear.addView(feedback_row);
sep.setText("---------------------------------------------------------------");
Linear.addView(sep);
}
while(allrows.moveToNext());
}
mydb.close();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Error encountered."+e.toString(), Toast.LENGTH_LONG).show();
}
试试吧..别忘了更改dbname,tablename和fielnames ..
答案 1 :(得分:1)
通常,您始终可以使用ArrayAdapter
在listview中显示内容。 (关于它有一个good tutorial以及你可以在互联网上找到的许多其他内容)
对于db中的内容,除了基本ArrayAdapter
之外,您还可以使用CursorAdapter
,它具有一些额外的好处,例如动态加载和自动刷新。
要使用CursorAdapter
,请让Activity
实施LoaderCallback<Cursor>
及其所需的回调。
初始化CursorAdapter
并将其设置为ListView
。
在CreateLoader(...)
方法中,查询您需要的任何内容。
请务必正确实施newView
和bindView
。
最简单的示例可能如下所示:
public class TestActivity extends Activity implements LoaderCallbacks<Cursor>{
ListView listview;
CursorAdapter cursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cursorAdapter = new CursorAdapter(this, null) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
rowView = inflater.inflate(R.layout.device_list_item, parent, false);
bindView(rowView, context, cursor);
return rowView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textView = view.findViewById(R.id.text);
textView.setText(cursor.getString(0));
}
};
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return db.query(...);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
cursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
cursorAdapter.swapCursor(null);
}
}
答案 2 :(得分:-1)
使用本教程我会帮助您。 1 GT; Android-sqlite-and-listview-example