前面已經把主界面的佈局內容的顯示過程作了介紹,接下來,將對成語列表的顯示以及每個成語的詳細內容做詳細的介紹
- 顯示所有動物類成語的列表
在Layout下新建activity_animal.xml文件,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_animal"
android:orientation="vertical" >
<ListView
android:id="@+id/lvAnimalList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/anim_layout_listview"
android:listSelector="#00000000"
>
</ListView>
</LinearLayout>
然後需要爲ListView的子佈局指定一個我們自定義的佈局,在layout目錄下新建animal_item.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="助人爲樂"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<ImageButton
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:layout_alignParentRight="true"
android:src="@drawable/btnsave"
/>
</RelativeLayout>
效果圖:
接下來需要新建AnimalAdapter,代碼如下:
public class AnimalAdapter extends ArrayAdapter<Animal>{
private int resourceid;
private Context context;
public AnimalAdapter(Context context, int resource, List<Animal> objects) {
super(context, resource, objects);
this.context=context;
resourceid= resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Animal animal=getItem(position);//獲取當前項的Animal示例
View view;
ViewHolder viewHolder;
DBHelper helper = new DBHelper(context);
SQLiteDatabase db=helper.getWritableDatabase();
db.close();
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceid, null);
viewHolder=new ViewHolder();
viewHolder.tvName=(TextView) view.findViewById(R.id.tvName);
viewHolder.btnSave=(ImageButton) view.findViewById(R.id.btnSave);
viewHolder.btnSave.setFocusable(false);
viewHolder.btnSave.setFocusableInTouchMode(false);
viewHolder.btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String name=animal.getName();
String pronounce=animal.getPronounce();
String explain=animal.getExplain();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("pronounce", pronounce);
values.put("explain", explain);
DBHelper helper = new DBHelper(context);
helper.insert(values);
Intent intent = new Intent(context, QueryActivity.class);
context.startActivity(intent);
}
});
view.setTag(viewHolder);//將ViewHolder存儲到View中
}else{
view=convertView;
viewHolder=(ViewHolder) view.getTag(); //重新獲取viewHolder
}
viewHolder.tvName.setText(animal.getName());
return view;
}
class ViewHolder{
TextView tvName;
ImageButton btnSave;
}
}
下面在activity包下新建StudyAnimalActivity代碼如下:
public class StudyAnimalActivity extends Activity {
private List<Animal> animalList;
private AnimalDao animalDao;
private ListView lvAnimalList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal);
initAnimals();
lvAnimalList=(ListView) findViewById(R.id.lvAnimalList);
AnimalAdapter animalAdapter=new AnimalAdapter(this, R.layout.animal_item, animalList);
lvAnimalList.setAdapter(animalAdapter);
lvAnimalList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
Animal animal=animalList.get(position);
String result=animal.getName()+"\n"+animal.getPronounce()+
"\n【解釋】:"+animal.getExplain()+
"\n【近義詞】:"+animal.getHomoionym()+
"\n【反義詞】:"+animal.getAntonym()+
"\n【來源】:"+animal.getDerivation()+
"\n【示例】:"+animal.getExamples();
DialogUtil.showDialog(result, StudyAnimalActivity.this);
}
});
}
private void initAnimals() {
animalDao=AnimalDao.getInstance(this);
animalList=animalDao.getAllAnimals();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.study_animal, menu);
return true;
}
}
- 顯示每條成語的詳細信息
首先Layout下新建佈局文件dialog.xml,代碼如下;
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_ling"
android:orientation="vertical" >
<TextView
android:id="@+id/tvldiominfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/laba" />
</LinearLayout>
</ScrollView>
在StudyAnimalActivity中已經添加了點擊事件
lvAnimalList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
Animal animal=animalList.get(position);
String result=animal.getName()+"\n"+animal.getPronounce()+
"\n【解釋】:"+animal.getExplain()+
"\n【近義詞】:"+animal.getHomoionym()+
"\n【反義詞】:"+animal.getAntonym()+
"\n【來源】:"+animal.getDerivation()+
"\n【示例】:"+animal.getExamples();
DialogUtil.showDialog(result, StudyAnimalActivity.this);
}
});
}
這裏DialogUtil類,代碼如下:
public class DialogUtil {
public static void showDialog(String result,Context context){
AlertDialog.Builder builder=new AlertDialog.Builder(context);
LayoutInflater layoutInflater=LayoutInflater.from(context);
View view=layoutInflater.inflate(R.layout.dialog_info, null);
builder.setView(view);
TextView tvldiominfo=(TextView) view.findViewById(R.id.tvldiominfo);
tvldiominfo.setText(result);
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
效果圖: