Hossam Oukli
Hossam Oukli

Reputation: 1296

Call OnItemClick method several times on the same activity:

I'm trying to call the onItemClick method several time on my activity, every time i click on an item of my listVew, its content changes, and when i click again on an item of the new content i need to call the method again, he is my code:

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

                }
            });
        }
    }

is there any way better to do this? what if i need to call the onItemClick method more than two time do i have to repeat lv.setOnItemClickListener(new OnItemClickListener() { //code}); again

Upvotes: 1

Views: 1636

Answers (1)

Kuffs
Kuffs

Reputation: 35661

You are creating a new adapter every time you click on an item which is what is causing your list to change.

Set your adapter once (outside the click handler) and then handle the clicks.

You also only need to set the click handler once.

See this example: http://android-helper.blogspot.co.uk/2011/04/android-listview-onclick-ample.html

UPDATE: Amendment after new information.

Your original question was "Is there a better way to do this?" so this is my opinion.

Your solution of recreating listviews and adapters on every click is unnecessary and wasteful of resources. In my opinion, you should have one listview and one adapter to accomplish the task you have set. The clickhandler on the listview should also be set once only.

In effect, what you are doing when clicking on a category is discarding the current listview and adapter, creating new ones then replacing your view tree when it would be much more efficient to reuse the objects you already have.

When you click on the listviewitem, clear the current adapter, repopulate it with your updated list of categories based on the one that was clicked on and call NotifyDatasetChanged on the adapter which will repopulate your listview with the updated information.

This is not a complete sample but it illustrates the major points.

// Assumes that you have a Custom Listadapter named CatAdapter that returns "Category" views

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           CategoryItem ci=((CategoryItem) getListAdapter()).getItem(position); // Get the clicked category

            RefreshData(ci.getCategoryID); 
        }
    });

    RefreshData(0); // Root Category

}

// Updates the listview with a list of categories
public void RefreshData(int ParentCategoryID) {
    ArrayList<Category> items = Category.GetItem(ParentCategoryID);

    CatAdapter adp = (CatAdapter) getListAdapter();
    // You may need to implement these methods on your adapter
    adp.Clear(); // Clear out the old categories
    adp.AddAll(items); // Add new items to the adapter
    adp.notifyDataSetChanged(); // refresh the UI

}

Upvotes: 2

Related Questions