我为listview创建了包含文本和图像的自定义适配器,点击特定列表项我必须打开另一个活动,但我无法触发listview点击事件,下面是我的代码。
感谢。
适配器
public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private Context myCtx;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);
myCtx = ctx;
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
@Override
public int getCount() {
return mStrings.length;
}
@Override
public String getItem(int position) {
return mStrings[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}
主要活动
public class OurWishingWellActivity extends ListActivity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.list_view_image);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.reg_item_names);
TypedArray icons = res.obtainTypedArray(R.array.reg_icons);
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item,
options, icons));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
添加监听器
@Override
public View getView(int position,View convertView,ViewGroup parent){ convertView = mInflater.inflate(mViewResourceId,null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
convertView .setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity();
}
});
return convertView;
}
答案 1 :(得分:0)
尊敬的Aafag#
你已经在下面列出了listview的点击。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(AdvancedListViewActivity.this, "working", Toast.LENGTH_SHORT).show();
System.out.println("working!!!!!!!!!");
}