程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> android-Android 如何在適配器中啟動intent跳轉activity

android-Android 如何在適配器中啟動intent跳轉activity

編輯:編程解疑
Android 如何在適配器中啟動intent跳轉activity

如題 如何在適配器中啟動intent跳轉activity

最佳回答:


你在適配器中傳入context就可以了。

 public class ProductListAdapter extends BaseAdapter {
    private Context context;
    private List<String> listData;
    private ViewHolderProduct viewHolderProduct;
    public ProductListAdapter(Context context,List<String> listData){
        this.context = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData == null ? 0 : listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData == null ? null : listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null || convertView.getTag() == null){
            viewHolderProduct = new ViewHolderProduct();
            convertView = LayoutInflater.from(context).inflate(R.layout.product_item_layout,null);
            viewHolderProduct.iv_pro_pic = (ImageView) convertView.findViewById(R.id.iv_imageView);
            viewHolderProduct.tv_pro_date = (TextView) convertView.findViewById(R.id.tv_pro_date);
            viewHolderProduct.tv_pro_desc = (TextView) convertView.findViewById(R.id.tv_pro_desc);
            viewHolderProduct.tv_pro_from = (TextView) convertView.findViewById(R.id.tv_pro_from);
            viewHolderProduct.tv_pro_name = (TextView) convertView.findViewById(R.id.tv_pro_name);
            viewHolderProduct.tv_pro_number = (TextView) convertView.findViewById(R.id.tv_pro_number);
            viewHolderProduct.tv_pro_price = (TextView) convertView.findViewById(R.id.tv_pro_price);
            convertView.setTag(viewHolderProduct);
        }else{
            viewHolderProduct = (ViewHolderProduct) convertView.getTag();
        }
        viewHolderProduct.tv_pro_price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LoginActivity.class);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolderProduct{
        TextView tv_pro_number,tv_pro_name,tv_pro_desc,tv_pro_from,tv_pro_date,tv_pro_price;
        ImageView iv_pro_pic;
    }

}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved