Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use One Class for Multiple Listview Item Clicks
    text
    copied!<p>This is my main code</p> <pre><code>protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_of_button); ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1); ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2); ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3); ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4); ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5); ImageButton btn6 = (ImageButton)findViewById(R.id.imageButton6); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); btn6.setOnClickListener(this); } @Override 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, Listviewact.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... } } private void setListAdapter(ArrayAdapter&lt;String&gt; arrayAdapter) { // TODO Auto-generated method stub } private ListView getListView() { // TODO Auto-generated method stub return null; } } </code></pre> <p>This is my listview code.</p> <pre><code> import android.app.Activity; import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class Listviewact extends Activity { public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.listview_layout); Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/AlexBrush-Regular-OTF.otf"); TextView tv = (TextView) findViewById(R.id.textView1); tv.setTypeface(tf); } 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.imageView1); 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.listView1); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { if(position == 0) { //code specific to first list item Intent myIntent = new Intent(view.getContext(), Information.class); startActivityForResult(myIntent, 0); } if(position == 1) { //code specific to 2nd list item Intent myIntent = new Intent(view.getContext(), Information.class); startActivityForResult(myIntent, 0); } } }); // When clicked, show a toast with the TextView text //Or do whatever you need. } public void onItemClick1(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } private void setListAdapter(ArrayAdapter adapter) { // TODO Auto-generated method stub } // 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.1, R.drawable.2, R.drawable.3, R.drawable.4, R.drawable.5, R.drawable.6 }; // 6 different sets of strings for the listviews private final static String[][] LISTVIEW_DATA = new String[][] { {"First A", "First B", "First C", "First D","First E","First F"}, {"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>What i would like to do, which i am finding it hard is when any of my listviews are clicked they all go to the same layout but have different data/infromation this image is the best way to show you guys. <a href="http://img40.imageshack.us/img40/705/f6h9.png" rel="nofollow">http://img40.imageshack.us/img40/705/f6h9.png</a>. I am thinking that if i can have one class which can be reused and not have like 50 activity slowing things down would be great.</p> <p>The image is what i would like to do. If possible have text on one side i.e HOTEL: PETS,VIEWS,PARKING,CHILDREN. Then i would like to call different information for the reslut on the left hand side. NO pets parking YES etc.</p>
 

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