Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The image of what you want to achieve is a good idea. </p> <p>I suggest you have only one second activity for showing your layout with the image on top and the listview at the bottom. You can pass information on which image to show and which data to show in the listview to that activity from your first (image-button) activity. This is done by putting the information into the Intent object using the <code>putExtra()</code> methods.</p> <p>Then in the second activity, you read that information from the <code>Intent</code> and act accordingly by setting the image and initializing the Adapter of the Listview.</p> <p>Btw., you do not need a custom <code>ListView</code> for that layout, just use a <code>LinearLayout</code> and put a <code>ImageView</code> and a <code>ListView</code> into it.</p> <p><strong>Example Code</strong></p> <p>I added some example code, that should explain how to do it. It's not tested, so there might well be typos or ommisions in there, but it should at least give you a start and an idea how to do it.</p> <p><em><strong>Main Activity</em></strong></p> <p>In your main-activity do this, to pass the ID of the pressed button along to the listview-activity.</p> <pre><code>public void onClick(View v) { switch(v.getId()) { // if one of the image buttons is pressed... case R.id.imageButton1: case R.id.imageButton2: case R.id.imageButton3: case R.id.imageButton4: case R.id.imageButton5: case R.id.imageButton6: Intent intent = new Intent(this, MyListViewActivity.class); // pass ID of pressed button to listview-activity intent.putExtra("buttonId", v.getId()); startActivity(intent); break; // here you could place handling of other clicks if necessary... } } </code></pre> <p><em><strong>MyListViewActivity</em></strong></p> <p>This will be your second activity, which I called ListViewActivity here. In onResume() we check for the extra data in the intent (any Activity has an Intent by which it got started).</p> <pre><code>public MyListViewActivity extends Activity { public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.activity_mylistview); } public void onResume() { super.onResume(); int buttonId = getIntent().getIntExtra("buttonId", 0); int buttonIdx = getButtonIdx(buttonId); // find and set image according to buttonId int imageId = IMAGE_IDS[buttonIdx]; // image to show for given button ImageView imageView = (ImageView)findViewById(R.id.imageView); imageView.setImageResource(imageId); // find and set listview imtes according to buttonId String[] items = LISTVIEW_DATA[buttonIdx]; // listview items to show for given button ListView listView = (ListView)findViewById(R.id.listView); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); listView.setListAdapter(adapter); } // a little helper to map ids to array indices // to be able to fetch the correct image and listview data later private final static int[] BUTTON_IDS = new int[] { R.id.imageButton1, R.id.imageButton2, R.id.imageButton3, R.id.imageButton4, R.id.imageButton5, R.id.imageButton6 }; // 6 images private final static int[] IMAGE_IDS = new int[] { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5, R.drawable.image6 }; // 6 different sets of strings for the listviews private final static String[][] LISTVIEW_DATA = new String[][] { {"First A", "First B", "First C"}, {"Second A, "Second B", Second C"}, {"Third A, "Third B", Third C"}, {"Forth A, "Forth B", Forth C"}, {"Fifth A, "Fifth B", Fifth C"}, {"Sixth A, "Sixth B", Sixth C"}, }; // map button id to array index static private int getButtonIdx(int id) { for(int i = 0; i&lt;BUTTON_IDS.length; i++) { if (BUTTON_IDS[i] == id) return i; } return 0; // should not happen } } </code></pre> <p>*<strong>activity_mylistview.xml*</strong></p> <p>A simple example for the layout you wanted in your second picture, you'll want to improve this.</p> <pre><code>&lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ImageView android:id="imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;ListView android:id="listView" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload