您的位置:首页 > 其它

乐学成语案例分析

2016-06-15 19:51 267 查看
<pre name="code" class="html"><pre class="java" name="code">public class StudyActivity extends Activity {
private List<Category> categoryList;
private String[] category_names;
private int[] category_images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
initCategory();//初始化类
CategoryAdapter adapter=new CategoryAdapter(this, R.layout.category_item, categoryList);
ListView listView=(ListView) findViewById(R.id.lvCategories);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent;
switch(position){
case 0:
intent=new Intent(StudyActivity.this,StudyAnimalActivity.class);
startActivity(intent);
break;
case 6:
intent=new Intent(StudyActivity.this,StudyOtherActivity.class);
startActivity(intent);
break;
default:
break;
}

}
});
}

private void initCategory() {
// TODO Auto-generated method stub
categoryList=new ArrayList<Category>();
Resources resources=getResources();
category_names=resources.getStringArray(R.array.category);
category_images=new int[]{R.drawable.category_animal,
R.drawable.category_nature,R.drawable.category_human,
R.drawable.category_season,R.drawable.category_number,
R.drawable.category_fable,R.drawable.category_other
};
for(int i=0;i<category_names.length;i++){
categoryList.add(new Category(category_names[i], category_images[i]));

}

}

@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, menu);
return true;
}

}
<img src="https://img-blog.csdn.net/20160615133812641?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="310" height="407" alt="" />


public class DBOpenHelper { private final int BUFFER_SIZE=400000;//缓冲区大小 public static final String DB_NAME="idioms.db"; //保存的数据库文件名 public static final String PACKAGE_NAME="com.example.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; } }

public class DBOpenHelper {
private final int BUFFER_SIZE=400000;//缓冲区大小
public static final String DB_NAME="idioms.db"; //保存的数据库文件名
public static final String PACKAGE_NAME="com.example.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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: