2017-04-21 39 views
-2

我想單擊微調框和下一頁來顯示android studio中繪圖的圖像。通過單擊微調框來顯示圖像

這顯示了微調僅

public class navigation extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_navigation); 

    Spinner end = (Spinner) findViewById(R.id.end_spinner); 
    //Create an ArrayAdapter using the string array and a default spinner layout 
    ArrayAdapter<CharSequence> end_adapter = ArrayAdapter.createFromResource(this, R.array.end_point, android.R.layout.simple_spinner_item); 
    //Specify the layout to use when the list of choices appears 
    end_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    //Apply the adapter to the spinner 
    end.setAdapter(end_adapter); 
    end.setOnItemSelectedListener(this); 
} 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    if (parent.getId() == R.id.end_spinner){ 
     if(position >=1 && position <=16){ 
      TextView mytext = (TextView) view; 
      Toast.makeText(this,mytext.getText()+" selected",Toast.LENGTH_LONG).show(); 
      Intent PlanIntent = new Intent(navigation.this, AStar.class); 
      Bundle bundle = new Bundle(); 
      bundle.putInt("classname",position); 
      PlanIntent.putExtras(bundle); 
      startActivity(PlanIntent); 
     } 
     else { 
      Toast.makeText(this,"Please select the destination",Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

     public void onNothingSelected(AdapterView<?> parent) 
     { 
      Spinner start = (Spinner) findViewById(end_spinner); 
      assert start != null; 
      start.setOnItemSelectedListener(this); 
      if (start.getId() != end_spinner) { 

      } 

     } 
@Override 
public void onBackPressed(){ 
    startActivity(new Intent(this,MainActivity.class)); 
    finish(); 
} 
} 

這是下一個頁面,可以顯示圖像

公共類愛仕達擴展AppCompatActivity {

ImageView imageView; 

PhotoViewAttacher mAttacher; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_astar); 

    imageView = (ImageView) findViewById(R.id.imageView); 
    int position = getIntent().getIntExtra("classname", -1); 
    if(position != -1){ 
     int classImg = classImages[position]; 
     imageView.setImageResource(classImg); 
    } 

    mAttacher = new PhotoViewAttacher(imageView); 
} 
} 

我在16類名微調,我想要做的是,當用戶點擊每個類名稱時,它將顯示一張可繪製的圖片。每個班級名稱都有不同的圖像。我的想法是使用,但我不知道代碼。

回答

1

您已經知道如何使用R.array.end_point,因此請爲相應的可繪製對象創建另一個相同大小的xml數組。

您也知道如何使用bundle.putInt("classname",position);,所以您現在需要從意圖中獲取第二個Activity中的整數。

有了這兩個概念,你就可以得到繪製在第二活動中的特定位置,並繪製圖像所

imageView = (ImageView) findViewById(R.id.imageView); 
int position = getIntent().getIntExtra("classname", -1); 
if(position != -1){ 
    // TODO: get some R.array.classImages 
    int classImg = classImages[position]; 
    imageView.setImageResource(classImg); 
} 

我的想法是使用,如果其他

這是錯誤的想法。往上看。沒有別的聲明,只有一個聲明,以確保你已經得到了意圖的額外。所有你需要的是一個整數的可繪製資源

+0

雅。 if(bundle!= null){ }這裏我不知道如何編寫if else代碼 –

+0

你別別的語句......不知道你爲什麼認爲你做 –

+0

以int classPosition = bundle.getInt' –

相關問題