Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当用户单击按钮时显示列表视图_Android_Listview - Fatal编程技术网

Android 当用户单击按钮时显示列表视图

Android 当用户单击按钮时显示列表视图,android,listview,Android,Listview,我做了一个迷你冒险游戏,玩家可以去北方、南方、东方、西方等等 现在我想让播放器保存一个清单,但我不知道如何制作GUI。我希望在用户单击inventory按钮时显示一个列表视图 视图的代码如下所示 package dev.game.adventure; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.*; import android.support.

我做了一个迷你冒险游戏,玩家可以去北方、南方、东方、西方等等

现在我想让播放器保存一个清单,但我不知道如何制作GUI。我希望在用户单击inventory按钮时显示一个
列表视图

视图的代码如下所示

package dev.game.adventure;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.*;
import android.support.annotation.Nullable;
import android.os.*;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.*;
import android.widget.*;
import java.util.Collection;
import java.util.Iterator;


/**
 * An adventure that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class AdventureActivity extends AppCompatActivity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;
    PlaceView ag;

    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * Some older devices needs a small delay between UI widget updates
     * and a change of the status and navigation bar.
     */
    private static final int UI_ANIMATION_DELAY = 300;
    private final Handler mHideHandler = new Handler();
    private View mContentView;
    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            // Delayed removal of status and navigation bar

            // Note that some of these constants are new as of API 16 (Jelly Bean)
            // and API 19 (KitKat). It is safe to use them, as they are inlined
            // at compile-time and do nothing on earlier devices.
            mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    };
    private View mControlsView;
    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            // Delayed display of UI elements
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.show();
            }
            mControlsView.setVisibility(View.VISIBLE);
        }
    };
    private boolean mVisible;
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };
    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };

    TextView scrollable;

    static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
            "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
            "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        scrollable = (TextView) findViewById(R.id.textView1);
        scrollable.setMovementMethod(new ScrollingMovementMethod());
        ag = findViewById(R.id.view);
        ag.text = scrollable;
        ag = findViewById(R.id.view);
        new Adventure(scrollable, this, ag);
        Button buttonOne = (Button) findViewById(R.id.close);
        buttonOne.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ag.mainCharacter.go("North", ag.target);
                ag.display("North", ag.target, ag.mainCharacter.place);
            }
        });

        Button buttonOne2 = (Button) findViewById(R.id.close2);
        buttonOne2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ag.mainCharacter.go("East", ag.target);
                ag.display("East", ag.target, ag.mainCharacter.place);
            }
        });

        Button buttonOne3 = (Button) findViewById(R.id.close3);
        buttonOne3.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ag.mainCharacter.go("South", ag.target);
                ag.display("South", ag.target, ag.mainCharacter.place);
            }
        });

        Button buttonOne4 = (Button) findViewById(R.id.close4);
        buttonOne4.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ag.mainCharacter.go("West", ag.target);
                ag.display("West", ag.target, ag.mainCharacter.place);
            }
        });

        Button buttonOne5 = (Button) findViewById(R.id.close5);
        buttonOne5.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ag.mainCharacter.go("Up", ag.target);
                ag.display("Up", ag.target, ag.mainCharacter.place);
            }
        });

        Button buttonOne6 = (Button) findViewById(R.id.close6);
        buttonOne6.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ag.mainCharacter.go("Down", ag.target);
                ag.display("Down", ag.target, ag.mainCharacter.place);
            }
        });

        Button buttonOne7 = (Button) findViewById(R.id.close7);
        buttonOne7.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                ag.mainCharacter.say("Where is the key?", ag.target);

                for (Iterator<Person> i = ag.persons.iterator(); i.hasNext();) {
                    Person item = i.next();
                    if (item instanceof WalkingPerson) {
                        WalkingPerson item2 = (WalkingPerson) item;
                        item2.query(ag.mainCharacter, ag.target);
                    }
                }
            }
        });


        Button buttonOne8 = (Button) findViewById(R.id.close7);
        buttonOne7.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {



            }
        });

        ag.mainCharacter.goTo("Dungeon", this);
        ag.display("", this, ag.mainCharacter.place);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        // delayedHide(100);
    }

    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mControlsView.setVisibility(View.GONE);
        mVisible = false;

        // Schedule a runnable to remove the status and navigation bar after a delay
        mHideHandler.removeCallbacks(mShowPart2Runnable);
        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }

    @SuppressLint("InlinedApi")
    private void show() {
        // Show the system bar
        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        mVisible = true;

        // Schedule a runnable to display UI elements after a delay
        mHideHandler.removeCallbacks(mHidePart2Runnable);
        mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
    }

    /**
     * Schedules a call to hide() in delay milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
}

class PlaceView extends View {
    public PlaceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        NBRSTEPS = 4;//difficulty;
        heroName = "Jamie";//name;
        target = (AdventureActivity) context;

        this.cont = context;
        x = new int[NBRSTEPS];
        y = new int[NBRSTEPS];
        this.target = target;
        hero_positions = new int[NBRSTEPS];
        int resourceIdFalling = 0;
        int resourceIdHero = 0;

        falling = BitmapFactory.decodeResource(getResources(), resourceIdFalling); //load a falling image
        hero = BitmapFactory.decodeResource(getResources(), resourceIdHero); //load a hero image
    }

    public Collection<Person> getPersons() {
        return persons;
    }

    Person mainCharacter;

    public void setPersons(Collection<Person> persons) {
        this.persons = persons;
    }

    public void display(String s1, AdventureActivity target, Place p) {
        setBackground(p.image);
        target.scrollable.append("This place is called " + p.getName() + ".");
        if(p.getName().equals("Heaven")) {
            final Toast toast = Toast.makeText(target.ag.getContext(), "GAME OVER!\n", Toast.LENGTH_LONG);// duration);
            toast.show();
        }

    }

    /**
     * Persons at this Place.
     */
    public Collection<Person> persons;
    int NBRSTEPS; // number of discrete positions in the x-dimension; must be uneven
    String heroName;
    int[] x; // x-coordinates for falling objects
    int[] y; // y-coordinates for falling objects
    int[] hero_positions; // x-coordinates for hero
    int ballW; // width of each falling object
    int ballH; // height of ditto
    Bitmap falling, hero;

    Context cont;
    TextView text;
    AdventureActivity target;



    // update the canvas in order to display the game action
    @Override
    public void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        int xx = 200;
        int yy = 0;
        if (persons != null) {
            synchronized (persons) {
                Iterator<Person> iterate = persons.iterator();
                while (iterate.hasNext()) {
                    Person p = iterate.next();
                    if (p.getImage() != 0) {
                        hero = BitmapFactory.decodeResource(getResources(), p.getImage()); //load a character image
                        // Draw the visible person's appearance
                        //  g.drawImage(appearance, xx,
                        //          240 + yy - (appearance.getHeight(this) * 4) / 5,
                        //          this);
                        canvas.drawBitmap(hero, xx, 0, null); //Draw the hero on the canvas.

                        // Draw the name
                        Paint paint = new Paint();
                        paint.setStyle(Paint.Style.FILL);

                        canvas.save();
                       // canvas.translate(100, 200);
                        paint.setStrokeWidth(1);
                        paint.setColor(Color.WHITE);
                        paint.setTextSize(50);
                        canvas.drawText(p.name, 0, 0, paint);
                        xx += 500;
                        yy = (int) (Math.random() * 20);
                    }
                }
            }
        }
        canvas.save(); //Save the position of the canvas.
        canvas.restore();
        //Call the next frame.
        invalidate();
    }
}
我从答案中尝试了以下代码,但它没有任何作用

ListView DynamicListView = new ListView(this);

final String[] DynamicListElements = new String[] {
        "Hello",
        "Hi",
        "Android Studio",
        "Java"
};

ArrayAdapter adapter = new ArrayAdapter (AdventureActivity.this, android.R.layout.simple_list_item_1, DynamicListElements);
DynamicListView.setAdapter(adapter);



TableRow element1 = (TableRow) findViewById(R.id.tableRow3);
element1.addView(DynamicListView);
如果您认为活动名称不同于MainActivity,请相应地更改它。在本例中,我使用MainActivity.this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, DynamicListElements);

DynamicListView.setAdapter(adapter);

linearLayout.addView(DynamicListView);

//this may not be needed if you are using exisitng layout
this.setContentView(linearLayout, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ArrayAdapter=新的ArrayAdapter(MainActivity.this,android.R.layout.simple\u list\u item\u 1,DynamicListElements);
DynamicListView.setAdapter(适配器);
linearLayout.addView(DynamicList视图);
//如果您使用的是现有布局,则可能不需要这样做
this.setContentView(linearLayout,newlinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

您是否尝试过以编程方式创建它,然后将其添加到布局中?@ManjunathaMuniyappa我不知道怎么做。没有好的例子。或者,如果你能找到一个示例教程,那么我想要它。suer,我正在添加一个示例我使用现有的TableLayout。我不知道为什么它不起作用。它使一半的视图变成黑色。我用我的发现更新了这个问题。我尝试将ListView添加到TableLayout和TableRow中,但没有成功。我想我一定是做错了什么,但我不明白。
//create a new layout or use if you have one
LinearLayout linearLayout = new LinearLayout(this);

//creating listview dynamically
ListView DynamicListView = new ListView(this);

//lit items. Hardcoded here, you can set the values dynamically using ArrayList

final String[] DynamicListElements = new String[] {"Hello","Hi","Android Studio","Java"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, DynamicListElements);

DynamicListView.setAdapter(adapter);

linearLayout.addView(DynamicListView);

//this may not be needed if you are using exisitng layout
this.setContentView(linearLayout, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));