标签:content shuf private 单词 over extends col change 字符串
写了一个简单的示例来说明ListView的用法:给定一个单词,下面有四个含义,找出正确的一个,无论是否成功,则会跳转到下一个单词;
主要用到的知识有: findViewById()、 ListView、 AdapterView、 匿名内部类、 ArrayList的一些用法:
下面主要看代码,代码里面注释的很详细,就不再过多赘述:
xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:id="@+id/wordTextView" 12 android:text="word" 13 android:textSize="26dp" 14 android:layout_gravity="center"/> 15 <ListView 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:id="@+id/definitions_ListView"> 19 20 </ListView> 21 22 </LinearLayout>
java文件:
setOnItemClickListener这是ListView的监听方法;
1 package com.chenye.dictionarychange; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.AdapterView; 7 import android.widget.ArrayAdapter; 8 import android.widget.ListView; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 12 import org.w3c.dom.Text; 13 14 import java.util.ArrayList; 15 import java.util.Collection; 16 import java.util.Collections; 17 import java.util.HashMap; 18 import java.util.Scanner; 19 20 public class MainActivity extends AppCompatActivity { 21 22 private HashMap<String, String> dictionary; // 存放单词-单词含义的字典 23 private ArrayList<String> chosenWords; // 将所有单词存放到chosenwords 24 private String word; //单词 25 private ArrayList<String> definations; // 含义列表 26 private ArrayAdapter<String> adapter; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 readAllDefination(); 33 this.chosenWords = new ArrayList<>(this.dictionary.keySet()); // 获取到所有单词并存在在ArrayList中 34 this.definations = new ArrayList<>(); 35 this.adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, this.definations); // 初始化ArrayAdapter 36 // 随机选择5个单词含义 37 pick4Definations(); 38 // 将列表添加到adapter中 39 ListView defnListView = findViewById(R.id.definitions_ListView); 40 defnListView.setAdapter(this.adapter); 41 // 监听这个列表, 用到了匿名内部类 42 defnListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 43 @Override 44 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 45 ListView defnListView = findViewById(R.id.definitions_ListView); 46 String chooseDefnText = defnListView.getItemAtPosition(i).toString(); // 获取点击位置的字符串, i是list的位置, 大部分情况下i和l(list的第几行)是一样的 47 String correctDefn = MainActivity.this.dictionary.get(word); // 获取对应单词的解释 48 // 如果选择和正确结果一致,则: 49 if(correctDefn.equals(chooseDefnText)){ 50 Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show(); 51 }else{ 52 Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT).show(); 53 } 54 // 选择之后在重新刷新一次列表,即再次挑选一个单词和5个单词的含义 55 pick4Definations(); 56 57 } 58 }); 59 } 60 public void pick4Definations(){ 61 62 Collections.shuffle(this.chosenWords); // 打乱单词的存放位置 63 this.word = this.chosenWords.get(0); // 获取存放单词的list中的第一个单词 64 TextView wordText = findViewById(R.id.wordTextView); 65 wordText.setText(this.word); // 显示单词 66 this.definations.clear(); // 防止结果有脏数据,清空一下单词含义的list 67 for(int i = 0; i < 4; i++){ 68 String defn = this.dictionary.get(this.chosenWords.get(i)); // 获取到单词对应的意思 69 this.definations.add(defn); 70 } 71 Collections.shuffle(this.definations); // 打乱结果 72 this.adapter.notifyDataSetChanged(); // 通知adpter改变 73 } 74 // 将所有单词及其含义放在dictionary这个字典中 75 private void readAllDefination(){ 76 Scanner scanner = new Scanner(getResources().openRawResource(R.raw.gre_words)); 77 if(this.dictionary == null){ 78 this.dictionary = new HashMap<>(); 79 } 80 while (scanner.hasNext()){ 81 String line = scanner.nextLine(); 82 String[] spiece = line.split("\t"); 83 this.dictionary.put(spiece[0], spiece[1]); 84 } 85 } 86 }
标签:content shuf private 单词 over extends col change 字符串
原文地址:http://www.cnblogs.com/leavescy/p/7845700.html