选择Toast Item - AdapterView / ArrayList

时间:2015-01-20 21:25:51

标签: java android arraylist onclicklistener android-adapterview

我创建了一个使用LayoutInflater的Adapter arraylist,因此图像保持比例。我正在尝试对所选项目标题进行简单的祝酒,即“牛肉”。 Everthing按预期工作,除了我从例如

的toast获得输出
  

测试com.sweatboxbbq.www.sweatboxbbq.Kansas City $ MyAdaptor $ Item @ 42aa9398

我知道我正在请求一个字符串值,而我正在获取它,但是从代码中,我无法弄清楚如何得到我想要的东西。 我只想抓住这个标题,这样我就可以把它重新给它(现在然后再做一些其他事情)。

示例items.add(new Item( beef, R.drawable.mine_beef));我会得到一个字符串值“beef”

也许我能以某种方式识别这些数组项目的标题并使用onclick方法将它们从那里拉出来?

下面是我的Java代码

public class KansasCity extends ActionBarActivity {

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

    final GridView gridView = (GridView) findViewById(R.id.meat_choice);
    gridView.setAdapter(new MyAdapter(this));


    //on click starts here
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        public void onItemClick(AdapterView<?> MyAdapter, View v, int position, long id)
        {
            String flavorPicked = "" +
                    String.valueOf(MyAdapter.getItemAtPosition(position));
            Toast.makeText(KansasCity.this, "Test " + flavorPicked,
                    Toast.LENGTH_LONG).show();
        }
    });

}


//gridview
public class MyAdapter extends BaseAdapter {
    public List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;


    public MyAdapter(Context context) {
        inflater = LayoutInflater.from(context);

        items.add(new Item( "Beef", R.drawable.mine_beef));
        items.add(new Item("Chicken", R.drawable.mine_chicken));
        items.add(new Item("Swine", R.drawable.mine_pork));
        items.add(new Item("Fish", R.drawable.mine_pork));
        items.add(new Item("Turkey", R.drawable.mine_pork));
        items.add(new Item("Sauce", R.drawable.mine_pork));

    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return items.get(i).drawableId;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = view;
        ImageView picture;
        TextView name;

        if (v == null) {
            v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
            v.setTag(R.id.text, v.findViewById(R.id.text));
        }

        picture = (ImageView) v.getTag(R.id.picture);
        name = (TextView) v.getTag(R.id.text);

        Item item = (Item) getItem(i);

        picture.setImageResource(item.drawableId);
        name.setText(item.name);

        return v;


    }

    private class Item {
        final String name;
        final int drawableId;

        Item(String name, int drawableId) {
            this.name = name;
            this.drawableId = drawableId;
        }



    }


}

   }

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以覆盖toString()类中的Item,并使其返回您要显示的字符串,或将name成员Item公开,并使用它喜欢

String flavorPicked = ((Item)MyAdapter.getItemAtPosition(position)).name;

答案 1 :(得分:0)

请删除“after flavorPicked

Toast.makeText(KansasCity.this,“Test”+ flavorPicked“,                     Toast.LENGTH_LONG).show();