[Solved] How do i display a new list after another list in Android [closed]


There is very simple way to do it. You can put many options along with chest in that string array bodyPart and you can create relevant string array like chestsWorkout for particular bodyPart. You can check which bodyPart user has clicked and then you can pass relevant string array like chestsWorkout with intent.

MainActivity.java

package com.example.opnanotherlistexample;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {
    String[] bodyPart = { "Chest", "Legs" };

    String[] chestsWorkout = { "Bench", "Pushups", "Pullups", "Incline bench",
            "Decline bench" };
    String[] legsWorkout = { "walk", "run"};

    private ListView listNew;
    private ArrayList<String> newarraylist;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listNew = (ListView) findViewById(R.id.listNew);
        newarraylist = new ArrayList<String>();
        for (int i = 0; i < bodyPart.length; i++) {
            newarraylist.add(bodyPart[i]);
        }
        adapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_dropdown_item_1line, newarraylist);
        listNew.setAdapter(adapter);
        listNew.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapter, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                String clickedItem = newarraylist.get(position).toString();
                Intent intent = new Intent(MainActivity.this,
                        SecondActivity.class);
                if(clickedItem.toLowerCase().equals("chest")){
                    intent.putExtra("parameters", chestsWorkout);
                }else if(clickedItem.toLowerCase().equals("legs")){
                    intent.putExtra("parameters", legsWorkout);
                }
                startActivity(intent);

            }
        });
    }

}

SecondActivity.java

package com.example.opnanotherlistexample;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SecondActivity extends Activity {
    private ListView listAnother;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> arraylist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
        listAnother = (ListView) findViewById(R.id.listAnother);
        if (getIntent().hasExtra("parameters")) {
            String[] params = getIntent().getStringArrayExtra("parameters");
            arraylist = new ArrayList<String>();
            for (int i = 0; i < params.length; i++) {
                arraylist.add(params[i]);
                Log.e("balvier", params[i]);
            }
            adapter = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_dropdown_item_1line, arraylist);
            listAnother.setAdapter(adapter);
        }
    }

}

opnanotherlistexample manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.opnanotherlistexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity" />
    </application>

</manifest>

1

solved How do i display a new list after another list in Android [closed]