如何在自定义列表视图中实现对特定元素的意图?

时间:2017-02-06 12:43:03

标签: android

如何实现点击自定义ListView中的特定元素的意图?

public class CustomerAdapter extends BaseAdapter{

    String[] result;

    enter code here
    Context context;
    int[] imageId;
    private static LayoutInflater inflater=null;
    public CustomerAdapter(MainActivity mainActivity,String[] prgmNameList,int[] prgmImages)
    {
        result=prgmNameList;
        context=mainActivity;
        imageId=prgmImages;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount()
    {
        return result.length;
    }
    @Override
    public Object getItem(int position)
    {
        return position;
    }
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder=new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.program_list, null);
        holder.tv=(TextView) rowView.findViewById(R.id.textView1);
        holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();

            }
        });
        return rowView;
    }
}

1 个答案:

答案 0 :(得分:1)

例如您可以将OnClickListener设置为holder.img,如

holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                context.startActivity(new Intent(context,NewActivity.class));

            }
        });