无法在Adapter的getView方法内的TextView(以编程方式创建)中启动Intent

时间:2014-10-07 10:28:53

标签: android android-intent

编译器显示“方法startActivity(Intent)未定义类型new View.OnClickListener(){}

public View getView(int position , View convertView , ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dharmesh = inflater.inflate(R.layout.fact , parent, false);

    topLayout = (LinearLayout) dharmesh.findViewById(R.id.layoutone);
    TextView tv;

    ImageView iv = (ImageView) dharmesh.findViewById(R.id.image);

    if(list.get(position).getType().equals("Water"))
    {

    }


    else if(list.get(position).getType().equals("Computer"))
    {
        int size = list.get(position).getArrayString().size();
        for(int i = 0 ; i < size ; i++)
        {

            iv.setImageResource(R.drawable.toilet);
            tv = new TextView(context);
            tv.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT , 0 ,1f));
            tv.setText(list.get(position).getArrayString().get(i));
            tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

            tv.setEnabled(true);
            tv.setClickable(true);
            tv.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    Intent it = new Intent(context , SchoolListShow.class);
                    // i.putExtra("School", list.get(0).getSchoolList());
                    startActivity(it);
                }
            });

            topLayout.addView(tv);
        }
    }
    return dharmesh;
}

朋友们有什么问题

1 个答案:

答案 0 :(得分:0)

更改以下代码....

tv.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    Intent it = new Intent(context , SchoolListShow.class);
                    // i.putExtra("School", list.get(0).getSchoolList());
                    startActivity(it);
                }
            });

要...

tv.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    Intent it = new Intent(context , SchoolListShow.class);
                    // i.putExtra("School", list.get(0).getSchoolList());

                    // startActivity is a member of the context, not of the OnClickListener.
                    context.startActivity(it);
                }
            });

在onClickListener类型上,startActivity()方法不存在于上下文或活动中。