0

任何人都可以帮助我学习如何将代码转换为类,以便我可以在任何地方使用它吗?例如,我有这段代码如何将其转换为单独的类并在我的不同活动中使用它?

我是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

4

3 回答 3

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!");
    }
}
于 2013-04-07T07:44:50.020 回答
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;
}

}
}

欢迎来到面向对象编程 :)

于 2013-04-07T07:41:40.133 回答
1

通过右键单击您的src文件夹并单击 create来简单地创建一个新类new class

完成命名类的过程后,将当前代码粘贴到该类中,这将是您的public String getInformationData(String mySQL)方法。

完成此操作后,创建对此类的引用,在您要从中调用的每个类/活动中创建此类的对象String getInformationData(String mySQL)

YourClass foo =  new YourClass();
foo.getInformationData(string);

我希望这有帮助。

于 2013-04-07T08:31:36.643 回答