2013-04-14 4 views
0

저는 안드로이드를 처음 접했고, 어쩌면 내가 끔찍한 잘못을 저지르고 있습니다. 나는 게임에 대한 "생물"클래스의 인스턴스에 대한 세부 정보를 보여주는 특정 활동을 원합니다. 이름, 피해야, 그런 종류의.활동이 제대로 업데이트되지 않습니다.

생물체 데이터가 GUI 개체에 올바르게 표시되도록하는 데 문제가 있습니다. 초기 생성시 (생물체의 이름을 이름 필드에 복사해야하는 경우) 및 손상 표시가 추가 된 경우 (올바른 이미지를 표시하도록 업데이트되지 않는 경우). 이제 문제는 응용 프로그램이로드 및 시작하지만 다음 위젯 중 어느 것도 제대로 업데이트하지 않습니다 것입니다

public class CreatureDetailActivity2 extends Activity 
{ 
    Creature creature; 

    public void addMark(View v) 
    { 
    // connected to the button via android:onClick="addMark" in the XML 
    creature.getTrack().addDamage(DamageType.Normal, 1); 
    refreshDisplay(); 
    new AlertDialog.Builder(this).setTitle(creature.getName()) 
     .setMessage(creature.getTrack().toString()).show(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_creature_detail); 
    creature = new Creature("Example"); 
    refreshDisplay(); 
    } 


    public void refreshDisplay() 
    { 
    final View creatureDetailView = this.getLayoutInflater().inflate(
     R.layout.activity_creature_detail, null); 

    final EditText nameField = (EditText) (creatureDetailView 
     .findViewById(R.id.textbox_creature_name)); 
    nameField.setText(creature.getName()); 

    final ImageView damageBox0 = (ImageView) (creatureDetailView.findViewById(R.id.damageBox0)); 
    damageBox0.setImageResource(R.drawable.__n); 
    // in the full program this does the same for 0 through 9, but this is a sample 
    // also, in the full program, this is a dynamic lookup for the correct pic 
    // but again, this is just a sample version. 
    } 
} 

:

는 여기에 내가 무엇을 내 미니 예입니다. 버튼을 클릭하면 AlertDialog가 표시되고 AlertDialog의 텍스트는 변경되지만 액티비티의 텍스트 필드는 변경되지 않으며 ImageView는 다음과 같이 시작하는 시점에서 언제든지 변경되지 않습니다. 그것을 바꿀 것으로 예상되는 것.

그래서 나는 매우 혼란 스럽습니다. 중요한 항목을 빠뜨리면 프로젝트 설정에 대해 더 게시 할 수 있지만 문제가 무엇인지 확신 할 수 없으므로 내 질문에 무엇을 포함 시킬지 잘 모르겠습니다.

답변

2
final View creatureDetailView = this.getLayoutInflater().inflate(
     R.layout.activity_creature_detail, null); 

활동의 레이아웃을 기본적으로 아무 것도 보지 않고 팽창 한보기 만 반환합니다. setContentView은 실제로 레이아웃을 Activity View 계층 구조로 팽창시키는 것입니다.

레이아웃을 확장 한 후에는 다시 레이아웃 할 필요가 없습니다. 붙어 있지 않은 매달린보기를 참조하지 않고 findViewById을 사용하면됩니다.

이에 refreshDisplay 방법을 변경

: 당신은 완전히 잘못 않기 때문에

public void refreshDisplay() 
{ 
    final EditText nameField = (EditText) findViewById(R.id.textbox_creature_name); 
    nameField.setText(creature.getName()); 

    final ImageView damageBox0 = (ImageView) findViewById(R.id.damageBox0); 
    damageBox0.setImageResource(R.drawable.__n); 
    // in the full program this does the same for 0 through 9, but this is a sample 
    // also, in the full program, this is a dynamic lookup for the correct pic 
    // but again, this is just a sample version. 
} 
1

아무것도 변경되지 않습니다.

당신은 현재 활동의 뷰 요소를 업데이트 할 경우이

View v = findViewById(R.id.element); 
v.setText("text"); 

처럼 할 이것은 단순한 예입니다. 사용 가능한 모든 메서드에 액세스 할 수 있도록 반환 된 요소를 올바른 형식으로 캐스팅해야합니다.

잘못된 점은 레이아웃을 다시 팽창하려고하는 것입니다.

관련 문제