gpt4 book ai didi

java - 访问 Android 中简单类的成员

转载 作者:行者123 更新时间:2023-12-01 19:02:46 25 4
gpt4 key购买 nike

我有一个简单的 Java 类,它扩展了另一个包中的 View,并且我正在使用此类创建自定义 View 。我想将它用作另一个类的 XML 中的 View,该类是一个 Activity

我的自定义View需要来自该类的一些数据,因此我创建了一个Pt类的对象,它扩展了Activity。该对象已创建,但是当我使用该对象访问扩展 Activity 的类的成员时,它不显示任何成员。我做错了什么吗,或者有更好的方法吗?

这是我的代码。我已经在评论中展示了我所期待的内容。

public class PlotView extends View {//class to make a custom view

Plot_View obj_plot = new Plot_View();
obj_plot.// here i am expecting it to show the members of that Plot_View class which is extending Activity

class Pt{
float x, y;
Pt(float _x, float _y){
x = _x;
y = _y;
}
}

Pt[] myPath = { new Pt(100, 100), new Pt(200, 200), new Pt(200, 500),
new Pt(400, 500), new Pt(400, 200) };

public PlotView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public PlotView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.STROKE);
Path path = new Path();
path.moveTo(myPath[0].x, myPath[0].y);

for (int i = 1; i < myPath.length; i++){
path.lineTo(myPath[i].x, myPath[i].y);
}
canvas.drawPath(path, paint);
}
}//

这是我的 Plot_View Activity 。我想访问此 Activity 中定义的这些 Arraylists

     public class Plot_View extends Activity implements OnClickListener {


public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.plotview);

calculationForPlot();

findViewById(R.id.btn_measure).setOnClickListener(this);



initMirrorMatrix();

drawMatrix();
}

private void calculationForPlot(){
ArrayList<String> al_edit_A_ft = new ArrayList<String>();
ArrayList<String> al_edit_A_inch=new ArrayList<String>();
ArrayList<String>al_edit_B_ft=new ArrayList<String>();
ArrayList<String>al_edit_B_inch= new ArrayList<String>();
float x=0;
AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getReadableDatabase();
String q="SELECT * FROM ab_measurement WHERE job_id=" +"\""+Settings.jobid +"\"";


Cursor cursor = sqliteDatabase.rawQuery(q,null);

if (cursor.moveToNext()) {
System.out.println(cursor.getCount());
m=Integer.parseInt(cursor.getString(4));

do {
try{

float a = 0,b = 0;



a=(float) Double.parseDouble(cursor.getString(6));
String number =String.valueOf(a);
System.out.println("aaa ggg"+number);
String int_part =number.substring(0,number.indexOf("."));
String float_part=number.substring(number.lastIndexOf(".")+1,number.length());
System.out.println("aaa values"+int_part);
System.out.println("aaa values"+float_part);


al_edit_A_ft.add(int_part);
al_edit_A_inch.add(float_part);

b= (float) Double.parseDouble(cursor.getString(7));
String number_b =String.valueOf(b);
System.out.println("aaa ggg"+number_b);
String int_part_b =number_b.substring(0,number_b.indexOf("."));
String float_part_b=number_b.substring(number_b.lastIndexOf(".")+1,number_b.length());
System.out.println("aaa values"+int_part_b);
System.out.println("aaa values"+float_part_b);
al_edit_B_ft.add(int_part_b);
al_edit_B_inch.add(float_part_b);

x= (float) Double.parseDouble(cursor.getString(3));
String ft_base =String.valueOf(x);
System.out.println("aaa ggg"+ft_base);
String int_part_ft =ft_base.substring(0,ft_base.indexOf("."));
String float_part_inch=ft_base.substring(ft_base.lastIndexOf(".")+1,ft_base.length());
System.out.println("aaa values"+int_part_ft);
System.out.println("aaa values"+float_part_inch);


}
catch (Exception e) {

}

} while (cursor.moveToNext());

最佳答案

试试这个......

在Java类中创建 Your_Activity 类型的对象引用变量,并在构造函数中使用 Context 传递给它。

Activity 类别:

public class MyActivity extends Activity {
TextView myView ;
protected void onCreate(android.os.Bundle savedInstanceState) {
myView = (TextView)findViewById(R.id.myView);
Points myPoints = new Points(this);
myPoints.displayMsg("MY NAME IS VIVEK");
}
}

Java 类:

    private class Points {

public MyActivity mcontext;

////---------- a constructor with the Context of your activity

public Points(MyActivity context){
mcontext = context;
}

public void displayMsg( final String msg){
context.runOnUiThread(new Runnable() {

@Override
public void run() {
mcontext.myView.setText(msg);
}
});
}
}

关于java - 访问 Android 中简单类的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602801/

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