您好,我是Android编程新手
我已成功创建显示列表视图的代码 在每个列表视图中,左侧有一个图像
错误:当我点击图片时,我无法将其链接到另一个页面
请帮帮我 这是我的代码:
CustomAdapter.java
public class CustomAdapter extends BaseAdapter implements OnClickListener {
private OnImageClickListener mListener;
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.tabitem, null);
/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.image);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = (ListModel) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.text.setText(tempValues.getCompanyName());
holder.text1.setText(tempValues.getUrl());
holder.image.setImageResource(res.getIdentifier
("com.example.customlistview24:drawable
/"+tempValues.getImage(),null,null));
vi.setOnClickListener(new OnItemClickListener(position));
//Gabriel Code 28/01/2014
ImageView imageView = (ImageView) vi.findViewById(R.id.image);
imageView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mListener != null) {
Log.v("234", "123");
mListener.onClick(v);
}
}
});
//End of Code
}
return vi;
}
}
public class CustomListViewAndroidExample extends Activity implements
OnImageClickListener {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new
Intent(getApplicationContext(),SingleLockerActivity.class);
startActivity(intent);
}
}
public interface OnImageClickListener {
public void onClick(View view); // Object data [Optional]
}
请帮帮我
答案 0 :(得分:0)
从您发布的代码中,点击图片元素时没有任何反应,因为mListener
永远不会设置为有效对象。所以当代码:
if (mListener != null) {
Log.v("234", "123");
mListener.onClick(v);
}
遇到,它会完全跳过该块并且不采取任何操作。