標籤: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 <ListView16 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; // 將所有單詞存放到chosenwords24 private String word; //單詞25 private ArrayList<String> definations; // 含義列表26 private ArrayAdapter<String> adapter;27 28 @Override29 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); // 初始化ArrayAdapter36 // 隨機播放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 @Override44 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(); // 防止結果有髒資料,清空一下單詞含義的list67 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 }
Android ListView用法