您的位置:首页 > 移动开发 > Android开发

用一个小实例说明android运用SqLite数据库的方法-唐诗三百首

2013-08-04 14:19 253 查看
目前初学android,也许很多人对于SQLite数据库的使用不是很清楚,主要对于查询这方面,直接先上图。

1.主界面,直接查询出所有数据



2,按类型查询





3,按作者查询



4,点击一首诗,出现对于的诗的完整信息



5,点击译文,注解都会再下面插入,再次点击撤销




可以上下拖动



下面我们就开始上代码和数据库建立


Activity:

MianActivity的activity_main布局,放入一个TabHost,TabHost分别跳转到3个Activity-全部(AllActiity),按类型(TypeActivity),按作者(PoetActivity)。

按类型(TypeActivity):两个类型分别跳转到-五言绝句(Type5Activity),七言绝句(Type7Activity)

按作者(PoetAtivity):加入一个自动文本AutoCompleteTextView进行搜索。

数据库:Poet(database)下有2个表:poet表( _id;poetname;poemname;poemtype;) content表(_id;poemname;poetname;poemcontent;poemyiwen;poemzhujie;)

实体类:

Poet.java:content.java

SQLiteOpenHelper:DBOpenHelper.java创建数据库

Dao.java:对表中数据进行操作的方法类

Poet.java:

package com.example.android_1800_sqlite1;
public class Poet {
int _id;
String poetname;
String poemname;
String poemtype;
public Poet() {
super();
// TODO Auto-generated constructor stub
}
public Poet(int _id, String poetname, String poemname, String poemtype) {
super();
this._id = _id;
this.poetname = poetname;
this.poemname = poemname;
this.poemtype = poemtype;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getPoetname() {
return poetname;
}
public void setPoetname(String poetname) {
this.poetname = poetname;
}
public String getPoemname() {
return poemname;
}
public void setPoemname(String poemname) {
this.poemname = poemname;
}
public String getPoemtype() {
return poemtype;
}
public void setPoemtype(String type) {
this.poemtype = type;
}
@Override
public String toString() {
return "Poet [_id=" + _id + ", poetname=" + poetname + ", poemname="
+ poemname + ", type=" + poemtype + "]";
}
}


content.java

package com.example.android_1800_sqlite1;
import java.io.Serializable;
public class Content implements Serializable{
int _id;
String poemname;
String poetname;
String poemcontent;
String poemyiwen;
String poemzhujie;
public Content() {
super();
// TODO Auto-generated constructor stub
}
public Content(int _id, String poemname, String poetname,
String poemcontent, String poemyiwen, String poemzhujie) {
super();
this._id = _id;
this.poemname = poemname;
this.poetname = poetname;
this.poemcontent = poemcontent;
this.poemyiwen = poemyiwen;
this.poemzhujie = poemzhujie;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getPoemname() {
return poemname;
}
public void setPoemname(String poemname) {
this.poemname = poemname;
}
public String getPoetname() {
return poetname;
}
public void setPoetname(String poetname) {
this.poetname = poetname;
}
public String getPoemcontent() {
return poemcontent;
}
public void setPoemcontent(String poemcontent) {
this.poemcontent = poemcontent;
}
public String getPoemyiwen() {
return poemyiwen;
}
public void setPoemyiwen(String poemyiwen) {
this.poemyiwen = poemyiwen;
}
public String getPoemzhujie() {
return poemzhujie;
}
public void setPoemzhujie(String poemzhujie) {
this.poemzhujie = poemzhujie;
}
@Override
public String toString() {
return "Content [_id=" + _id + ", poemname=" + poemname + ", poetname="
+ poetname + ", poemcontent=" + poemcontent + ", poemyiwen="
+ poemyiwen + ", poemzhujie=" + poemzhujie + "]";
}
}


MainActivity.java:

package com.example.android_1800_sqlite1;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.Toast;
public class MainActivity extends Activity implements OnCheckedChangeListener {
private Intent intent1;
private Intent intent2;
private Intent intent3;
private TabHost tab ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab = (TabHost) findViewById(R.id.tabhost);
LocalActivityManager groupActivity = new LocalActivityManager(this,false);
groupActivity.dispatchCreate(savedInstanceState);
tab.setup(groupActivity);
initIntent();
addSpac();
RadioGroup activity_main_RadioGroup_group = (RadioGroup) findViewById(R.id.activity_main_RadioGroup_group);
activity_main_RadioGroup_group.setOnCheckedChangeListener(this);
}
public TabHost.TabSpec builder(String tag, Intent intent, String label,
Drawable icon) {
return tab.newTabSpec(tag).setContent(intent).setIndicator(label, icon);
}
/*
* 创建space
*/
public void addSpac() {
tab.addTab(builder("tab1", intent1, "tab1",
getResources().getDrawable(R.drawable.ic_launcher)));
tab.addTab(builder("tab2", intent2, "tab2",
getResources().getDrawable(R.drawable.ic_launcher)));
tab.addTab(builder("tab3", intent3, "tab3",
getResources().getDrawable(R.drawable.ic_launcher)));
}
/*
* 初始化intetn
*/
public void initIntent() {
intent1 = new Intent("AllActivity");
intent2 = new Intent("TypeActivity");
intent3 = new Intent("PoetActivity");
}
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.activity_main_RadioButton_rb1:
tab.setCurrentTabByTag("tab1");
break;
case R.id.activity_main_RadioButton_rb2:
tab.setCurrentTabByTag("tab2");
break;
case R.id.activity_main_RadioButton_rb3:

tab.setCurrentTabByTag("tab3");
break;
default:
break;
}
}
}

AllActivity.java:

package com.example.android_1800_sqlite1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AllActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.allpoem);
final Dao dao=new Dao(this);
List<Map<String, String>> listmap=new ArrayList<Map<String, String>>();
List<Poet> list=new ArrayList<Poet>();
list=dao.getAlldata();
for(Poet poet : list){
Map<String, String> map=new HashMap<String, String>();
map.put("id", "第"+poet.get_id()+"首");
map.put("poemname", poet.getPoemname());
map.put("poetname","作者:"+poet.getPoetname());
map.put("poemtype", "类型;"+poet.getPoemtype());
listmap.add(map);
}
ListView listview=(ListView) findViewById(R.id.allpoem_ListView_poemnameandpoetname);
String [] from={"id","poemname","poetname","poemtype"};
int [] to={R.id.allpoem_list_TextView_id,R.id.allpoem_list_TextView_poemname,R.id.allpoem_list_TextView_poetname,R.id.allpoem_list_TextView_poemtype};
SimpleAdapter adapter=new SimpleAdapter(this, listmap, R.layout.allpoem_list, from, to);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView allpoem_list_TextView_poemname=(TextView) view.findViewById(R.id.allpoem_list_TextView_poemname);
//通过诗明获取content对象
String poemname=allpoem_list_TextView_poemname.getText().toString();
Content content=dao.getcontent(poemname);
//跳转到显示内容界面,传递content对象
Intent intent=new Intent("ContentActivity");
intent.putExtra("key", content);
startActivity(intent);

}
});
}
}

TypeAtivity.java:

package com.example.android_1800_sqlite1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TypeActivity extends Activity {
Intent intent;
Button typepoem_Button_5,typepoem_Button_7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.typepoem);
typepoem_Button_5=(Button) findViewById(R.id.typepoem_Button_5);
typepoem_Button_7=(Button) findViewById(R.id.typepoem_Button_7);;
typepoem_Button_5.setOnClickListener(new Mytest());
typepoem_Button_5.setTag(1);
typepoem_Button_7.setOnClickListener(new Mytest());
typepoem_Button_7.setTag(2);
}
class Mytest implements OnClickListener{
@Override
public void onClick(View v) {
int key=(Integer) v.getTag();
switch (key) {
case 1:
Toast.makeText(TypeActivity.this, "五言绝句", 3000).show();
intent=new Intent("Type5Activity");
startActivity(intent);
break;
case 2:
Toast.makeText(TypeActivity.this, "七言绝句", 3000).show();
intent=new Intent("Type7Activity");
startActivity(intent);
break;
default:
break;
}
}
}

}

Type5Activity.java:

package com.example.android_1800_sqlite1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Type5Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.type5poem);
final Dao dao=new Dao(this);
List<Map<String, String>> listmap=new ArrayList<Map<String, String>>();
List<Poet> list=new ArrayList<Poet>();
String s="五言绝句";
list=dao.gettypedata(s);
for(Poet poet : list){
Map<String, String> map=new HashMap<String, String>();
map.put("id", "第"+poet.get_id()+"首");
map.put("poemname", poet.getPoemname());
map.put("poetname","作者:"+poet.getPoetname());
map.put("poemtype", "类型;"+poet.getPoemtype());
listmap.add(map);
}
ListView listview=(ListView) findViewById(R.id.type5poem_ListView);
String [] from={"id","poemname","poetname","poemtype"};
int [] to={R.id.allpoem_list_TextView_id,R.id.allpoem_list_TextView_poemname,R.id.allpoem_list_TextView_poetname,R.id.allpoem_list_TextView_poemtype};
SimpleAdapter adapter=new SimpleAdapter(this, listmap, R.layout.allpoem_list, from, to);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView allpoem_list_TextView_poemname=(TextView) view.findViewById(R.id.allpoem_list_TextView_poemname);
//通过诗明获取content对象
String poemname=allpoem_list_TextView_poemname.getText().toString();
Content content=dao.getcontent(poemname);
//跳转到显示内容界面,传递content对象
Intent intent=new Intent("ContentActivity");
intent.putExtra("key", content);
startActivity(intent);

}
});

}
}


Type7Activity.java:

package com.example.android_1800_sqlite1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Type7Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.type7poem);
final Dao dao=new Dao(this);
List<Map<String, String>> listmap=new ArrayList<Map<String, String>>();
List<Poet> list=new ArrayList<Poet>();
String s="七言绝句";
list=dao.gettypedata(s);
for(Poet poet : list){
Map<String, String> map=new HashMap<String, String>();
map.put("id", "第"+poet.get_id()+"首");
map.put("poemname", poet.getPoemname());
map.put("poetname","作者:"+poet.getPoetname());
map.put("poemtype", "类型;"+poet.getPoemtype());
listmap.add(map);
}
ListView listview=(ListView) findViewById(R.id.type7poem_ListView);
String [] from={"id","poemname","poetname","poemtype"};
int [] to={R.id.allpoem_list_TextView_id,R.id.allpoem_list_TextView_poemname,R.id.allpoem_list_TextView_poetname,R.id.allpoem_list_TextView_poemtype};
SimpleAdapter adapter=new SimpleAdapter(this, listmap, R.layout.allpoem_list, from, to);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView allpoem_list_TextView_poemname=(TextView) view.findViewById(R.id.allpoem_list_TextView_poemname);
//通过诗明获取content对象
String poemname=allpoem_list_TextView_poemname.getText().toString();
Content content=dao.getcontent(poemname);
//跳转到显示内容界面,传递content对象
Intent intent=new Intent("ContentActivity");
intent.putExtra("key", content);
startActivity(intent);

}
});

}
}


PoetActivity.java:

package com.example.android_1800_sqlite1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class PoetActivity extends Activity {
ImageView allactivity_searchroom_ImageView_searchimage;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.poetpoem);
// 设置搜索板块
LinearLayout allactivity_searchroom = (LinearLayout) LayoutInflater
.from(PoetActivity.this).inflate(
R.layout.allactivity_searchname, null);
LinearLayout poetpoem_LinearLayout_search = (LinearLayout) findViewById(R.id.poetpoem_LinearLayout_search);
poetpoem_LinearLayout_search.addView(allactivity_searchroom);
// 设置搜索适配进行自动文本
final AutoCompleteTextView allactivity_searchroom_AutoCompleteTextView_searchname = (AutoCompleteTextView) findViewById(R.id.allactivity_searchroom_AutoCompleteTextView_searchname);
ArrayAdapter searchadapter = ArrayAdapter.createFromResource(
PoetActivity.this, R.array.name,
android.R.layout.simple_spinner_item);
allactivity_searchroom_AutoCompleteTextView_searchname
.setAdapter(searchadapter);
// 获取搜索确认进入room按钮
allactivity_searchroom_ImageView_searchimage = (ImageView) allactivity_searchroom
.findViewById(R.id.allactivity_searchroom_ImageView_searchimage);
// 进入room点击事件监听
allactivity_searchroom_ImageView_searchimage
.setOnClickListener(new OnClickListener() {
Dao dao = new Dao(PoetActivity.this);
@Override
public void onClick(View v) {
// 获取当前自动文本内容
String txt = allactivity_searchroom_AutoCompleteTextView_searchname
.getText().toString();
// 判断为空或没有对应数据就弹出对话框,并清空
if (txt == null || txt.equals("")
|| dao.equalspoetname(txt) == false) {
new AlertDialog.Builder(PoetActivity.this)
.setTitle("提示:").setMessage("没有找到您到搜索的内容!")
.setPositiveButton("知道了!", null)
.setNegativeButton("取消", null).create()
.show();
allactivity_searchroom_AutoCompleteTextView_searchname
.setText("");
} else {
List<Map<String, String>> listmap = new ArrayList<Map<String, String>>();
List<Poet> list = new ArrayList<Poet>();
list = dao.getnamedata(txt);
for (Poet poet : list) {
Map<String, String> map = new HashMap<String, String>();
map.put("id", "第" + poet.get_id() + "首");
map.put("poemname", poet.getPoemname());
map.put("poetname", "作者:" + poet.getPoetname());
map.put("poemtype", "类型;" + poet.getPoemtype());
listmap.add(map);
}
ListView listview = (ListView) findViewById(R.id.poetpoem_ListView_search);
String[] from = { "id", "poemname", "poetname",
"poemtype" };
int[] to = { R.id.allpoem_list_TextView_id,
R.id.allpoem_list_TextView_poemname,
R.id.allpoem_list_TextView_poetname,
R.id.allpoem_list_TextView_poemtype };
SimpleAdapter adapter = new SimpleAdapter(
PoetActivity.this, listmap,
R.layout.allpoem_list, from, to);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
TextView allpoem_list_TextView_poemname = (TextView) view
.findViewById(R.id.allpoem_list_TextView_poemname);
// 通过诗明获取content对象
String poemname = allpoem_list_TextView_poemname
.getText().toString();
Content content = dao.getcontent(poemname);
// 跳转到显示内容界面,传递content对象
Intent intent = new Intent(
"ContentActivity");
intent.putExtra("key", content);
startActivity(intent);
}
});
}
}
});
}
}

创建数据库,并创建2张表

创建之后在DDMS中再到data-data—包明下的数据库

导出到可视化软件 SQLite Expert中,也可自己手动添加,也可用SQL语句添加数据

DBOpenHelper.java:

package com.example.android_1800_sqlite1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String name,
int version) {
super(context, name, null, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
/*//创建作者-诗名—类型表
String sql=" CREATE TABLE IF NOT EXISTS poet( "
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "poetname VARCHAR(22), " + "poemname VARCHAR(22), " + "poemtype VARCHAR(18) "
+ ");";
db.execSQL(sql);*/
//创建作者-诗名-内容-译文-注释
/*String sql=" CREATE TABLE IF NOT EXISTS content( "
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "poemname VARCHAR(22), " + "poetname VARCHAR(22), " + "poemcontent VARCHAR(18) "+ "poemyiwen VARCHAR(1000) "+"poemzhujie VARCHAR(3000) "
+ ");";
db.execSQL(sql);*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}


最关键的Dao.java,个人写对数据库的操作类都用Dao类

package com.example.android_1800_sqlite1;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Dao {
DBOpenHelper helper=null;
Context context=null;
SQLiteDatabase db=null;
public Dao(Context context){
this.context=context;
helper=new DBOpenHelper(context, "poet.db", 1);
db=helper.getWritableDatabase();
}

//查询全部数据
public List<Poet> getAlldata() {
List<Poet> list=new ArrayList<Poet>();
String sql = "select * from Poet ";
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext()){
list.add(new Poet(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3)));
}
return list;
}
//按类型查找对象
public List<Poet> gettypedata(String s) {
List<Poet> list=new ArrayList<Poet>();
String sql = "select * from Poet where poemtype=?";
Cursor cursor = db.rawQuery(sql, new String[]{s});
while(cursor.moveToNext()){
list.add(new Poet(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3)));
}
return list;
}
// 按类型查找对象,返回boolean
public boolean equalspoetname(String s) {
List<Poet> list = new ArrayList<Poet>();
String sql = "select * from Poet where poetname=?";
Cursor cursor = db.rawQuery(sql, new String[] { s });
if (cursor.getCount()>0) {
return true;
}else{
return false;
}

}
// 按类型查找对象,name
public List<Poet> getnamedata(String s) {
List<Poet> list = new ArrayList<Poet>();
String sql = "select * from Poet where poetname=?";
Cursor cursor = db.rawQuery(sql, new String[] { s });
while (cursor.moveToNext()) {
list.add(new Poet(cursor.getInt(0), cursor.getString(1), cursor
.getString(2), cursor.getString(3)));
}
return list;
}
//传人作者姓名 得到对应的content表中的内容
public Content getcontent(String s) {
Content content = null;
String sql = "select * from content where poemname=?";
Cursor cursor = db.rawQuery(sql, new String[] { s });
while (cursor.moveToNext()) {
content=new Content(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5));
}
return content;
}

}


最后是诗的显示界面ContentActivity.java

package com.example.android_1800_sqlite1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ContentActivity extends Activity {
TextView poet_poem_content_TextView_poemyiwen;
TextView poet_poem_content_TextView_poemzhujie;
Content content;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.poet_poem_content);
//接收Intent
Intent intent=getIntent();
content=(Content) intent.getSerializableExtra("key");
//找到要放content内容的控件
TextView poet_poem_content_TextView_poemname=(TextView) findViewById(R.id.poet_poem_content_TextView_poemname);
TextView poet_poem_content_TextView_poetname=(TextView) findViewById(R.id.poet_poem_content_TextView_poetname);
TextView poet_poem_content_TextView_poemcontent=(TextView) findViewById(R.id.poet_poem_content_TextView_poemcontent);
poet_poem_content_TextView_poemyiwen=(TextView) findViewById(R.id.poet_poem_content_TextView_poemyiwen);
poet_poem_content_TextView_poemzhujie=(TextView) findViewById(R.id.poet_poem_content_TextView_poemzhujie);
poet_poem_content_TextView_poemname.setText(content.getPoemname());
poet_poem_content_TextView_poetname.setText(content.getPoetname());
poet_poem_content_TextView_poemcontent.setText(content.getPoemcontent());
//获取按钮
Button poet_poem_content_Button_yiwen=(Button) findViewById(R.id.poet_poem_content_Button_yiwen);
poet_poem_content_Button_yiwen.setTag(1);
Button poet_poem_content_Button_zhujie=(Button) findViewById(R.id.poet_poem_content_Button_zhujie);
poet_poem_content_Button_zhujie.setTag(2);
//点击事件
poet_poem_content_Button_yiwen.setOnClickListener(new MyClickListener());
poet_poem_content_Button_zhujie.setOnClickListener(new MyClickListener());
}
class MyClickListener implements OnClickListener{
@Override
public void onClick(View v) {
int key=(Integer) v.getTag();
switch (key) {
case 1:
if(poet_poem_content_TextView_poemyiwen.getText().equals("")){
poet_poem_content_TextView_poemyiwen.setText("译文:"+content.getPoemyiwen());
}else{
poet_poem_content_TextView_poemyiwen.setText("");
}
break;
case 2:
if(poet_poem_content_TextView_poemzhujie.getText().equals("")){
poet_poem_content_TextView_poemzhujie.setText("注解:"+content.getPoemzhujie());
}else{
poet_poem_content_TextView_poemzhujie.setText("");
}
break;
default:
break;
}
}

}
}


数据库截图、;



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: