ListView 显示两列并添加监听(Button 控件除外)

2024-05-24 00:18

本文主要是介绍ListView 显示两列并添加监听(Button 控件除外),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

由于项目需要,查阅资料自己做的demo,总共两个类,两个布局文件
1.Activity

package com.example.sqlitelistview;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleAdapter;public class MainActivity extends Activity { private ListView listView = null; private List<String> nameList = null; private List<String> ageList = null; private MyAdapter adapter = null; // private List<HashMap<String, String>> list= null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list); adapter = new MyAdapter(this, getNameList(), getAgeList()); listView.setAdapter(adapter); // listView.setAdapter(getAdapter()); //设置监听 // listView.setOnItemClickListener(new OnItemClickListener() { // public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { // } // }); } public List<String> getAgeList(){ ageList = new ArrayList<String>(); for(int i = 1 ; i <= 20 ; i ++){ ageList.add(String.valueOf(i)); } return ageList; } public List<String> getNameList(){ nameList = new ArrayList<String>(); String firstName = "张"; for(int i = 1 ; i <= 20 ; i ++){ nameList.add(firstName + i); } return nameList; } // public List<HashMap<String,String>> getData(){ // HashMap<String, String> userMap = null; // list = new ArrayList<HashMap<String,String>>(); // String name = "张"; // for(int i = 22 ; i < 26 ; i ++){ // userMap = new HashMap<String, String>(); // userMap.put("name",name + i ); // userMap.put("age",String.valueOf(i) ); // list.add(userMap); // } // return list; // } // public SimpleAdapter getAdapter(){ // return new SimpleAdapter(getApplicationContext(), getData(), R.layout.adapter_list, new String[]{"name","age"}, new int[]{R.id.name,R.id.age}); // // } }

2.Adapter

package com.example.sqlitelistview;import java.util.ArrayList; import java.util.List;import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast;public class MyAdapter extends BaseAdapter { private Listener listener = null; private Context context; private List<String> name = new ArrayList<String>(); private List<String> age = new ArrayList<String>(); private LayoutInflater inflater; public MyAdapter(Context context,List<String> name, List<String> age){ this.context = context; this.name = name; this.age = age; inflater=LayoutInflater.from(context); listener = new Listener(); } public int getCount() { return name.size(); } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null){ convertView = inflater.inflate(R.layout.adapter_list, null); } TextView nameView = (TextView) convertView.findViewById(R.id.name); TextView ageView = (TextView) convertView.findViewById(R.id.age); nameView.setText(name.get(position)); ageView.setText(age.get(position)); //设置监听 nameView.setOnClickListener(listener); ageView.setOnClickListener(listener); return convertView; } class Listener implements OnClickListener{ public void onClick(View v) { TextView view = (TextView) v; switch(v.getId()){ case R.id.name: Toast.makeText(context, view.getText(), 0).show(); break; case R.id.age: Toast.makeText(context, view.getText(), 0).show(); break; } } } }


3.layout document

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" ></ListView> </LinearLayout>

4.ListView每列布局

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:textSize="12pt" android:clickable="true" ></TextView> <TextView android:id="@+id/age" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:textSize="12pt" android:clickable="true" > </TextView></LinearLayout>

这篇关于ListView 显示两列并添加监听(Button 控件除外)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/996826

相关文章

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

Flutter监听当前页面可见与隐藏状态的代码详解

《Flutter监听当前页面可见与隐藏状态的代码详解》文章介绍了如何在Flutter中使用路由观察者来监听应用进入前台或后台状态以及页面的显示和隐藏,并通过代码示例讲解的非常详细,需要的朋友可以参考下... flutter 可以监听 app 进入前台还是后台状态,也可以监听当http://www.cppcn

spring @EventListener 事件与监听的示例详解

《spring@EventListener事件与监听的示例详解》本文介绍了自定义Spring事件和监听器的方法,包括如何发布事件、监听事件以及如何处理异步事件,通过示例代码和日志,展示了事件的顺序... 目录1、自定义Application Event2、自定义监听3、测试4、源代码5、其他5.1 顺序执行

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

如何设置vim永久显示行号

《如何设置vim永久显示行号》在Linux环境下,vim默认不显示行号,这在程序编译出错时定位错误语句非常不便,通过修改vim配置文件vimrc,可以在每次打开vim时永久显示行号... 目录设置vim永久显示行号1.临时显示行号2.永www.chinasem.cn久显示行号总结设置vim永久显示行号在li

C#实现WinForm控件焦点的获取与失去

《C#实现WinForm控件焦点的获取与失去》在一个数据输入表单中,当用户从一个文本框切换到另一个文本框时,需要准确地判断焦点的转移,以便进行数据验证、提示信息显示等操作,本文将探讨Winform控件... 目录前言获取焦点改变TabIndex属性值调用Focus方法失去焦点总结最后前言在一个数据输入表单

电脑显示hdmi无信号怎么办? 电脑显示器无信号的终极解决指南

《电脑显示hdmi无信号怎么办?电脑显示器无信号的终极解决指南》HDMI无信号的问题却让人头疼不已,遇到这种情况该怎么办?针对这种情况,我们可以采取一系列步骤来逐一排查并解决问题,以下是详细的方法... 无论你是试图为笔记本电脑设置多个显示器还是使用外部显示器,都可能会弹出“无HDMI信号”错误。此消息可能

SpringBoot整合Canal+RabbitMQ监听数据变更详解

《SpringBoot整合Canal+RabbitMQ监听数据变更详解》在现代分布式系统中,实时获取数据库的变更信息是一个常见的需求,本文将介绍SpringBoot如何通过整合Canal和Rabbit... 目录需求步骤环境搭建整合SpringBoot与Canal实现客户端Canal整合RabbitMQSp

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE