乐学成语——显示每条成语的详细信息

     1.在layout下新建布局文件dialog_info.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/tvIdiomInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

</ScrollView>
  可以看到,最外层是ScrollView组建,当内容较多时会自动出现垂直滚动条。

     2.修改StudyAnimalAcitivity,增加点击事件处理,代码如下所示:

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(){		
			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.getAnytonym()+
						"\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;
	}

}
     3.在util包下新建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 tvIdiomInfo=(TextView) view.findViewById(R.id.tvIdiomInfo);	
		tvIdiomInfo.setText(result);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {			
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	

}
     4.可是运行时点击每个成语并不弹出对话框,解决办法是,修改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;
	}
	public View getView(int position,View convertView,ViewGroup parent){
	    final Animal animal=getItem(position);//获取当前项的Animal实例
		View view;
		ViewHolder viewHolder;
		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(){
				public void onClick(View view){
					Toast.makeText(context, "你要收藏"+animal.getName()+"吗", 
							Toast.LENGTH_SHORT).show();
				}
			});			
			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;
	}

}
     5.运行程序,结果如下:






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值