0

Ive searched for this all over the internet and there seems to be no simple explanation or tutorial on how to do this.

Basically, I want a layout that has a ListView where the user can click on an object and it will take them to the next layout.

In other words, using the listview as links to other layouts.

Everything Ive found on the internet has the end result of using a Toast... However I dont want a toast, i want to link to the next page.

j0k
  • 22,600
  • 28
  • 79
  • 90
  • The good practices in android are to have nice/clean activities (screens) so what is common is to open a new Activity(screen) on list tap. – Jordi Coscolla Jan 29 '13 at 12:00

4 Answers4

0

Your question is slightly confusing so I'm going to make an assumption.

Is [LinearLayout1 LinearLayout2 Breadcrumb] suppose to be navigation or tabs that when selected insert their corresponding content into the Main Content?

If so I would suggest using fragments for each piece of content. Then when you click the navigation/tab, perform an animation of the fragment which slides the content in and out.

See the google docs for how to use fragments: http://developer.android.com/guide/components/fragments.html

See another stackoverflow answer for how to do the slide animation: Android Fragments and animation or

http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

Community
  • 1
  • 1
AbuQauod
  • 919
  • 14
  • 30
0

Here is some code that outlines how to invoke an activity following a click on a list row. Hopefully you can adapt the Toast example you mention to make this work for you. The basic idea is that you launch a new Activity with a new Intent. You can pass any data you need from the listView row as an extra in the Intent.

final static String[] months = new String[] {"Jan","Feb","Mar"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.row_layout, R.id.text1, months);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    int intItem = (int)id;
    Intent intent= new Intent(this, SecondaryActivity.class);
    intent.putExtra("MONTH", intItem);
    startActivity(intent);
}
MarkMan
  • 184
  • 6
0

Why using ListView for it? Each row must lead to different layout? Its main benefits is in displaying dynamically changing data, but in your case data is constant, right?

Use vertical LinearLayout, fill it programmatically with "list elements", and add

leListComponent.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        startActivity(new Intent(YourActivity.this, TargetActivity.class));
    }
});

to each.

If i didn't get it, and you feel good of using some adapter, it can be like this:

public class LeWrapper {
    private String caption;
    private Class<? extends Activity> target;
    ...POJO here...
}

v.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
      //get leWrapper object from adapter
      startActivity(new Intent(MenuActivity.this, leWrapper.getTarget()));
  }
});

but its kinda overkill

Dmitriy Voronin
  • 649
  • 6
  • 19
0

Thanks for all the help guys. Sorry ive took my time replying. My solution is below :)

 public class FP_WL1_ListView extends Activity {

 private ListView lv1;

 private String lv_arr[]={"Exercise Bike", "Treadmill", "Cross Trainer", "Squats", "Lunges"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fitnessprograms_wlday_one);

        lv1=(ListView)findViewById(R.id.list);

        lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));


        lv1.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
          final TextView mTextView = (TextView)view;
          switch (position) {
            case 0:
             Intent newActivity0 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_sp.class);     
             startActivity(newActivity0);
            break;
            case 1:
                 Intent newActivity1 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Treadmill.class);     
                 startActivity(newActivity1);
                break;
            case 2:
                 Intent newActivity2 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Crosstrainer.class);     
                 startActivity(newActivity2);
                break;
            case 3:
                 Intent newActivity3 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Squats.class);     
                 startActivity(newActivity3);
                break;
            case 4:
                 Intent newActivity4 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Lunges.class);     
                 startActivity(newActivity4);
                break;


          }

      }
    });

    }   }      
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90