将代码隐藏起来
任何人都可以帮助我学习如何将代码转换为类,以便我可以在任何地方使用它? 例如我有这个代码 我怎样才能将它转换为一个单独的类,并在我的不同活动中使用它?将代码隐藏起来
我新的Java和真的有问题,this..Thanks您的帮助
public String getInformationData(String mySQL){
String information_text=null;
try{
db = SQLiteDatabase.openDatabase(ClubCP.DbPath,null,SQLiteDatabase.CREATE_IF_NECESSARY);
Cursor information = mDb.rawQuery(mySQL,null);
int information1 = information.getColumnIndex("description");
while (information.moveToNext()) {
String columns = (String) information.getString(information1);
information_text = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ ClubCP.SDcardPath+ "Homa.ttf');}body {font-family: 'verdana';color:#ffffff;font-size:18px;padding:10px 10px 0 10px;}</style></head>"+"<html Content-Type: text/html charset=UTF-8;dir=\"rtl\"><body>"
+ "<p dir=\"rtl\" align=\"justify\">"
+ columns
+ "</p> "
+ "</body></html>";
}
} catch (Exception e) {
Toast.makeText(callingActivity, e.getMessage(), 1).show();
}
return information_text;
}
最后我创造了我class..I添加另一种方法,但是当我把它称为我得到FC。我的错误在哪里?
package co.tosca.persianpoem;
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class persian_poem_class {
private Context c = null;
private SQLiteDatabase Db=null;
private Activity callingActivity;
//private Resources res =null ;
public persian_poem_class(Context c,Activity a)
{
// Constructor
this.c = c;
Db = SQLiteDatabase.openDatabase(ClubCP.DbPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
callingActivity=a;
}
public String getInformationData(String mySQL)
{
String information_text = null;
try
{
Cursor information = Db.rawQuery(mySQL,null);
int information1 = information.getColumnIndex("description");
while (information.moveToNext())
{
String columns = (String) information.getString(information1);
information_text = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ ClubCP.SDcardPath+ "Homa.ttf');}body {font-family: 'verdana';color:#ffffff;font-size:18px;padding:10px 10px 0 10px;}</style></head>"+"<html Content-Type: text/html charset=UTF-8;dir=\"rtl\"><body>"
+ "<p dir=\"rtl\" align=\"justify\">"
+ columns
+ "</p> "
+ "</body></html>";
}
}
catch (Exception e)
{
Toast.makeText(c, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return information_text;
}
public void Change_header(View v,String id){
String path = ClubCP.SDcardPath + "/temp/"+id+"-header.jpg";
Log.i("view binder", path);
File imgFile = new File(path);
ImageView img=(ImageView)v;
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
img.setImageBitmap(myBitmap);
}
else {
img.setImageDrawable(callingActivity.getDrawable(R.drawable.music_album_header_vinyl));
}
}
public Cursor getData(String mySQL){
Cursor c = Db.rawQuery(mySQL, null);
return c;
}
public void closeMyDb()
{
if (Db != null)
Db.close();
else
throw new NullPointerException("No database selected!");
}
}
我这段代码调用谢胜利方法
persian_poem_class main = new persian_poem_class(Book_list.this);
ImageView header=(ImageView)findViewById(R.id.img_header_book_list_activity);
main.Change_header(header, Peot_ID_for_db);
再次感谢您的时间.. 我固定我的课,但现在我有Change_header
method..I另一个问题得到这个错误getDrawable(R.drawable.music_album_header_vinyl)
“getdrawable is undifined”我搜索了,我发现问题是与范围,但无法修复它..我试过c.getDrawable
,但仍有问题enter code here
好吧,我提出应您要求,但一些部分的简单类的代码是不清楚的,我喜欢ClubCP.DbPath
或ClubCP.SDcardPath
,我认为这些是静态变量。
仍要使用这个类,你需要从myClass
做出新的实例:
myClass mMyClass = new myClass(youractivity.this);
mMyClass.getInformationData("your query");
mMyClass.closeMyDb() // To close your current database
市价修改意见:
import java.io.File;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class myClass
{
private Context c = null;
private SQLiteDatabase mDb;
public myClass(Context c)
{
// Constructor
this.c = c;
mDb = SQLiteDatabase.openDatabase(ClubCP.DbPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
}
public String getInformationData(String mySQL)
{
String information_text = null;
try
{
Cursor information = mDb.rawQuery(mySQL,null);
int information1 = information.getColumnIndex("description");
while (information.moveToNext())
{
String columns = (String) information.getString(information1);
information_text = "<head><style>@font-face {font-family: 'verdana';src: url('file://"+ ClubCP.SDcardPath+ "Homa.ttf');}body {font-family: 'verdana';color:#ffffff;font-size:18px;padding:10px 10px 0 10px;}</style></head>"+"<html Content-Type: text/html charset=UTF-8;dir=\"rtl\"><body>"
+ "<p dir=\"rtl\" align=\"justify\">"
+ columns
+ "</p> "
+ "</body></html>";
}
}
catch (Exception e)
{
Toast.makeText(c, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return information_text;
}
public void Change_header(View v, String id)
{
String path = ClubCP.SDcardPath + "/temp/"+id+"-header.jpg";
Log.i("view binder", path);
File imgFile = new File(path);
ImageView img = (ImageView) v;
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
img.setImageBitmap(myBitmap);
}
else
img.setImageDrawable(c.getResources().getDrawable(R.drawable.music_album_header_vinyl));
}
public void closeMyDb()
{
if (mDb != null)
mDb.close();
else
throw new NullPointerException("No database selected!");
}
}
感谢我的朋友的帮助..我添加了一些代码,但我有一些问题,我有一些问题..我更新了我的问题。你能帮我解答一下吗? – Majid 2013-04-07 08:32:58
@Majid Sure Majid jan,你的问题在哪里?我看不到您更新的问题! – NullPointer 2013-04-07 08:34:31
哦,你说波斯语吗?我更新了我的问题..谢谢:) – Majid 2013-04-07 08:46:43
首先,创建一个名称的新类这是描述它的功能(即用名称替换myClass)。然后,通过调用public myClass()
来创建这个类的构造函数,但没有返回类型(这是Java如何将它标识为构造函数的方式。构造函数是每次运行类时调用的构造函数,因此只需将代码粘贴到构造函数的主体中,它会被调用每次创建类的新对象。
Class myClass {
...
public myClass(){
public String getInformationData(String mySQL){
String information_text=null;
try{
db = SQLiteDatabase.openDatabase ...
... rest of code...
return information_text;
}
}
}
欢迎面向对象编程:)
谢谢你,我想添加一些其他方法到我的课..所以我认为它更好地只是在构造函数中添加我的开放数据库..这是更好的?还是不..再次感谢;) – Majid 2013-04-07 08:42:55
简单通过右键单击创建新的类你的src
文件夹并点击创建new class
。
一旦你完成了命名你的课程的过程,去当前的代码粘贴到该类,这将是你的public String getInformationData(String mySQL)
方法。
完成此操作后,创建对此类的引用,在您想要从中调用String getInformationData(String mySQL)
的每个类/活动中创建此类的对象。
YourClass foo = new YourClass();
foo.getInformationData(string);
我希望这有助于。
你想在运行时执行此操作还是编译时间? – iGili 2013-04-07 07:41:50
对不起,我没有得到你的意思..我认为你的答案是在编译时间 – Majid 2013-04-07 08:02:42