任何人都可以帮助我学习如何将代码转换为类,以便我可以在任何地方使用它吗?例如,我有这段代码如何将其转换为单独的类并在我的不同活动中使用它?
我是java新手,对此确实有问题..感谢您的帮助
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;
}
最后我创建了我的类..我向它添加了另一个方法,但是当我调用它时,我得到了 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
收到getDrawable(R.drawable.music_album_header_vinyl)
“getdrawable is undifined”的错误我搜索,我发现问题出在范围但无法修复它..我试过c.getDrawable
但仍然有问题enter code here