How to create a list with the ability to expand only certain elements?

I am trying to make a list with some extensible elements and some individual elements. I want to have it so that when I click on one element or an expandable child of a list, I can call an intent based on the text of the element.

I assume that extensible lists will work, but is there a way to set items in an extensible list so that they don't have an extensible 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 elements "Zone 1", "Zone 2", "Zone 3" between the points "Atlanta" and "Boston" or as a subset of Atlanta:

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); } } } 
+6
android list android-arrayadapter expandablelistview
source share
2 answers

Working and verified code below:

 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 Atlanta to redraw the list, switching between showing only Atlanta or the expanded list. R.layout.one_city_row.xml is a simple layout with one TextView, R.layout.Expandable_city_row.xml has additional suboptimals as TextView elements, as can be done from the attached code snippet.

Using this approach, you can fully customize the list to suit your needs. In those places where I put Log.i() , you should put your intentions.

+5
source share

If I understand your problem, then what you need is a list of elements that have different elements in the string, depending on the type of element.

I would recommend that you extend the BaseAdapter class and create your own string. This way you determine how the current line will be printed depending on the type, display, or hide.

In the official Android documentation, the Demos API section - ImageSwitcher, you will find an example.

0
source share

All Articles