Android ListView用法

來源:互聯網
上載者:User

標籤: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用法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.