borislemke
borislemke

Reputation: 9116

Click ListView item, opens activity that shows layout depending on what item was clicked?

in my app, I have a MainActivity that consist of many items that can be clicked(ListView). If an item is clicked, I want it to open the next activity(ex. NextActivity) that displays an xml layout which is relative to which item the user has clicked on the previous screen. This is my complete MainActivity code:

public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;

static final String[] title = new String[] {
    "Temaki Sushi", "Oyakodon", "Okonomiyaki", "Tofu Dango", "Oden", //5
    "Nikujaga", "Yellowtail Teriyaki", "Tendon", "Tonjiru", "Sukiyaki", //10
    };

private Integer[] releaseid =  {
      R.drawable.old, R.drawable.old, R.drawable.old, R.drawable.old,  R.drawable.old, //5
      R.drawable.old, R.drawable.old, R.drawable.old, R.drawable.old, R.drawable.old, //10
    };

static final String[] description = new String[] {
    "Variation of Sushis from fresh vegetables and seafoods! Good for family occassions!",
    "Oyakodon is a Japanese Rice Bowl dish with Chicken, Eggs and various sorts of healthy and delicious Veggetables!",
    "Japanese assorted Pancake that is made from many different ingredients. The taste is so delicious!",
    "Japanese Dumplings made of Rice Flour. This one of the healthiest sweets in Japan!",
    "Japanese assorted stews. Made from many different kind of veggetables. Popular in Winter!", //5
    "Stew made of Beef and Veggetables. Very delicious and delightful! Yummy!",
    "new era",
    "new era",
    "new era",
    "new era", //10
    };

private Integer[] difficultyid =  {
      R.drawable.medium, R.drawable.medium, R.drawable.medium, R.drawable.easy, R.drawable.hard, //5
      R.drawable.medium, R.drawable.hard, R.drawable.medium, R.drawable.hard, R.drawable.hard, //10
    };

private Integer[] timeid = {
      R.drawable.fifteen, R.drawable.thirty, R.drawable.fifteen, R.drawable.fifteen, R.drawable.ninety, //5
      R.drawable.fourtyfive, R.drawable.sixty, R.drawable.fourtyfive, R.drawable.fourtyfive, R.drawable.fifteen, //10
    };

private Integer[] servesid = {
      R.drawable.three,R.drawable.one, R.drawable.three, R.drawable.four,R.drawable.four, //5
      R.drawable.one,R.drawable.two, R.drawable.one,R.drawable.one, R.drawable.two, //10
    };

private Integer[] imgid = {
R.drawable.thumbtemaki, R.drawable.thumboyakodon, R.drawable.thumbokonomiyaki, R.drawable.thumbdango, R.drawable.thumboden, //5
R.drawable.thumbnikujaga, R.drawable.thumbyellowtail, R.drawable.thumbtendon, R.drawable.thumbtonjiru, R.drawable.thumbsukiyaki, //10
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.aboutapp:
    startActivity(new Intent(this, AboutActivity.class));
    return true;
case R.id.contact:
    startActivity(new Intent(this, ContactActivity.class));
    return true;
case R.id.screeninfo:
    startActivity(new Intent(this, ScreenInfoActivity.class));
    return true;
default:
    return super.onOptionsItemSelected(item);
}
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
overridePendingTransition(R.anim.enter, R.anim.leave);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){

try {
rd = new RowData(i,title[i],i,description[i],i,i,i);
} catch (ParseException e) {
    e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list, android.R.id.list, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}


protected void onListItemClick(ListView listView, View view, int position, long id) {
    switch( position )
    {
       case 0:  Intent newActivity0 = new Intent(this, TemakiActivity.class);     
                startActivity(newActivity0);
                break;
       case 1:  Intent newActivity1 = new Intent(this, OyakodonActivity.class);     
                startActivity(newActivity1);
                break;
       case 2:  Intent newActivity2 = new Intent(this, OkonomiyakiActivity.class);     
                startActivity(newActivity2);
                break;
       case 3:  Intent newActivity3 = new Intent(this, DangoActivity.class);     
                startActivity(newActivity3);
                break;
       case 4:  Intent newActivity4 = new Intent(this, OdenActivity.class);     
                startActivity(newActivity4);
                break;
       case 5:  Intent newActivity5 = new Intent(this, NikujagaActivity.class);     
                startActivity(newActivity5);
                break;
       case 6:  Intent newActivity6 = new Intent(this, YellowtailActivity.class);     
                startActivity(newActivity6);
                break;
       case 7:  Intent newActivity7 = new Intent(this, TendonActivity.class);     
                startActivity(newActivity7);
                break;
       case 8:  Intent newActivity8 = new Intent(this, TonjiruActivity.class);     
                startActivity(newActivity8);
                break;
       case 9:  Intent newActivity9 = new Intent(this, SukiyakiActivity.class);     
                startActivity(newActivity9);
                break;

    }
}
   private class RowData {
   protected int mId;
   protected String mTitle;
   protected int mRelease;
   protected String mDescription;
   protected int mDifficulty;
   protected int mServes;
   protected int mTime;
   RowData(int id, String title, int release, String description, int difficulty, int serves, int time){
   mId=id;
   mTitle = title;
   mRelease = release;
   mDescription = description;
   mDifficulty = difficulty;
   mServes = serves;
   mTime = time;
}
   @Override
   public String toString() {
           return mId+" "+mTitle+" "+mRelease+" "+mDescription+" "+mDifficulty+" "+mServes+" "+mTime;
   }
}
private class CustomAdapter extends ArrayAdapter<RowData> {

public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {               

super(context, resource, textViewResourceId, objects);
}
  @Override
   public View getView(int position, View convertView, ViewGroup parent) {   

   ViewHolder holder = null;
   TextView title = null;
   ImageView release = null;
   TextView description = null;
   ImageView difficulty = null;
   ImageView serves = null;
   ImageView time = null;
   ImageView thumbnail = null;
   RowData rowData = getItem(position);
   if(null == convertView){
        convertView = mInflater.inflate(R.layout.list, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
 }
         holder = (ViewHolder) convertView.getTag();
         title = holder.gettitle();
         title.setText(rowData.mTitle);

         release = holder.getrelease();
         release.setImageResource(releaseid[rowData.mRelease]);

         description = holder.getdescription();
         description.setText(rowData.mDescription);

         difficulty = holder.getdifficulty();
         difficulty.setImageResource(difficultyid[rowData.mDifficulty]);

         serves = holder.getserves();
         serves.setImageResource(servesid[rowData.mServes]);

         time = holder.gettime();
         time.setImageResource(timeid[rowData.mTime]);

         thumbnail = holder.getImage();
         thumbnail.setImageResource(imgid[rowData.mId]);
         return convertView;
 }
        private class ViewHolder {
        private View mRow;
        private TextView title = null;
        private ImageView release = null;
        private TextView description = null;
        private ImageView difficulty = null;
        private ImageView serves = null;
        private ImageView time = null;
        private ImageView thumbnail = null; 

        public ViewHolder(View row) {
        mRow = row;
 }
    public TextView gettitle() {
         if(null == title){
             title = (TextView) mRow.findViewById(R.id.titleviewer);
            }
        return title;
     }     

    public ImageView getrelease() {
        if(null == release){
            release = (ImageView) mRow.findViewById(R.id.releaseviewer);
           }
       return release;
    } 

     public TextView getdescription() {
         if(null == description){
              description = (TextView) mRow.findViewById(R.id.descriptionviewer);
                }
       return description;
     }
     public ImageView getdifficulty() {
         if(null == difficulty){
             difficulty = (ImageView) mRow.findViewById(R.id.difficultyviewer);
            }
        return difficulty;
     }  
     public ImageView getserves() {
         if(null == serves){
             serves = (ImageView) mRow.findViewById(R.id.servesviewer);
            }
        return serves;
     }  
     public ImageView gettime() {
         if(null == time){
             time = (ImageView) mRow.findViewById(R.id.timeviewer);
            }
        return time;
     }  

    public ImageView getImage() {
         if(null == thumbnail){
              thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail);
                                  }
            return thumbnail;
            }
        }            
    }
}

How do I code the NextActivity to show layouts based on item click? For example, if item 0 was clicked, setContentView(R.layout.item0), if item 1 was clicked, setContentView(R.layout.item1) and so on?

Upvotes: 0

Views: 2609

Answers (2)

Waza_Be
Waza_Be

Reputation: 39538

In MainActivity in the onListItemClick section:

Bundle bundle = new Bundle();
bundle.putint("Layout", position);

Intent newIntent = new Intent(this.getApplicationContext(), NextActivity.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);

and the second part is like sunny told:

Bundle bundle = this.getIntent().getExtras();
int chooser = bundle.getInt("Layout");

switch(chooser) {
case 0:
setContentView(R.layout.main0);
break;

case 1:
setContentView(R.layout.main1);
break;
......
......
} and so o

Upvotes: 1

Sunny
Sunny

Reputation: 14808

You can take a integer variable stored using sharedPreference and initially set to 0. change the value of integer on activity start and load the setContentView according to that variable.

http://developer.android.com/reference/android/content/SharedPreferences.html

http://developer.android.com/guide/topics/data/data-storage.html

for example

 int chooser;      // sharedPrefernce variable in your mainActivity 

//this is your activity for diiferent layouts

switch(chooser) {
case 0;
setContentView(R.layout.main0);
break;

case 1:
setContentView(R.layout.main1);
break;
......
......

} and so on............

Upvotes: 0

Related Questions