gpt4 book ai didi

java - 将代码转换为类

转载 作者:行者123 更新时间:2023-12-01 18:51:13 25 4
gpt4 key购买 nike

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

我是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 但仍然有问题在此处输入代码

最佳答案

好吧,我根据您的要求创建了一个简单的类,但我不清楚您代码的某些部分,例如 ClubCP.DbPathClubCP.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!");
}
}

关于java - 将代码转换为类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15860018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com