gpt4 book ai didi

java - 如何将参数传递给父类(super class)?

转载 作者:行者123 更新时间:2023-12-01 13:45:37 24 4
gpt4 key购买 nike

我正在尝试自定义 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;
}
}

// Activity 文件

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);
}
}

考虑 Activity 代码中的以下行,

CustomList 适配器 = new CustomList (MainActivity.this, web, imageId );

现在我不想将这些数组参数传递到适配器代码中,那么我需要在适配器代码中进行哪些更改?

我在适配器中尝试如下,

    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;
}

但是它给了我编译时错误在显式调用构造函数时无法引用实例字段 web

最佳答案

将此行更改为

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

它应该可以工作。

关于java - 如何将参数传递给父类(super class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com