如何将参数传递给超类?

时间:2013-12-05 07:29:16

标签: java android

我正在尝试自定义ListView示例。它的代码如下,

//适配器

public class CustomList extends ArrayAdapter<String>
{

    private final Activity context;
    private final String[] web; 
    private final Integer[] imageId;

    public CustomList ( Activity context, String[] web, Integer[] imageId )
    {
        super ( context, R.layout.list_single, web );
        this.context = context; 
        this.web = web; 
        this.imageId = imageId;
    }

    @Override
    public View getView ( int position, View view, ViewGroup parent )
    {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate( R.layout.list_single, null, true );

        TextView txtTitle = ( TextView ) rowView.findViewById( R.id.txt ); 
        ImageView imageView = ( ImageView ) rowView.findViewById( R.id.img );
        txtTitle.setText( web[position] );
        imageView.setImageResource(imageId[position] );
        return rowView;
    }
}

//活动文件

public class MainActivity extends Activity 
{
    ListView list; 
    String[] web = { "Google Plus","Twitter","Windows","Bing","iTunes","WordPress","Drupal" };
    Integer[] imageId = { R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7 }; 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        CustomList adapter = new CustomList ( MainActivity.this, web, imageId );
        list = ( ListView ) findViewById( R.id.list );
        list.setAdapter(adapter);
    }
}

请考虑活动代码中的以下行

CustomList adapter = new CustomList ( MainActivity.this, web, imageId );

现在我不想将这些数组参数传递给适配器代码然后我需要在适配器代码中进行哪些更改?

我在Adapter中尝试自我,

    String[] web = { "Google Plus","Twitter","Windows","Bing","iTunes","WordPress","Drupal" };
    public CustomList ( Activity context )
    {
        super ( context, R.layout.list_single, web ); // error at this line
        this.context = context; 
    }

但它给了我编译时错误cannot refer to an instance field web while explicitly invoking a constructor

1 个答案:

答案 0 :(得分:1)

将此行更改为

private static final String[] web = new String[] { "Google Plus","Twitter","Windows","Bing","iTunes","WordPress","Drupal" };

它应该有用。