I have created an Adapter arraylist that uses a LayoutInflater so images keep ratio. I am trying to perform a simple toast of the item title selected i.e "Beef". Everthing workd as expected except I am getting an output from toast of, example
test com.sweatboxbbq.www.sweatboxbbq.Kansas City$MyAdaptor$Item@42aa9398
I know I am requesting a string value of and I'm getting it, but from the code, I can't figure out how to get what I want. I want to capture the title only so I can toast it back (for now then aventually do something else).
Example items.add(new Item( beef, R.drawable.mine_beef));
I would get a string value of "beef"
Maybe I can id the title's of these array items somehow and pull them from there with the onclick method?
below is my Java code
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;
}
}
}
}
Thank you for any help.