我有一个简单的 Java 类,它View
在另一个包中扩展,我正在使用这个类制作一个自定义视图。我想将它用作View
另一个类的 XML 中的一个,它是一个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 活动。我想访问在此活动中定义的这些 Arraylist
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());