简体   繁体   English

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

[英]on button click reload adapter

I want to change data on listview runtime after button click... here is my code oncreate works fine 我想在单击按钮后在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 );}

but when i want to change it runtime after button click like this i get error ... 但是当我想像这样单击按钮后更改运行时时,我得到错误提示...

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

i got error on this line 我在这条线上有错误

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

            listAdapter = new PlanetArrayAdapter(this, planetList);

Thanks a lot. 非常感谢。

use : 采用 :

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

or you can use YOUR_Activity_NAME.this 或者您可以使用YOUR_Activity_NAME.this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM