乐学成语(3)

前面已经把主界面的布局内容的显示过程作了介绍,接下来,将对成语列表的显示以及每个成语的详细内容做详细的介绍

  1. 显示所有动物类成语的列表
    在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;
    }

}
  1. 显示每条成语的详细信息
    首先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();
    }
}

效果图:
这里写图片描述

这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章