我试图在我的活动中多次调用 onItemClick 方法,每次我点击我的 listVew 的一个项目时,它的内容都会改变,当我再次点击新内容的一个项目时,我需要再次调用该方法,他是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lesCat=new ArrayList<Categorie>();
CG=new categorieGest();
//Get all categories
lesCat=GC.GetAllCategories();
lvListe= (ListView)findViewById(R.id.listView1);
//Display Root Categories in the listView
adapter = new CategoriesAdapter(this, CG.getRootCategories(lesCat));
lvListe.setAdapter(adapter);
lvListe.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
CG=new categoriesGest();
//If clicked categorie has sub categories, this will display them
//in a new ListeView
if( CG.hasChild( lesCat.get(position) ) )
{
lv = new ListView(this);
lesCat1=CG.getChilds(lesCat, lesCat.get(position) );
CategoriesAdapter adapter = new CategoriesAdapter(this, lesCat1);
lv.setAdapter(adapter);
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
lesCat2=CG.getChilds(lesCat, lesCat1.get(position) );
CategoriesAdapter adapter = new CategoriesAdapter(MainActivity.this, lesCat2);
lv.setAdapter(adapter);
setContentView(lv);
}
});
}
}
有什么更好的方法吗?如果我需要调用 onItemClick 方法超过两次,我必须
lv.setOnItemClickListener(new OnItemClickListener() { //code});
再次重复吗?