1.新建布局文件dialog_infos.xml。
<span style="font-size:18px;"><?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/tvIdiomInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
</ScrollView></span>
2.新建DialogUtil类。
<span style="font-size:18px;">package com.edu.happyidiom.util;
import com.example.happyidiom.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
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_infos, null);
builder.setView(view);
TextView tvIdiomInfo=(TextView) view.findViewById(R.id.tvIdiomInfo);
tvIdiomInfo.setText(result);
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}</span>
3.修改StudyAnimalActivity的点击事件。
<span style="font-size:18px;">package com.example.happyidiom;
import java.util.List;
import com.edu.happyidiom.adapter.AnimalAdapter;
import com.edu.happyidiom.dao.AnimalDao;
import com.edu.happyidiom.entity.Animal;
import com.edu.happyidiom.util.DialogUtil;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
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_study_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) {
// TODO Auto-generated method stub
Animal animal=animalList.get(position);
String result=animal.getName()+"\n"+
animal.getPronounce()+
"\n【解释】:"+animal.getExplain()
+"\n【近义词】:"+animal.getHomoionym()+
"\n【反义词】:"+animal.getAutonym()+
"\n【来源】:"+animal.getDerivation()+
"\n【示例】:"+animal.getExamples();
DialogUtil.showDialog(result, StudyAnimalActivity.this);
}
});
}
private void initAnimals() {
// TODO Auto-generated method stub
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;
}
}</span>
现在可以运行一下,界面如下