问题描述
我是从数据库试图显示随机数据分成两个textviews(TextView1显示名称,TextView2显示PHONENUMBER)当我点击下一步按钮,但我不知道我能做到这一点。请帮忙!
还有我使用了一些类和XML文件(我在androidhive.info引用):
公共类数据库处理器扩展SQLiteOpenHelper { //所有静态变量
//数据库版本
私有静态最终诠释DATABASE_VERSION = 1; //数据库名称
私有静态最后弦乐DATABASE_NAME =contactsManager; //联系人表名
私有静态最后弦乐TABLE_CONTACTS =接触; //联系方式表列名
私有静态最后弦乐KEY_ID =ID;
私有静态最后弦乐KEY_NAME =名;
私有静态最后弦乐KEY_PH_NO =PHONE_NUMBER; 公共数据库处理器(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
} //创建表
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
字符串CREATE_CONTACTS_TABLE =CREATE TABLE+ TABLE_CONTACTS +(
+ KEY_ID +INTEGER PRIMARY KEY,+ KEY_NAME +TEXT
+ KEY_PH_NO +TEXT+);
db.execSQL(CREATE_CONTACTS_TABLE);
} //数据库升级
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
如果存在//删除旧的表
db.execSQL(DROP TABLE IF EXISTS+ TABLE_CONTACTS); //再次创建表
的onCreate(DB);
} / **
*所有CRUD(创建,读取,更新,删除)操作
* / //添加新联系人
无效的addContact(联系方式联系我们){
SQLiteDatabase分贝= this.getWritableDatabase(); ContentValues值=新ContentValues();
values.put(KEY_NAME,contact.getName()); // 联系人姓名
values.put(KEY_PH_NO,contact.getPhoneNumber()); // 联系电话 //插入行
db.insert(TABLE_CONTACTS,空,价值);
db.close(); //关闭数据库连接
} //获取单触点
联系getContact(INT ID){
SQLiteDatabase分贝= this.getReadableDatabase(); 光标光标= db.query(TABLE_CONTACTS,新的String [] {KEY_ID,
KEY_NAME,KEY_PH_NO},KEY_ID +=?,
新的String [] {将String.valueOf(ID)},NULL,NULL,NULL,NULL);
如果(指针!= NULL)
cursor.moveToFirst(); 联系方式联系我们=新的联系人(的Integer.parseInt(cursor.getString(0)),
cursor.getString(1),cursor.getString(2));
//返回接触
返回接触;
} //获取所有联系人
公开名单<联系与GT; getAllContacts(){
清单<联系与GT; contactList =新的ArrayList<联系与GT;();
//选择所有查询
字符串selectQuery =SELECT * FROM+ TABLE_CONTACTS; SQLiteDatabase分贝= this.getWritableDatabase();
光标光标= db.rawQuery(selectQuery,NULL); //通过所有行循环,并增加列表
如果(cursor.moveToFirst()){
做{
联系方式联系我们=新的联系人();
contact.setID(的Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
//添加联系人名单
contactList.add(接触);
}而(cursor.moveToNext());
} //返回联系人列表
返回contactList;
} //更新单触点
公众诠释updateContact(联系方式联系我们){
SQLiteDatabase分贝= this.getWritableDatabase(); ContentValues值=新ContentValues();
values.put(KEY_NAME,contact.getName());
values.put(KEY_PH_NO,contact.getPhoneNumber()); //更新行
返回db.update(TABLE_CONTACTS,价值观,KEY_ID +=?,
新的String [] {将String.valueOf(contact.getID())});
} //删除单个联系人
公共无效deleteContact(联系方式联系我们){
SQLiteDatabase分贝= this.getWritableDatabase();
db.delete(TABLE_CONTACTS,KEY_ID +=?,
新的String [] {将String.valueOf(contact.getID())});
db.close();
}
//获取联系人计数
公众诠释getContactsCount(){
字符串countQuery =SELECT * FROM+ TABLE_CONTACTS;
SQLiteDatabase分贝= this.getReadableDatabase();
光标光标= db.rawQuery(countQuery,NULL);
cursor.close(); //返回计数
返回cursor.getCount();
} }
Contact.java类:
公共类联系{ //私有变量
INT _id;
字符串_name;
串_phone_number; //空的构造
公共联系(){ }
//构造
公共联系(INT ID,字符串名称,字符串_phone_number){
this._id = ID;
this._name =名称;
this._phone_number = _phone_number;
} //构造
公众联系(字符串名称,字符串_phone_number){
this._name =名称;
this._phone_number = _phone_number;
}
//获取ID
公众诠释的getID(){
返回this._id;
} //设置ID
公共无效SETID(INT ID){
this._id = ID;
} //获取名称
公共字符串的getName(){
返回this._name;
} //设置名称
公共无效setname可以(字符串名称){
this._name =名称;
} //获取电话号码
公共字符串getPhoneNumber(){
返回this._phone_number;
} //设置电话号码
公共无效setPhoneNumber(字符串PHONE_NUMBER){
this._phone_number = PHONE_NUMBER;
}
}
AndroidSQLiteTutorialActivity.java类:
公共类AndroidSQLiteTutorialActivity延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); 数据库处理器DB =新的数据库处理器(本); / **
* CRUD操作
* * /
//插入联系人
Log.d(插入,插入..);
db.addContact(新联系(拉维,91亿));
db.addContact(新联系(SRINIVAS,9199999999));
db.addContact(新联系(汤米,9522222222));
db.addContact(新联系(KARTHIK,9533333333)); //读取所有联系人
Log.d(读,读所有联系人..);
清单<联系与GT;接触= db.getAllContacts(); 对于(联系CN:触点){
字符串日志=ID:+ cn.getID()+,名称:+ cn.getName()+,电话:+ cn.getPhoneNumber();
//写入联系人登录
Log.d(姓名,日志); }
}
私人无效contactList(){
//设置与当前讨论的问题文本 数据库处理器DB =新的数据库处理器(本); / **
* CRUD操作
* * /
//插入联系人
Log.d(插入,插入..);
db.addContact(新联系(拉维,91亿));
db.addContact(新联系(SRINIVAS,9199999999));
db.addContact(新联系(汤米,9522222222));
db.addContact(新联系(KARTHIK,9533333333)); //读取所有联系人
Log.d(读,读所有联系人..);
清单<联系与GT;接触= db.getAllContacts();
TextView的名字=(的TextView)findViewById(R.id.textView1);
对于(联系CN:触点){
字符串名称1 =ID:+ cn.getID()+,名称:+ cn.getName()+,电话:+ cn.getPhoneNumber();
//写入联系人登录
Log.d(姓名:名1); } }}< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:背景=@机器人:彩色/白><的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=大文本
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?<的TextView
机器人:ID =@ + ID / textView2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=大文本
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge?<按钮
机器人:ID =@ + ID /下一个
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=按钮/>< / LinearLayout中>
您需要设置 setOnClickListener 的按钮做按钮点击和使用计数器变量一些行动显示按钮单击下一步值:
列表<联系与GT;接触= db.getAllContacts();
TextView的名字=(的TextView)findViewById(R.id.textView1); 按钮buttonnext =(按钮)findViewById(R.id.next);
buttonnext.setOnClickListener(onclickListener);
INT listcounter = 0;私人OnClickListener onclickListener =新OnClickListener(){
@覆盖
公共无效的onClick(最终查看V){
如果(listcounter&下; contacts.size()){
联系CN = contacts.get(listcounter); name.setText();
name.append(ID:+ cn.getID());
name.append(,名称:+ cn.getName());
name.append(,电话:+ cn.getPhoneNumber()); listcounter ++;
}
其他{
//此处复位计数器
listcounter = 0;
}
}
};
I'm trying display random data from database into two textviews (TextView1 display NAME, TextView2 display PHONENUMBER) when I click on next button, but I don't know how can I do it. please help!There are some class and xml file I'm using (I referenced at androidhive.info):
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
return contact;
}
// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
Contact.java class :
public class Contact {
//private variables
int _id;
String _name;
String _phone_number;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String name, String _phone_number){
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
// constructor
public Contact(String name, String _phone_number){
this._name = name;
this._phone_number = _phone_number;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
}
AndroidSQLiteTutorialActivity.java class :
public class AndroidSQLiteTutorialActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Ravi", "9100000000"));
db.addContact(new Contact("Srinivas", "9199999999"));
db.addContact(new Contact("Tommy", "9522222222"));
db.addContact(new Contact("Karthik", "9533333333"));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
private void contactList() {
//set the question text from current question
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Ravi", "9100000000"));
db.addContact(new Contact("Srinivas", "9199999999"));
db.addContact(new Contact("Tommy", "9522222222"));
db.addContact(new Contact("Karthik", "9533333333"));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
TextView name = (TextView) findViewById(R.id.textView1);
for (Contact cn : contacts) {
String name1 = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
Log.d("Name: ", name1);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
you will need to set setOnClickListener for button to do some action on Button click and use and Counter variable to show next value on button click as:
List<Contact> contacts = db.getAllContacts();
TextView name = (TextView) findViewById(R.id.textView1);
Button buttonnext = (Button) findViewById(R.id.next);
buttonnext.setOnClickListener(onclickListener);
int listcounter=0;
private OnClickListener onclickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
if(listcounter<contacts.size()){
Contact cn=contacts.get(listcounter);
name.setText("");
name.append("Id: "+cn.getID());
name.append(" ,Name: "+cn.getName());
name.append(",Phone: "+cn.getPhoneNumber());
listcounter++;
}
else{
//Reset Counter here
listcounter=0;
}
}
};
这篇关于从数据库到TextView中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!