+ Reply to Thread
Results 1 to 2 of 2

Thread: How can I create a list with only certain items expandable?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    How can I create a list with only certain items expandable?

    I am trying to compose a list with some items expandable and some single items. I wish to have it so that when either a single item or expandable list child is clicked, I can call an intent based on the text of the item.

    I suppose expandable lists would work, but is there a way to set items in an expandable list so that they don't have the expandable list icon? Should I use something other than a string array?

    What is the best way to do this?

    Thanks

    Below is my incomplete code, I would like to insert the items "Zone 1", "Zone 2", "Zone 3" between the items "Atlanta" and "Boston" or as a subset of Atlanta:
    Code:
    package test.lists.special;
    
    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class SpecialList extends ListActivity{
    TextView toptext;
    String[] items={"Atlanta", "Boston", "Chicago", "Dallas"};
    
    @Override
    
     public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      setListAdapter(new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1,
      items));
      toptext=(TextView)findViewById(R.id.toptext);
     }
    
    public void onListItemClick(ListView parent, View v, int position,
     long id) {
         Intent intent = new Intent();
    
         if (parent.getItemAtPosition(position)=="Atlanta")
         {
               //THIS IS WHERE I WISH TO INSERT "ZONE 1", "ZONE 2", "ZONE 3" 
                      //between Atlanta and Boston in the list
         }
         else if (parent.getItemAtPosition(position)=="Boston")
         {
              intent.setClass(this, test.lists.special.Boston.class);
               startActivity(intent); 
         }
         else if (parent.getItemAtPosition(position)=="Chicago")
         {
                 intent.setClass(this, test.lists.special.Chicago.class);
                  startActivity(intent); 
         }
         else if (parent.getItemAtPosition(position)=="Dallas")
         {
                intent.setClass(this, test.lists.special.Dallas.class);
                 startActivity(intent); 
         }
         else if (parent.getItemAtPosition(position)=="Zone 1")
         {
                intent.setClass(this, test.lists.special.Atlanta.Zone1.class);
                 startActivity(intent); 
         }
         else if (parent.getItemAtPosition(position)=="Zone 2")
         {
                intent.setClass(this, test.lists.special.Atlanta.Zone2.class);
                 startActivity(intent); 
         }
         else if (parent.getItemAtPosition(position)=="Zone 3")
         {
                intent.setClass(this, test.lists.special.Atlanta.Zone3.class);
                 startActivity(intent); 
         }
        }
    
    }

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    14
    Working and tested code below:
    Code:
    import android.app.ListActivity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View; 
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter; 
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ListView; 
    import android.widget.TextView; 
    import android.widget.Toast; 
    
    public class SpecialList extends ListActivity{ 
    
        TextView toptext; 
        String[] items={"Atlanta", "Boston", "Chicago", "Dallas"};
        private CityListAdapter mListOfCities;
    
        boolean mAtlantaListExpanded;
    
        @Override 
        public void onCreate(Bundle icicle) { 
            super.onCreate(icicle); 
            setContentView(R.layout.main); 
    
            mAtlantaListExpanded = false;
            mListOfCities = new CityListAdapter();
            setListAdapter(mListOfCities);
    
            for (int n=0; n < items.length; n++)
            {
                mListOfCities.add(items[n]);
            }
        } 
    
        public void onListItemClick(ListView parent, View v, int position, long id) 
        { 
             Intent intent = new Intent(); 
    
             if (parent.getItemAtPosition(position)=="Atlanta") 
             { 
                 mAtlantaListExpanded = !mAtlantaListExpanded;
                 mListOfCities.clear();
                 for (int n=0; n < items.length; n++)
                 {
                    mListOfCities.add(items[n]);
                 }           
                 Log.i("SpecialList", "Atlanta");            
             } 
             else if (parent.getItemAtPosition(position)=="Boston") 
             { 
                 Log.i("SpecialList", "Boston");
             } 
             else if (parent.getItemAtPosition(position)=="Chicago") 
             { 
                 Log.i("SpecialList", "Chicago");
             } 
             else if (parent.getItemAtPosition(position)=="Dallas") 
             { 
                 Log.i("SpecialList", "Dallas");
             } 
        }
    
        class CityListAdapter extends ArrayAdapter<String> {    
    
            CityListAdapter() {         
                super(SpecialList.this, R.layout.one_city_row);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) 
            {
                View row = null;
                LayoutInflater inflater = getLayoutInflater();
    
                if ((position == 0) && (!mAtlantaListExpanded))
                {
                    row = inflater.inflate(R.layout.expandable_city_row, parent, false);
                    TextView city = (TextView) row.findViewById(R.id.Expandable_TextView01);
                    city.setText(items[position]);
    
                    TextView cityZone1 = (TextView) row.findViewById(R.id.Zone_TextView01);
                    cityZone1.setText("Zone 1");
    
                    TextView cityZone2 = (TextView) row.findViewById(R.id.Zone_TextView02);
                    cityZone2.setText("Zone 2");
    
                    TextView cityZone3 = (TextView) row.findViewById(R.id.Zone_TextView03);
                    cityZone3.setText("Zone 3");
    
                    cityZone1.setOnClickListener(
                        new Button.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                 Log.i("SpecialList", "Zone 1");
                            }
                        }
                    );
    
                    cityZone2.setOnClickListener(
                        new Button.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                 Log.i("SpecialList", "Zone 2");
                            }
                        }
                    );
    
                    cityZone3.setOnClickListener(
                        new Button.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                 Log.i("SpecialList", "Zone 3");
                            }
                        }
                    );              
                }
                else
                {
                    row = inflater.inflate(R.layout.one_city_row, parent, false);
                    TextView city = (TextView) row.findViewById(R.id.City_TextView01);
                    city.setText(items[position]);
                }                   
    
                return(row);
            }
        }
    }
    Click on "Atlanta" cause redraw of the list, toggling between showing only "Atlanta" or expanded list. R.layout.one_city_row.xml is simple layout with one TextView, R.layout.Expandable_city_row.xml has additional suboptions as TextView elements, as may be concluded from the attached code snippet.

    Using this approach you may customize list completely according to your needs. On the places where I put Log.i(), you should put your intents.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts