哎,今晚就一个小问题耽误了好长时间。前面两篇日志就是因为开始在List中加入Button后没法正确给这个Button添加事件处理,所以只有先选中某个ListItem之后,再点击下边没在List中的按钮完成逻辑处理。今天终于把这个问题攻克了。哈哈……
问题的关键就是自定义一个继承Adapter的类,我这里是继承的SimpleAdapter。继承BaseAdapte可以参考http://blog.sina.com.cn/s/blog_4fa676f60100edzf.html。继承ArrayAdapter可以参考http://www.vogella.de/articles/Android/article.html
好了,列出代码:
首先是item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView android:id="@+id/showTv"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:textSize="24dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<RelativeLayout android:id="@+id/jjjj"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button android:id="@+id/gointoBt"
android:focusable="false"
android:layout_alignParentRight="true"
android:text="进入"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button android:id="@+id/chooseBt"
android:layout_toLeftOf="@id/gointoBt"
android:layout_marginRight="10dip"
android:text="选择"
android:focusable="false"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
自定义的MySimpleAdapter代码如下:
import ........;
public class MySimpleAdapter extends SimpleAdapter {
//当然 也可以根据自己的需要 写继承 ArrayAdapter或者BaseAdapter的Adapter
private final Context context;
private List<Map<String, Object>> data;
private int resource;
private String[] from;
private int[] to;
public MySimpleAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
this.context=context;
this.data=(List<Map<String, Object>>) data;
this.resource=resource;
this.from=from;
this.to=to;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View rowView = inflater.inflate(resource, null, true); //这里根据SimpleAdapter的本意这样利用xml的布局view
//当然自己也可以完全通过代码构造View
//比如http://blog.sina.com.cn/s/blog_4fa676f60100edzf.html,这种做法
Map<String, Object> medMap=data.get(position);
final TextView[] showTv=new TextView[from.length]; //当然这可可以不都是TextView 可以自己设计
for (int i = 0; i < from.length; i++) { //from和to的长度一样 这里可以灵活运用!!!!!
showTv[i]=(TextView)rowView.findViewById(to[i]);
showTv[i].setText(""+medMap.get(from[i]));
}
Button btn=(Button)rowView.findViewById(R.id.gointoBt);
Button.OnClickListener mOkOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
Log.v("ttttttt", ""+showTv[0].getText());
Toast.makeText(context,""+showTv[0].getText(), Toast.LENGTH_LONG).show();
}
};
btn.setOnClickListener(mOkOnClickListener);
Button btn2=(Button)rowView.findViewById(R.id.chooseBt);
Button.OnClickListener mOkOnClickListener2 = new Button.OnClickListener()
{
public void onClick(View v) {
Log.v("hhhhhhh", ""+showTv[0].getText());
Toast.makeText(context,"abc"+showTv[0].getText(), Toast.LENGTH_LONG).show();
}
};
btn2.setOnClickListener(mOkOnClickListener2);
return rowView; //切记**** 开始这里写出了super的默认方法 浪费不少时间
}
}
Activty代码如下:
import .......;
public class ActivityMain extends Activity {
ListView listview;
List<Map<String,Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("My work");
prepareData();
listview =new ListView(this);
MySimpleAdapter adapter=new MySimpleAdapter(this,data,R.layout.item,new String[]{"uu"},new int[]{R.id.showTv});
listview.setAdapter(adapter);
setContentView(listview);
}
private void prepareData(){
data=new ArrayList<Map<String,Object>>();
Map<String,Object> item;
item=new HashMap<String,Object>();
item.put("uu", "hello");
data.add(item);
item=new HashMap<String,Object>();
item.put("uu", "myyou");
data.add(item);
item=new HashMap<String,Object>();
item.put("uu", "piero");
data.add(item);
}
}
效果截图如下: