-1

i need help i'm a rookie in Android Development. I'm creating this Starbuzz App. The Top Level Screen Shows a Starbuck Logo With a Big TextView That says "Starbuzz Coffee" and it has a ListView with 3 item(Drinks,Food Store) when the user click on Drink, a new Screen pops up and here's the code for that in XML:

    <ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src = "@drawable/starbucks_logo"
    android:id = "@+id/image_id"
    android:layout_marginLeft = "10dp"/>
 <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text = "@string/starbuzz1"
   android:id = "@+id/starbuzz1_id"
   android:textSize = "54sp"
   android:layout_marginLeft = "120dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text = "@string/starbuzz2"
    android:id = "@+id/starbuzz2_id"
    android:textSize = "54sp"
    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/starbuzz1_id"
    android:layout_alignStart="@+id/starbuzz1_id"
    android:layout_marginTop="52dp" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/options"
    android:layout_below="@+id/starbuzz2_id"
    android:id = "@+id/list_2">

</ListView>

Here's the code that Populate the ListView in Java:

package com.example.android.starbuzz;

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

public class TopLevelActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.top_level_activity);

 AdapterView.OnItemClickListener itemClick = new AdapterView.OnItemClickListener(){

            public void onItemClick(AdapterView <?> listView, View itemView, int position, long id){

          if(position == 0){
          Intent firstIntent = new Intent(TopLevelActivity.this, Drinks.class);
          startActivity(firstIntent);
                }
 }

        };

        ListView theList = (ListView) findViewById(R.id.list_2);
        theList.setOnItemClickListener(itemClick);

    }

}

Everything work perfectly fine here. When the user click on "Drinks" The app goes to another activity and shows a screen with a ListView with 3 item(Latte, Cappuccino, Filter)

Here's the XML code for that:

 <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id = "@+id/list_id">


    </ListView>

Here's the code in Java, that Populate the list:

package com.example.android.starbuzz;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Drinks extends Activity {

    ListView myList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drinks);


        String [] drinks =  {"Latte", "Cappuccino", "Filter"};

        ListAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, drinks);

        myList = (ListView) findViewById(R.id.list_id);

        myList.setAdapter(listAdapter);


        AdapterView.OnItemClickListener listen = new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                if(position == 0){


                    Intent intents = new Intent(Drinks.this, DrinkDetails.class);
                    startActivity(intents);

                }

            }
        };

       myList.setOnItemClickListener(listen);

    }



}

THIS IS WHERE EVERYTHING GOES WRONG, When the user click on Latte the app crash. What i want is when the user click on Latte, i want another activity to start, and a picture of a latte to show up, and a description of the latte.

Here's the code in XML of me Attempting to accomplish this:

<ImageView
       android:layout_width="200dp"
       android:layout_height="wrap_content"

       android:layout_marginLeft="5dp"
       android:id = "@+id/image_id"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id = "@+id/text_1_id"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id = "@+id/text_2_id"/>

Here's the code of me Populating the views in Java:

package com.example.android.starbuzz;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class DrinkDetails  extends Activity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drink_details);

    ImageView image = (ImageView) findViewById(R.id.image_id);
        image.setImageResource(R.drawable.latte_logo);

        TextView drinkName = (TextView) findViewById(R.id.text_1_id);
        drinkName.setText(R.string.latte);

        TextView drinkDescription = (TextView) findViewById(R.id.text_2_id);
        drinkDescription.setText(R.string.latteDescription);
    }
}

When i ran the app no error shows up, but the app crash when a user tap Latte.

Mykey
  • 3
  • 1
  • 4

1 Answers1

-1

You can refer to this post How to handle the click event in Listview in android? and add onItemClicklistener on it. and then put your logic here

 lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            if(position == 0){

            Intent intent = new Intent(Drinks.this, DrinkDetails.class);
            startActivity(intent);

            }
        }
    });
Sardar Khan
  • 845
  • 6
  • 16