如何将数组适配器填充到menuscreen.xml

时间:2016-05-12 04:36:28

标签: java android android-layout android-studio android-arrayadapter

我有一个arrayadapter,我无法通过点击按钮填充我的menuscreen.xml屏幕

这是我的MainActivity 我试图设置一个按钮和onclick听众,但是无法正确设置

package com.example.vitoriano_vaz.eastbayvapes;

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

/*
This app is for a local Vape Shop
 it's purpose is to increase customer flow, and provide a menu for Android users

 the array is going to be called = da_menu
 */
public class MainActivity extends AppCompatActivity {
    private static Intent java;

    /*
    called when the user clicks the send Button
     */

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

        /*
        sends user to about us xml via AboutActivity.class
         */
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(intent);
            }
        });

        /*
    this is the button call that sends user to the menuscreen.xml
     */
        Button menubutton = (Button)findViewById(R.id.menubutton);
        button.setOnClickListener(new view.OnClickListener);




        populateListView();
    }


    private void populateListView() {
        // Create the list of items

        String[] juiceMenu= {
                "#1 BLUEBERRY BOMBSHELL \nThis blueberry dessert will keep your taste buds begging for more",
                "#2 RICHIE RICH\nShout out to Rich! This was his all day vape. Strawberry cream with a bunch of deliciousness",
                "#3 CHIQUITA\nCreamy Banana with a bready after taste",
                "#4 AFTERNOON DELIGHT\nGot that 3 o'clock feeling? Try a little Afternoon delight. Kiwi Strawberry Dessert",
                "#5 POPPIN OTTERS\nMelt an otterpop put it in your tank and vape it",
                "#6 VIVA LA SANGRIA\nFruit, with a little bit of fruit,  add a teaspoon of fruit mixed with white win and top it off with fruit",
                "#7 OKOLE MALUNA\nPineapple fruit blend. Bottom up!",
                "#8 CARMEN MIRANDA\nVanilla custard with a crisp",
                "#9 POMALADE\nIt's Bombalade! Yuour mother will love it",
                "#10 IZUAL\nWhirlwind of icy Fruits",
                "#11 BUTTER STOTCH\nProfessor Chaos will surely get grounded for this one!",
                "#12 BLUE BULLS\nGrab the bull by the horns",
                "#13 GRAPE APE\nA grapple a day will keep the doctor away",
                "#14 BRUCE JUICE\nPerfectly blended old fashioned. This one is for you Bruce",
                "#15 DOC HOLIDAY\nN.E.T. Tabacco flavor. Limited release",
                "#16 PEACHY KEEN\nPeach cobbler. Better than what your mama used to make",
                "#17 HULA\nStrawberry coconut? What in the hula?",
                "#18 NEW YORK\nPeppermint paddy dipped in chocolate...Amazing really",
                "#19 AL GORE\nIm super duper cereal right now!",
                "#20 LUX CHARMS\nHearts, stars, horseshoes, clovers & blue moons, pots of gold % rainbows & me red balloons",
                "#21 SAILOR JACK\nCant go to the ball game without some cracker jacks",
                "#22 GET HIM TO THE GREEK\n...Yogurt, peach, delightful",
                "#23 KEY WE LIE CHI\nFruit lovers will enjoy this lychee vape",
                "#24 SPRING FLING\nA menage a trois of honeydew and cream on your tongue",
                "#25 GUMBY\nWatermelon sour gummy",
                "#26 CHAI\nMILK and Chai Tea",
                "#27 MR. BBEAN\nCappuccino with cream to start the day off right",
                "#28 50 SHADES OF ORANGE\nA delightful blend of orange and cream",
                "#29 BLUE WAFFLES\nBreakfast for dinner with this Blueberry Waffle",
                "#30 ENIGMA\nEnigma conundrum of mango raspberry with a hint of mystery can you solve the puzzle?",
                "#31 MR. FREEZE\nPut down the tank and STEP AWAY hardcore mint lovers ONLY! This juice is too coll for most",
                "#32 CUP OF JOE\nThe best part of waking up is coffee in your vape",
                "#33 BON JOVI\nVanilla Bourbon ",
        };
        // Build adapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,               // context for the activity
                R.layout.da_menu,   // Layout to create
                juiceMenu);         // Items to be displayed

        //configure list view
        ListView list = (ListView) findViewById(R.id.listViewMain);
        list.setAdapter(adapter);
    }
}

这是我要将用户发送到

的屏幕
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollIndicators="right">

    <ListView
        android:id="@+id/listViewMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

具有按钮调用的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:src="@drawable/ebvbottle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vitoriano_vaz.eastbayvapes.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="East Bay Vapes"
        android:textAllCaps="true"
        android:textColor="#FEFEFF"
        android:textSize="28sp"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="About Us"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendMenu"
        android:text="Menu"
        android:id="@+id/menubutton"
        android:layout_alignTop="@+id/button"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

androidmaifest 如果它是相关的 在创建新的SecondActivity.java类后更新了android清单

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

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

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

da_menu.xml我使用它发送到android:id =&#34; @ + id / listViewMain&#34;

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</TextView>

4 个答案:

答案 0 :(得分:0)

这样做:

 button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    populateListView();
                }
            });

答案 1 :(得分:0)

你有2个屏幕 - 所以你需要2 Activity s。

您需要ArrayAdapter一秒Activity,例如“SecondActivity.java”

然后这样做:

button.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Intent secondActivityIntent = new Intent(this, SecondActivity.class);
         startActivity(secondActivity);
     }
});

您还需要删除mainactivity.xml中按钮的“onClick”属性。它将尝试调用Activity中名为“onClick(View view)”的方法,这是您不想要的。

您应该查看有关启动Activityhttp://developer.android.com/intl/es/training/basics/firstapp/starting-activity.html

的文档

编辑:

方法populateListView()应位于此SecondActivity中 - 您将填充并显示它。

答案 2 :(得分:0)

将意图传递给按钮点击列表器,并填写您希望在点击列表器上显示的屏幕。

答案 3 :(得分:0)

我认为你的问题是你在写

    Button menubutton = (Button)findViewById(R.id.menubutton);
    button.setOnClickListener(new view.OnClickListener);

将其替换为:

menubutton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
     populateListView();
 }
});