作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试自定义 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/
我是一名优秀的程序员,十分优秀!