如果ListView使用的是自定义的适配器,比如MyArrayAdapter extends ArrayAdapter<String>
那么,如何实现适配器中的点击事件执行activity界面中的方法呢?
实现思路:在自定义适配器MyArrayAdapter 类型中自定义接口和接口方法,然后在activity界面中MyArrayAdapter实例实现这个接口。
较为完整的代码见上文。
1、MyArrayAdapter 关键代码
自定义接口和方法的代码如下:
//列表项的单击事件监听接口 public interface OnMyItemClickListener{ void OnMyItemClick(String itemInfo); } public void setOnMyItemClickListener(OnMyItemClickListener onMyItemClickListener){ this.onMyItemClickListener = onMyItemClickListener; } private OnMyItemClickListener onMyItemClickListener; //列表项的长按事件监听接口 public interface OnMyItemLongClickListener{ void OnMyItemLongClick(String itemInfo); } public void setOnMyItemLongClickListener(OnMyItemLongClickListener onMyItemLongClickListener){ this.onMyItemLongClickListener = onMyItemLongClickListener; } private OnMyItemLongClickListener onMyItemLongClickListener;
使用接口代码如下:
//列表项的单击事件 view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TextView itemText = (TextView) v.findViewById(R.id.listitemText); String itenTitle = itemText.getText().toString(); Log.v("MyArrayAdapter", itenTitle); if(onMyItemClickListener != null){ onMyItemClickListener.OnMyItemClick(itenTitle);//执行Activity界面中的方法 } } }); //列表项的长按事件 view.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { TextView itemText = (TextView) v.findViewById(R.id.listitemText); String itenTitle = itemText.getText().toString(); Log.v("MyArrayAdapter", itenTitle); if(onMyItemLongClickListener != null){ onMyItemLongClickListener.OnMyItemLongClick(itenTitle);//执行Activity界面中的方法 } return false; } });
2、Activity界面的使用代码如下:
private MyArrayAdapter myarrayAdapter;//自定义的历史记录列表的数组适配器 //列表项的点击事件 myarrayAdapter.setOnMyItemClickListener(new OnMyItemClickListener() { @Override public void OnMyItemClick(String itemInfo) { //赋值给AutoCompleteTextView autoTextView.setText(itemInfo); } }); //列表项的长按事件 myarrayAdapter.setOnMyItemLongClickListener(new OnMyItemLongClickListener() { @Override public void OnMyItemLongClick(String itemInfo) { // TODO Auto-generated method stub cleanOneHistory(itemInfo); } });
完整的自定义适配器类代码:
package com.why.autosearch.adapter; import java.util.List; import com.why.autosearch.activity.R; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.TextView; public class MyArrayAdapter extends ArrayAdapter<String>{ private int listitemResourceid;//列表项的布局文件ID private Context mcontext; public MyArrayAdapter(Context context, int resource, List<String> objects) { super(context, resource, objects); listitemResourceid = resource; mcontext = context; } /** * 重写getView*/ public View getView(int position, View convertView, ViewGroup parent) { String listItemStr = getItem(position); View view = null; ViewHolder holder = null; if(convertView == null){ view = LayoutInflater.from(mcontext).inflate(listitemResourceid, parent, false); holder = new ViewHolder(); holder.listitemText = (TextView) view.findViewById(R.id.listitemText); view.setTag(holder); }else{ view = convertView; holder = (ViewHolder) view.getTag(); } //列表项的标题 holder.listitemText.setText(listItemStr); //列表项的单击事件 view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TextView itemText = (TextView) v.findViewById(R.id.listitemText); String itenTitle = itemText.getText().toString(); Log.v("MyArrayAdapter", itenTitle); if(onMyItemClickListener != null){ onMyItemClickListener.OnMyItemClick(itenTitle);//执行Activity界面中的方法 } } }); //列表项的长按事件 view.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { TextView itemText = (TextView) v.findViewById(R.id.listitemText); String itenTitle = itemText.getText().toString(); Log.v("MyArrayAdapter", itenTitle); if(onMyItemLongClickListener != null){ onMyItemLongClickListener.OnMyItemLongClick(itenTitle);//执行Activity界面中的方法 } return false; } }); return view; } class ViewHolder{ TextView listitemText; } //列表项的单击事件监听接口 public interface OnMyItemClickListener{ void OnMyItemClick(String itemInfo); } public void setOnMyItemClickListener(OnMyItemClickListener onMyItemClickListener){ this.onMyItemClickListener = onMyItemClickListener; } private OnMyItemClickListener onMyItemClickListener; //列表项的长按事件监听接口 public interface OnMyItemLongClickListener{ void OnMyItemLongClick(String itemInfo); } public void setOnMyItemLongClickListener(OnMyItemLongClickListener onMyItemLongClickListener){ this.onMyItemLongClickListener = onMyItemLongClickListener; } private OnMyItemLongClickListener onMyItemLongClickListener; }