在按钮上单击重新加载适配器

时间:2012-04-01 14:39:11

标签: android listview

我想在按钮点击后更改listview运行时的数据... 这是我的代码 oncreate工作正常

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Find the ListView resource. 
mainListView = (ListView) findViewById( R.id.mainListView );

// When item is tapped, toggle checked properties of CheckBox and Planet.
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick( AdapterView<?> parent, View item, 
                           int position, long id) {
    Planet planet = listAdapter.getItem( position );
    planet.toggleChecked();
    PlanetViewHolder viewHolder = (PlanetViewHolder) item.getTag();
    viewHolder.getCheckBox().setChecked( planet.isChecked() );
  }
});


// Create and populate planets.
planets = (Planet[]) getLastNonConfigurationInstance() ;
if ( planets == null ) {
  planets = new Planet[] { 
      new Planet("Mercury"), new Planet("Venus"), new Planet("Earth"), 
      new Planet("Mars"), new Planet("Jupiter"), new Planet("Saturn"), 
      new Planet("Uranus"), new Planet("Neptune"), new Planet("Ceres"),
      new Planet("Pluto"), new Planet("Haumea"), new Planet("Makemake"),
      new Planet("Eris")
  };  
}
ArrayList<Planet> planetList = new ArrayList<Planet>();
planetList.addAll( Arrays.asList(planets) );

// Set our custom array adapter as the ListView's adapter.
listAdapter = new PlanetArrayAdapter(this, planetList);
mainListView.setAdapter( listAdapter );}

但是当我想在按钮点击之后更改运行时这样我会收到错误...

private View.OnClickListener onLigyVyber = new View.OnClickListener() {
     public void onClick(View button3) {

                // Create and populate planets.
        planets = (Planet[]) getLastNonConfigurationInstance() ;

          planets = new Planet[] { 
              new Planet("a"), new Planet("b"), new Planet("c"), 
              new Planet("d")
          };  

        ArrayList<Planet> planetList = new ArrayList<Planet>();
        planetList.addAll( Arrays.asList(planets) );

        // Set our custom array adapter as the ListView's adapter.
        listAdapter = new PlanetArrayAdapter(this, planetList);
        mainListView.setAdapter( listAdapter );  

我在这一行出错了

  

构造函数Ventana.PlanetArrayAdapter(new View.OnClickListener(){},ArrayList)未定义

            listAdapter = new PlanetArrayAdapter(this, planetList);

非常感谢。

1 个答案:

答案 0 :(得分:1)

使用:

listAdapter = new PlanetArrayAdapter(getBaseContext(), planetList);

或者您可以使用YOUR_Activity_NAME.this