package com.example.han.lec3ninjaturtles;
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap;
public class MainActivity extends AppCompatActivity { // list of word/definition pairs for game private static final String[] WORDS = { "abate", "to lessen to subside", "abeyance", "suspended action", "abjure", "promise or swear to give up", "abrogate", "repeal or annul by authority", "abstruse", "difficult to comprehand", "acarpous", "effect no longer fertile worn out", "accretion", "the growing of seperate things into one", "abate", "to lessen to subside", "abeyance", "suspended action", "abjure", "promise or swear to give up", "abate", "to lessen to subside", "abeyance", "suspended action", "abjure", "promise or swear to give up" };
private HashMap<String, String> dictionary; private ArrayList<String> list; private ArrayAdapter<String> adapter; private ArrayList<String> fiveDefinition;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
dictionary = new HashMap<String, String>(); list = new ArrayList<String>(); for(int i = 0 ; i < WORDS.length; i+=2){ list.add(WORDS[i]); dictionary.put(WORDS[i], WORDS[i+1]); } fiveDefinition = new ArrayList<String>();
pickRandomWord(); }
private void pickRandomWord(){ ArrayList<String> fiveWords = new ArrayList<String>(); //shuffle word Collections.shuffle(list); for(int i = 0 ; i < 5; i++){ fiveWords.add(list.get(i)); } // 하나 골라서 문제냄 final String theWord = fiveWords.get(0); // list of matched definition
fiveDefinition.clear(); for(String word: fiveWords) fiveDefinition.add(dictionary.get(word)); Collections.shuffle(fiveDefinition);
//listView를 위한 adapter를 만들어야 함 if(adapter==null){ adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fiveDefinition); }else{ adapter.notifyDataSetChanged(); } ListView listView = (ListView)findViewById(R.id.word_list); listView.setAdapter(adapter);
TextView word = (TextView)findViewById(R.id.word); word.setText(theWord);
// clickable하게 만들기 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { // new anonymous 어쩌고를 만들면 위에서 변수 선언할 때 final이어야 함 @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { String defnClicked = fiveDefinition.get(position); String rightAnswer = dictionary.get(theWord); if(defnClicked.equals(rightAnswer)) { /* Toast.makeText(this, "you got it!", Toast.LENGTH_SHORT).show(); * 지금 this는 activity 가 아님 */ Toast.makeText(MainActivity.this, "you got it!", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(MainActivity.this, "wrong!", Toast.LENGTH_SHORT).show(); }
pickRandomWord();
} }); }
} |