任何人都可以帮助我学习如何将代码转换为类,以便我可以在每个地方使用它吗? 例如,我有这个代码 如何将其转换为单独的类并在我的不同活动中使用它?
我是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!");
}
}
我通过此代码调用secound方法
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
答案 0 :(得分:2)
好的,我根据您的要求制作了一个简单的课程,但我的代码的某些部分不清楚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!");
}
}
答案 1 :(得分:1)
首先,创建一个名称描述其内容的新类(即用名称替换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;
}
}
}
欢迎使用面向对象的编程:)
答案 2 :(得分:1)
通过右键单击src
文件夹并点击创建new class
,即可轻松创建新课程。
完成命名课程的过程后,请将当前代码粘贴到该课程中,这将是您的public String getInformationData(String mySQL)
方法。
完成此操作后,创建对此类的引用,在要从中调用String getInformationData(String mySQL)
的每个类/活动中创建此类的对象。
YourClass foo = new YourClass();
foo.getInformationData(string);
我希望这会有所帮助。