2016-10-24 2 views
-2

사용자가 다른 작업 대신 작업을 선택하면 내 배열의 요소를 삭제하는 방법을 물어 보았습니다.
내 문자열 배열을 ArrayList로 만드는 것에 대한 답을 얻었습니다.Studio 배열 값 변경 2 부

이제 그 값에 다음 자식을 표시하기 위해 데이터에 id 값을 제공하는 방법을 알고 싶습니다.
예를 들어 사용자가 그리스로 '예'를 누르면 그리스의 도시를 표시하고 싶습니다.

int randome=0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final Button no = (Button) findViewById(R.id.No); 
    final Button yes=(Button) findViewById(R.id.Yes); 
    final TextView tvMessage=(TextView) findViewById(R.id.tvMessage); 

    final ArrayList<String> country = new ArrayList<>(); 
    country.add("Do you wanna see an Action Movie???"); 
    country.add("Do you wanna see a Comedy Movie???"); 
    country.add("Do you wanna see a Drama Movie???"); 
    no.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (randome>=0 && randome<country.size()){ 
       country.remove(randome); 
       Random rand = new Random(); 
       randome = rand.nextInt(country.size()); 
       tvMessage.setText(country.get(randome)); 
      } 
     } 
    }); 
    yes.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Random rand = new Random(); 
      randome = rand.nextInt(country.size()); 
      tvMessage.setText(country.get(randome)); 


     } 
    }); 
} 

그게 내가 ArrayList로 이전 질문의 첫 번째 부분에 사용했던 것입니다. ArrayList로 할 수있는 방법이 없다면, 다음과 같이 문자열 배열로 처리해야합니다.

String[][] countriesAndCities= new String[][]; 
countriesAndCities [0][0] = "United Kingdom"; 
countriesAndCities [0][1] = "London"; 
countriesAndCities [1][0] = "USA"; 
countriesAndCities [1][1] = "Washington"; 
countriesAndCities [2][0] = "India"; 
countriesAndCities [2][1] = "New Delhi"; 
+0

당신이 무엇을 요구하고 있는지 분명하지 않습니다. 그러나 ID가있는 국가/도시를 원할 경우 문자열로만 제한하지 마십시오. 'List cities' 또는 무엇인가 –

+0

을 보유하고있는 'Country' 클래스를 만들고 안드로이드에서 코드를 실행하면 welcome (기본 레이아웃은 환영합니다)이라고 말할 수있는 textView 1 개가 생기고 2 개의 버튼이있을 것입니다. 환영 텍스트에서 예를 누르면 랜드는 국가 (country.add ("당신은 액션 영화를보고 싶니?"))에서 질문을받습니다 country.add ("코메디 영화를보고 싶니? ");) country.add ("드라마 무비를보고 싶니? ");) 레이아웃을 환영하는 데 표시 할 것입니다. 원하는 것은 : 사용자가"보고 싶습니다. Commedy Movie "나는 어떻게 든 다른 배열이나 그와 비슷한 곳에 저장된 영화를 보여주고 싶습니다. – Kostasfra

답변

1

경우 사용자 프레스 yes로 난 당신이 확인할 수

같은 다른 배열이나 뭔가에 저장된 영화를 표시하기 위해 어떻게 든 싶다 "당신이 코미디 영화를보고 싶어합니까" 단추를 클릭 할 때 textview 텍스트가 같으면 일부 목록/배열에서 적절한 데이터를 가져옵니다.

final Random rand = new Random(); 
final TextView tvMessage=(TextView) findViewById(R.id.tvMessage); 

// Some prompts to compare against later 
final String actionMovie = "Do you wanna see an Action Movie???"; 
final ArrayList<String> prompts = new ArrayList<>(); 
prompts.add(actionMovie); 

// Your lists 
final ArrayList<String> actionMovies = new ArrayList<>(); 
actionMovies.add("Terminator 2"); 

// Setup the click event 
findViewById(R.id.Yes).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

     // Check which prompt is shown 
     if (tvMessage.getText().toString().equals(actionMovie)) { 

      // Get a random movie 
      String movieTitle = actionMovies.get(rand.nextInt(actionMovies.size())); 

     } else { } // equals something else 
    } 
+0

그게 전부 내가 이해하고 싶었던, 소중한 대답에 감사드립니다. – Kostasfra