一、首先上效果图:
二、代码部分 :
(一)布局部分:
1.单个成语的布局:
<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:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvName" 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:layout_alignTop="@id/tvName" android:src="@drawable/btnsave"/> </RelativeLayout>2.点击后主页面显示的布局:-listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@drawable/bg_animal" android:orientation="vertical" android:layout_height="match_parent"> <ListView android:layout_width="match_parent" android:id="@+id/lvAnimalList" android:layout_height="wrap_content" android:layoutAnimation="@anim/anim_layout_listview" android:listSelector="@drawable/clickbackground"> </ListView> </LinearLayout>3.:主页面的布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 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:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tvIdiomInfo" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> </ScrollView>(二)类和数据库:
1.adapter:
public class AnimalAdapter extends ArrayAdapter<Animal> { private int resourceId; public AnimalAdapter(Context context, int resource, List<Animal> objects) { super(context, resource, objects); resourceId=resource; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { Animal animal = getItem(position); 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); view.setTag(viewHolder); }else { view=convertView; viewHolder=(ViewHolder)view.getTag(); } viewHolder.tvName.setText(animal.getName()); viewHolder.btnSave.setFocusable(false); viewHolder.btnSave.setFocusableInTouchMode(false); return view; } private class ViewHolder { TextView tvName; ImageButton btnSave; } }2.dao:
public class AnimalDao { private static AnimalDao animalDao; private SQLiteDatabase db; private AnimalDao(Context context) { DBOpenHelper dbOpenHelper=new DBOpenHelper(context); db= dbOpenHelper.openDatabase(); } public synchronized static AnimalDao getInstance(Context context) { if (animalDao == null) { animalDao= new AnimalDao(context); } return animalDao; } public List<Animal>getAllAnimals(){ List<Animal> list=new ArrayList<Animal>(); Cursor cursor=db.query("animal",null,null,null,null,null,null); if (cursor.moveToFirst()){ do { Animal animal=new Animal(); animal.setId(cursor.getInt(cursor.getColumnIndex("_id"))); animal.setName(cursor.getString(cursor .getColumnIndex("name"))); animal.setPronounce(cursor.getString(cursor .getColumnIndex("pronounce"))); animal.setAntonym(cursor.getString(cursor .getColumnIndex("antonym"))); animal.setHomoionym(cursor.getString(cursor .getColumnIndex("homoionym"))); animal.setDerivation(cursor.getString (cursor.getColumnIndex("derivation"))); animal.setExamples(cursor.getString (cursor.getColumnIndex("examples"))); list.add(animal); }while (cursor.moveToNext()); } return list; } }3.helper:
public class DBOpenHelper { private final int BUFFER_SIZE = 400000;//缓冲区大小 public static final String DB_NAME = "idiom.db";//保存的数据库文件名 public static final String PACKAGE_NAME = "cn.edu.bzu.happyidiom";//应用包名 public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME + "/databases";//在手机里存放数据库的位置 private Context context; public DBOpenHelper(Context context) { this.context = context; } public SQLiteDatabase openDatabase() { try { File myDataPath = new File(DB_PATH); if (!myDataPath.exists()) { myDataPath.mkdirs();//如果没有这个目录就创建 } String dbfile = myDataPath + "/" + DB_NAME; if (!(new File(dbfile).exists())) {//判断是否存在,若不存在机导入,否则直接打开 InputStream is = context.getResources().openRawResource(R.raw.idioms); FileOutputStream fos = new FileOutputStream(dbfile); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.close(); is.close(); } SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); return db; } catch (FileNotFoundException e) { Log.e("Database", "File not found"); e.printStackTrace(); } catch (IOException e) { Log.e("Database", "IO exception"); e.printStackTrace(); } return null; } }4.animal类:
public class Animal { private int id; private String name; private String pronounce; private String explain;//解释 private String antonym;//反义词 private String homoionym;//同义词 private String derivation;//源自 private String examples;//例子 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPronounce() { return pronounce; } public void setPronounce(String pronounce) { this.pronounce = pronounce; } public String getExplain() { return explain; } public void setExplain(String explain) { this.explain = explain; } public String getAntonym() { return antonym; } public void setAntonym(String antonym) { this.antonym = antonym; } public String getHomoionym() { return homoionym; } public void setHomoionym(String homoionym) { this.homoionym = homoionym; } public String getDerivation() { return derivation; } public void setDerivation(String derivation) { this.derivation = derivation; } public String getExamples() { return examples; } public void setExamples(String examples) { this.examples = examples; } }5.util:
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(){ @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } }(三)mainactivity:
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 AdapterView.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(); } }(四)manifest:更改入口类:
<activity android:name=".activity.StudyAnimalActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>三、错误部分:
当出现R的错误的时候 ,要看你的res部分有没有错误,如果没有就是系统的问题。