从Arraylist获取项目名称并进行比较

时间:2015-05-15 18:50:56

标签: java arrays arraylist

我有一个数组列表,声明为

ArrayList<Item> items = new ArrayList<Item>();

在下面的屏幕截图中,您可以找到其中的数组项:

enter image description here

现在如何获取该字符串并与现有字符串值进行比较

假设我有一个字符串名称"inbox",如何获得名为title的{​​{1}}。

inbox

SectionItem

public interface Item {

    public boolean isSection();

}

有人能建议我更好的处理方法吗?

3 个答案:

答案 0 :(得分:1)

使用ArrayList的get(int index)方法。 返回此列表中指定位置的元素。

在你的情况下

String itemTitle = items.get(index).getTitle(); // Here index represent position of item you want to get title from current array list

答案 1 :(得分:0)

唯一的方法是使用演员:

package com.triodefender.game; import java.util.Vector; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import jdk.nashorn.internal.runtime.Debug; public class Player { SpriteBatch batch = new SpriteBatch (); Sprite sprite = new Sprite (); //Animator animator; float cx, cy; float hp; Texture img = new Texture ("bullet.png"), imgp = new Texture("turret.png"); Vector<Bullet> bullets = new Vector<Bullet>(); void Debug () { Sprite debug = new Sprite (new Texture("debug.png"), 0, 0, 15, 15); debug.setPosition (sprite.getX(), sprite.getY()); batch.begin (); debug.draw (batch); batch.end (); debug.setPosition (cx, cy); batch.begin (); debug.draw (batch); batch.end(); } Player (com.badlogic.gdx.graphics.Color color) { //animator = new Animator(a); //sprite = new Sprite (imgp); //this works sprite = new Sprite (); sprite.setTexture(imgp); //this doesn't work sprite.setOriginCenter(); sprite.setColor(color); hp = 100f; } void Update (float ox, float oy, float rotation, boolean shoot) { cx = ox; cy = oy; sprite.setPosition(ox - sprite.getWidth()/2, oy - sprite.getHeight()/2); sprite.setRotation(-rotation); if (shoot) { AddBullet(); //animator.lockRow(0, 1, 5); } UpdateBullets(); } void AddBullet () { float l = sprite.getWidth()/2f; float sn = (float) Math.sin(Math.toRadians(-sprite.getRotation())); float cs = (float)Math.cos(Math.toRadians( -sprite.getRotation() )); float newpx = cx + (float)(l+16f)*sn; float newpy = cy + (float)(l+16f)*cs; bullets.add(new Bullet(new Sprite(img), newpx, newpy, -sprite.getRotation(), 10f, sprite.getColor())); } void UpdateBullets () { for (int i = 0; i < bullets.size(); i++) if (bullets.elementAt(i).Update() == false) { bullets.remove(i); i--; } } void DrawBullets () { for (int i = 0; i < bullets.size(); i++) bullets.elementAt(i).Draw(); } float getRotation () { return sprite.getRotation(); } void Draw () { //Debug (); batch.begin(); sprite.draw(batch); batch.end(); DrawBullets (); } }

虽然,在我看来,您应该//Save where you last were in the list. int index = mList.getFirstVisiblePosition(); View v = mList.getChildAt(0); int top = (v == null) ? 0 : v.getTop(); // Call to notify, or updated(change, remove, move, add) notifyDatasetChanged(); //Prevents the scroll to the new item at the new position mList.setSelectionFromTop(index, top); 作为界面的成员String sectiontitle = ((SectionItem)items.get(index)).getTitle();

答案 2 :(得分:0)

只需通过访问索引获取标题,如果您要比较字符串,则使用等于:

 String title;

如果你想遍历数组,请执行for循环:

Item