今天遇到一个问题:android中ListView点击和里边ImageView点击不能同时生效问题解决。
研究ListView,首先ListView的适配器类型分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter,其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用setAdapter()完成适配的最后工作。
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件,HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。
然后我在使用SimpleAdapter的时候,希望在SimpleAdapter中添加的ImageView图片并且添加点击事件,我通过ID找到这个组件,然后给组件添加单击事件:
img.setOnClickListener(new View.OnClickListener() {
<wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr>public void onClick(View v) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> Toast.makeText(FriendListTwitter.this, "click add friend.",<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> Toast.LENGTH_SHORT).show();<br><wbr><wbr><wbr><wbr>}<br> });</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
但是程序一直出现NullPointerException。
原来以为只要给图片添加clickable和focusable属性就行了,可是还是出现异常时一直出现NullPointerException。
后来看到网上一篇文章后才明白,文章的关键内容是:
但是有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。
Xml代码:
meetinghistory_listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<wbr> xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"<br><wbr> android:layout_width="fill_parent"<br><wbr> android:layout_height="50dip"<br><wbr> android:orientation="horizontal"><br><wbr> <LinearLayout<br><wbr><wbr>android:id="@+id/history_linearlayout01"<br><wbr><wbr>android:layout_width="fill_parent"<br><wbr><wbr>android:layout_height="fill_parent"<br><wbr><wbr>android:layout_toLeftOf="@+id/history_linearlayout02"<br><wbr><wbr><wbr> android:orientation="vertical"<wbr><wbr><br><wbr><wbr><wbr> android:descendantFocusability="blocksDescendants"></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr> <TextView<br><wbr><wbr><wbr>android:id="@+id/history_item_theme"<br><wbr><wbr><wbr>android:textColor="#ffffff"<br><wbr><wbr><wbr>android:layout_weight="1"<br><wbr><wbr><wbr>android:layout_width="fill_parent"<br><wbr><wbr><wbr>android:layout_height="wrap_content"<br><wbr><wbr><wbr>android:textSize="19sp"<br><wbr><wbr><wbr>android:gravity="center_vertical"<br><wbr><wbr><wbr>android:text="ItemName"<br><wbr><wbr><wbr>android:textStyle="bold"<br><wbr><wbr><wbr>android:layout_marginLeft="10dip"<br><wbr><wbr><wbr>/><br><wbr><wbr><wbr><TextView<br><wbr><wbr><wbr>android:id="@+id/history_item_time"<br><wbr><wbr><wbr>android:layout_weight="1"<br><wbr><wbr><wbr>android:layout_width="fill_parent"<br><wbr><wbr><wbr>android:layout_height="wrap_content"<br><wbr><wbr><wbr>android:textSize="16sp"<br><wbr><wbr><wbr>android:gravity="center_vertical"<br><wbr><wbr><wbr>android:textColor="#999999"<br><wbr><wbr><wbr>android:text="ItemNumber"<br><wbr><wbr><wbr>android:layout_marginLeft="10dip"<br><wbr><wbr><wbr>/><wbr><wbr><wbr><br><wbr></LinearLayout><br><wbr><LinearLayout<br><wbr><wbr><wbr>android:id="@+id/history_linearlayout02"<br><wbr><wbr><wbr>android:layout_width="50dip"<br><wbr><wbr><wbr>android:layout_height="50dip"<br><wbr><wbr><wbr>android:layout_alignParentRight="true"><br><wbr><wbr><ImageView<br><wbr><wbr><wbr>android:id="@+id/history_imagebutton"<br><wbr><wbr><wbr>android:layout_height="wrap_content"<br><wbr><wbr><wbr>android:layout_width="wrap_content"<br><wbr><wbr><wbr>android:layout_gravity="center"<br><wbr><wbr><wbr>android:background="#000000"<br><wbr><wbr><wbr>android:focusable="false"<br><wbr><wbr><wbr>android:clickable="true"<wbr><wbr><wbr><br><wbr><wbr><wbr>android:src="@drawable/right_image"/><br><wbr></LinearLayout></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
</RelativeLayout>
<wbr></wbr>
meetinghistory.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<wbr>android:layout_width="wrap_content" android:layout_height="wrap_content"<br><wbr>android:descendantFocusability="blocksDescendants"></wbr></wbr>
<wbr><ListView android:layout_width="wrap_content"<br><wbr><wbr>android:layout_height="wrap_content" android:id="@+id/meetinghisrory_list"></ListView></wbr></wbr></wbr>
</RelativeLayout>
Java代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.lskl.mtalk.adr.model.ToolBarMaker;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.GridView;
public class MeetingHistoryListActivi<wbr>ty extends Activity implements<br><wbr><wbr>OnItemClickListener, OnClickListener {<br><wbr>private ListView mListView;<br><wbr>private List<Map<String, Object>> mList;<br><wbr>private MyAdapter mAdapter;</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>@Override<br><wbr>protected void onCreate(Bundle savedInstanceState) {<br><wbr><wbr>super.onCreate(savedInstanceState);<br><wbr><wbr>setContentView(R.layout.meetinghistory);<br><wbr><wbr>mListView = (ListView) findViewById(R.id.meetinghisrory_list);<br><wbr><wbr>mListView.setOnItemClickListener(this);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>mAdapter = new MyAdapter(this);<br><wbr><wbr>mListView.setAdapter(mAdapter);<br><wbr><wbr>mList = initMeetingHistoryList();<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>private List<Map<String, Object>> initMeetingHistoryList() {<br><wbr><wbr>List<Map<String, Object>> listItem = new ArrayList<Map<String, Object>>();</wbr></wbr></wbr>
<wbr><wbr>HashMap<String, Object> map = new HashMap<String, Object>();<br><wbr><wbr>map.put("ItemTheme", "Beijing China1");<br><wbr><wbr>map.put("ItemTime", "4/26 17:00");<br><wbr><wbr>map.put("ItemImage", R.drawable.right_image);<br><wbr><wbr>listItem.add(map);<br><wbr><wbr>map = new HashMap<String, Object>();<br><wbr><wbr>map.put("ItemTheme", "Beijing China2");<br><wbr><wbr>map.put("ItemTime", "2012-4-26-17:00");<br><wbr><wbr>map.put("ItemImage", R.drawable.right_image);<br><wbr><wbr>listItem.add(map);<br><wbr><wbr>map = new HashMap<String, Object>();<br><wbr><wbr>map.put("ItemTheme", "Beijing China3");<br><wbr><wbr>map.put("ItemTime", "2012-4-26-17:00");<br><wbr><wbr>map.put("ItemImage", R.drawable.right_image);<br><wbr><wbr>listItem.add(map);<br><wbr><wbr>map = new HashMap<String, Object>();<br><wbr><wbr>map.put("ItemTheme", "Beijing China4");<br><wbr><wbr>map.put("ItemTime", "2012-4-26-17:00");<br><wbr><wbr>map.put("ItemImage", R.drawable.right_image);<br><wbr><wbr>listItem.add(map);<br><wbr><wbr>map = new HashMap<String, Object>();<br><wbr><wbr>map.put("ItemTheme", "Beijing China5");<br><wbr><wbr>map.put("ItemTime", "2012-4-26-17:00");<br><wbr><wbr>map.put("ItemImage", R.drawable.right_image);<br><wbr><wbr>listItem.add(map);<br><wbr><wbr>return listItem;<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>class MyAdapter extends BaseAdapter {<br><wbr><wbr>private LayoutInflater mInflater;</wbr></wbr></wbr>
<wbr><wbr>@Override<br><wbr><wbr>public int getCount() {<br><wbr><wbr><wbr>if (mList == null) {<br><wbr><wbr><wbr><wbr>return 0;<br><wbr><wbr><wbr>} else {<br><wbr><wbr><wbr><wbr>return mList.size();<br><wbr><wbr><wbr>}<br><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>public MyAdapter(Context context) {<br><wbr><wbr><wbr>this.mInflater = LayoutInflater.from(context);<br><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>@Override<br><wbr><wbr>public Object getItem(int position) {<br><wbr><wbr><wbr>return mList.get(position);</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>}</wbr></wbr>
<wbr><wbr>@Override<br><wbr><wbr>public long getItemId(int position) {<br><wbr><wbr><wbr>return position;</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>}</wbr></wbr>
<wbr><wbr>@Override<br><wbr><wbr>public View getView(int position, View convertView, ViewGroup parent) {<br><wbr><wbr><wbr>ViewHolder holder;<br><wbr><wbr><wbr>if (convertView == null) {<br><wbr><wbr><wbr><wbr>holder = new ViewHolder();<br><wbr><wbr><wbr><wbr>convertView = mInflater.inflate(<br><wbr><wbr><wbr><wbr><wbr><wbr>R.layout.meetinghistory_listitem, null);<br><wbr><wbr><wbr><wbr>holder.username = (TextView) convertView<br><wbr><wbr><wbr><wbr><wbr><wbr>.findViewById(R.id.history_item_theme);<br><wbr><wbr><wbr><wbr>holder.location = (TextView) convertView<br><wbr><wbr><wbr><wbr><wbr><wbr>.findViewById(R.id.history_item_time);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>holder.img = (ImageView) convertView<br><wbr><wbr><wbr><wbr><wbr><wbr>.findViewById(R.id.history_imagebutton);<br><wbr><wbr><wbr><wbr>convertView.setTag(holder);<br><wbr><wbr><wbr>} else {<br><wbr><wbr><wbr><wbr>holder = (ViewHolder) convertView.getTag();<br><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>holder.username.setText((String) mList.get(position).get(<br><wbr><wbr><wbr><wbr><wbr>"ItemTheme"));<br><wbr><wbr><wbr>holder.location.setText((String) mList.get(position)<br><wbr><wbr><wbr><wbr><wbr>.get("ItemTime"));<br><wbr><wbr><wbr>holder.img.setBackgroundResource((Integer) mList.get(position).get(<br><wbr><wbr><wbr><wbr><wbr>"ItemImage"));</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>holder.img.setOnClickListener(new OnClickListener() {</wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr>public void onClick(View v) {<br><wbr><wbr><wbr><wbr><wbr>Intent intent = new Intent();<br><wbr><wbr><wbr><wbr><wbr>intent.setClass(MeetingHistoryListActivi<wbr>ty.this,<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>MianMenu.class);<br><wbr><wbr><wbr><wbr><wbr>startActivity(intent);<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>});</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>return convertView;<br><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr>
<wbr>}</wbr>
<wbr>class ViewHolder {<br><wbr><wbr>ImageView img;<br><wbr><wbr>TextView username;<br><wbr><wbr>TextView location;<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr>public void onClick(View v) {<br><wbr>}</wbr></wbr>
<wbr>//添加点击</wbr>
<wbr>@Override<br><wbr>public void onItemClick(AdapterView<?> parent, View view,<br><wbr><wbr><wbr><wbr><wbr>int position,long id) {</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>switch(position){</wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr> case 0:</wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr> break;</wbr></wbr></wbr></wbr></wbr>
<wbr> }<br><wbr>}<br> }<br></wbr></wbr>
下面将对上述代码,做详细的解释,listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到listView的长度(这也是为什么在开始的第一张图特别的标出列表长度),然后根据这个长度,调用getView()逐一绘制每一行。如果你的getCount()返回值是0的话,列表将不显示同样return 1,就只显示一行。
系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。当手动完成适配时,必须手动映射数据,这需要重写getView()方法。系统在绘制列表的每一行的时候将调用此方法。getView()有三个参数,position表示将显示的是第几行,covertView是从布局文件中inflate来的布局。我们用LayoutInflater的方法将定义好的vlist2.xml文件提取成View实例用来显示。然后将xml文件中的各个组件实例化(简单的findViewById()方法)。这样便可以将数据对应到各个组件上了。但是按钮为了响应点击事件,需要为它添加点击监听器,这样就能捕获点击事件。至此一个自定义的listView就完成了,现在让我们回过头从新审视这个过程。系统要绘制ListView了,他首先获得要绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?调用getView()函数。在这个函数里面首先获得一个View(实际上是一个ViewGroup),然后再实例并设置各个组件,显示之。好了,绘制完这一行了。那再绘制下一行,直到绘完为止。在实际的运行过程中会发现listView的每一行没有焦点了,这是因为Button抢夺了listView的焦点,只要布局文件中将Button设置为没有焦点就OK了。
<wbr></wbr>
另外,通过这种方法不可以将ListView和ImageView同时添加了监听,通过网上搜索,原来是没有在xml中添加部分属性,在list的配置xml的根节点添加属性android:descendantFocusability="blocksDescendants",还有就是在要添加事件的控件上添加android:focusable="false",上边的代码中我已经添加过了