2012-07-22 3 views
0

다른 패키지에 View을 확장하는 간단한 Java 클래스가 있으며이 클래스를 사용하여 사용자 정의보기를 만들고 있습니다. 나는 Activity 인 다른 클래스의 XML에서 View으로 사용하고 싶습니다.Android에서 간단한 클래스의 멤버에 액세스

내 맞춤형 View은 해당 클래스의 데이터가 필요하므로 Activity까지 확장되는 Pt 클래스의 개체를 만들었습니다. 개체가 생성되었지만 해당 개체를 사용하여 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); 
    } 
}// 

여기가

 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()); 
+0

위대한 .am하고있다 – Rahulkapil

+0

내 코드를 게시했습니다. – Rahulkapil

+0

올바른 답변을 드릴 수 있도록 내 대답을 편집했습니다. –

답변

4

이 시도이 활동에 여기에 정의 된 이러한 Arraylists에 액세스 할 .I 내 Plot_View 활동 ....

의 객체 참조 변수를 만듭니다이다 Your_ActivityJava 클래스에 입력하고Context이 전달 된 컨스트럭터에서 초기화하십시오.

활동 클래스 :

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"); 
} 
} 

자바 클래스 :

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);  
      } 
     }); 
    } 
} 
+0

내 코드 plss를 게시했습니다. – Rahulkapil

0

코드의 당신의 주석 라인이 불완전 방식에서, 나는 당신이 자동 완성 기능을 사용하는 가정 IDE의.

첫 번째 제안은 Plot_ViewPlotActivity으로 변경하는 것입니다. 이렇게하면 나중에이 프로젝트로 돌아올 때 혼란을 피할 수 있습니다. 또한 질문을 할 때 혼란을 피할 수 있습니다. 당신의 actualy 질문에 대해서는

, 나는 다음과 같은 ArrayList의 언급하는 가정

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>(); 

먼저 당신이이 Plot_View 클래스의 하지 구성원이라는 것을 인식 할 필요가있다. 그들은 calculationForPlot() 메서드에서 로컬로 선언됩니다. 하지만 전체 Plot_View 클래스를 게시하지 않은 것으로 보입니다. 따라서 다른 ArrayList을 다른 위치에 선언했을 수 있습니다. 위에서 붙여 넣은 ArrayList을 실제로 참조하는 경우 calculationForPlot()의 로컬 변수가 아닌 Plot_View에 멤버 변수로 선언해야합니다. 그런 다음 getter 메서드를 만들어 View 클래스에서 액세스 할 수 있도록합니다.

+0

선생님이 설명해 드리겠습니다.Plot_View는 다른 패키지에서 선언 된 활동이고 PlotView (밑줄은 없음)는 내 사용자 정의보기 클래스이며 그래프가있는 뷰를 만들고 Plot_View 활동을 직접 사용합니다. 지금 grah에 대한 몇 가지 포인트가 필요하고 해당 포인트는 Plot_View Activity에 있습니다. PlotView 클래스 (사용자 정의보기 클래스)에 해당 포인트가 있어야합니다. – Rahulkapil

+0

감사합니다. 먼저'Plot_View'라는 이름을'PlotActivity'로 바꾸라고 제안합시다. 한 글자 만 다른 두 개의 이름을 가짐으로써 혼란을 쉽게 겪을 수 있습니다 (다른 모든 사람들만큼이나). 둘째, 당신의 IDE에서 자동 완성 기능을 사용하려고한다는 나의 첫 번째 가정이 맞습니까? 셋째,'Plot_View' 클래스를 충분히 게시하여 자신이하는 일을 더 명확히하십시오. –

+0

ok..thanxx 아프다. 나중에 자동 완성 기능을 사용하려고하고 병든 게시자 활동 클래스를 기다리고있다. – Rahulkapil

관련 문제