写了一个简单的示例来说明ListView的用法:给定一个单词,下面有四个含义,找出正确的一个,无论是否成功,则会跳转到下一个单词;
主要用到的知识有: findViewById()、  ListView、    AdapterView、 匿名内部类、 ArrayList的一些用法:

下面主要看代码,代码里面注释的很详细,就不再过多赘述:
xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/wordTextView"
android:text="word"
android:textSize="26dp"
android:layout_gravity="center"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/definitions_ListView"> </ListView> </LinearLayout>

java文件:

setOnItemClickListener这是ListView的监听方法;
 package com.chenye.dictionarychange;

 import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 org.w3c.dom.Text; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner; public class MainActivity extends AppCompatActivity { private HashMap<String, String> dictionary; // 存放单词-单词含义的字典
private ArrayList<String> chosenWords; // 将所有单词存放到chosenwords
private String word; //单词
private ArrayList<String> definations; // 含义列表
private ArrayAdapter<String> adapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readAllDefination();
this.chosenWords = new ArrayList<>(this.dictionary.keySet()); // 获取到所有单词并存在在ArrayList中
this.definations = new ArrayList<>();
this.adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, this.definations); // 初始化ArrayAdapter
// 随机选择5个单词含义
pick4Definations();
// 将列表添加到adapter中
ListView defnListView = findViewById(R.id.definitions_ListView);
defnListView.setAdapter(this.adapter);
// 监听这个列表, 用到了匿名内部类
defnListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ListView defnListView = findViewById(R.id.definitions_ListView);
String chooseDefnText = defnListView.getItemAtPosition(i).toString(); // 获取点击位置的字符串, i是list的位置, 大部分情况下i和l(list的第几行)是一样的
String correctDefn = MainActivity.this.dictionary.get(word); // 获取对应单词的解释
// 如果选择和正确结果一致,则:
if(correctDefn.equals(chooseDefnText)){
Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
}
// 选择之后在重新刷新一次列表,即再次挑选一个单词和5个单词的含义
pick4Definations(); }
});
}
public void pick4Definations(){ Collections.shuffle(this.chosenWords); // 打乱单词的存放位置
this.word = this.chosenWords.get(0); // 获取存放单词的list中的第一个单词
TextView wordText = findViewById(R.id.wordTextView);
wordText.setText(this.word); // 显示单词
this.definations.clear(); // 防止结果有脏数据,清空一下单词含义的list
for(int i = 0; i < 4; i++){
String defn = this.dictionary.get(this.chosenWords.get(i)); // 获取到单词对应的意思
this.definations.add(defn);
}
Collections.shuffle(this.definations); // 打乱结果
this.adapter.notifyDataSetChanged(); // 通知adpter改变
}
// 将所有单词及其含义放在dictionary这个字典中
private void readAllDefination(){
Scanner scanner = new Scanner(getResources().openRawResource(R.raw.gre_words));
if(this.dictionary == null){
this.dictionary = new HashMap<>();
}
while (scanner.hasNext()){
String line = scanner.nextLine();
String[] spiece = line.split("\t");
this.dictionary.put(spiece[0], spiece[1]);
}
}
}

Android ListView用法的更多相关文章

  1. android ListView用法介绍

    ListView在Android开发中是比较常用的组件,它是以列表的形式展示内容,并且还可以处理用户的选择与点击等操作: LIstView显示数据一般需要三方面: (1)ListView组件:用来展示 ...

  2. xamarin android listview的用法

    listview也许是用的非常频繁的一个控件之一,下面我写一个xamarin的listview栗子,大家尝一尝xamarin android开发的乐趣.原谅我的大小写吧. listview绑定自定义的 ...

  3. Android listview与adapter用法(BaseAdapter + getView)

    Android listview与adapter用法http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html package ...

  4. Android ListView OnItemLongClick和OnItemClick事件内部细节分享以及几个比较特别的属性

    本文转自 http://blog.sina.com.cn/s/blog_783ede030101bnm4.html 作者kiven 辞职3,4个月在家休息,本以为楼主要程序员逆袭,结果失败告终继续码农 ...

  5. [置顶] android ListView包含Checkbox滑动时状态改变

    题外话: 在xamarin android的开发中基本上所有人都会遇到这个小小的坎,的确有点麻烦,当时我也折腾了好一半天,如果你能看到这篇博客,说明你和我当初也是一样的焦灼,如果你想解决掉这个小小的坎 ...

  6. Android ListView工作原理完全解析,带你从源码的角度彻底理解

    版权声明:本文出自郭霖的博客,转载必须注明出处.   目录(?)[+] Adapter的作用 RecycleBin机制 第一次Layout 第二次Layout 滑动加载更多数据   转载请注明出处:h ...

  7. android ListView 九大重要属性详细分析、

    android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...

  8. Android ListView onItemClick Not Work

    Android ListView onItemClick Not Work ListView item中有Button和RadioButton的时候,它的Item点击事件不起作用,需要设置item的属 ...

  9. Android Meun 用法

    Android Meun 用法 点击菜单实体键弹出菜单:如下图 main_activity.xml <?xml version="1.0" encoding="ut ...

随机推荐

  1. Python __builtin__模块

    你有没有好奇过当我们打开Python后就可以直接使用str(),list(),eval(),print(),max()这样的函数,而不用导入任何模块? 其实原因很简单,就是当我们打开Python解释器 ...

  2. LNA与PA

    LNA是低噪声放大器,主要用于接收电路设计中.因为接收电路中的信噪比通常是很低的,往往信号远小于噪声,通过放大器的时候,信号和噪声一起被放大的话非常不利于后续处理,这就要求放大器能够抑制噪声.PA(功 ...

  3. sk-learning(2)

    sk-learning 学习(2) sklearing 训练评估 针对kdd99数据集使用逻辑回归分类训练 然后进行评估 发觉分数有点高的离谱 取出10%数据494021条,并从中选择四分之一作为测试 ...

  4. java的图形界面初学惯用

    1.单一界面的创建 public void mainFrame() { HashMap<String, Component> views = new HashMap<String, ...

  5. 访问mongo数据库报错

    It looks like you are trying to access MongoDB over HTTP on the native driver port. 出错原因: 1.没有安装mong ...

  6. IOS UIView动画(封装动画)

    ● UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView 将为这些改变提供动画支持 ● 执行动画所需要的工作由UIView类自动完成,但仍要在希望执行动画时通知视 图, ...

  7. UIButton 加载网络图片

    以后就可以 用这个分类   UIButton轻松加载网络图片了, UIButton+WebCache.h #import <UIKit/UIKit.h> @interface UIButt ...

  8. 【转】 Solr的SolrCloud与Master-slave主从模式对比

    第一印象 SolrCloud是Solr4.0引入的,主要应对与商业场景.它很像master-slave,却能自动化的完成以前需要手动完成的操作.利用ZooKeeper这个工具去监控整个Solr集群,以 ...

  9. 02-CSS基础与进阶-day13_2018-09-21-20-05-21

    css3动画 @keyframes 动画名 { 0% { } 100% { } } 元素执行动画 animation: 动画名 运动时间 运动曲线 01运动的汽车.html <!DOCTYPE ...

  10. Java自带工具包StringUtils包含方法

    //导入包 import org.apache.commons.lang3.StringUtils //判断不为空 不包含空格 StringUtils.isNotEmpty(" " ...