如何从一个ArrayList中的GridView onItemClick中获取字符串中的一个片段? [英] How to get String from an ArrayList from a GridView onItemClick in a fragment?
本文介绍了如何从一个ArrayList中的GridView onItemClick中获取字符串中的一个片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图为gridview设置一个onclick监听器。 gridview显示文本和图像。当一个项目被点击时,我希望它从该项目获取文本,并根据一些if语句进行检查。这是我的班。由于这个事实,它在一个片段,我是新来的碎片,我有困难搞清楚。谢谢
导入这里...
公共类扩展SherlockFragment {
public view onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.activity_main,container,false);
GridView gridView =(GridView)view.findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(getActivity()));
gridView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent,View v,
int position,long id){
$ b $ if(SOMETHING.equals(Furniture)){
Toast.makeText(getActivity(),Furny,Toast.LENGTH_SHORT).show();
// THIS我在哪里,我不知道如何用
$ b}来代替某件事);
返回视图;
}
public class MyAdapter extends BaseAdapter
{
private List< Item> items = new ArrayList< Item>();
私人LayoutInflater inflater;
public MyAdapter(上下文上下文)
{
inflater = LayoutInflater.from(context);
items.add(new Item(Weapons,R.drawable.BOMB));
items.add(new Item(Tools,R.drawable.TOOL));
items.add(new Item(Furniture,R.drawable.CHAIR));
$ b @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;
}
@覆盖
public View getView(int i,View view,ViewGroup viewGroup)
{
View v =查看;
ImageView图片;
TextView名称;
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);
选项options = new BitmapFactory.Options();
options.inDither = false;
options.inScaled = false;
位图源= BitmapFactory.decodeResource(getResources(),item.drawableId,options);
final int maxSize = 120;
int outWidth;
int outHeight;
int inWidth = source.getWidth();
int inHeight = source.getHeight();
if(inWidth> inHeight){
outWidth = maxSize;
outHeight =(inHeight * maxSize)/ inWidth;
} else {
outHeight = maxSize;
outWidth =(inWidth * maxSize)/ inHeight;
}
位图resizedBitmap = Bitmap.createScaledBitmap(source,outWidth,outHeight,false);
picture.setImageBitmap(resizedBitmap);
name.setText(item.name);
return v;
}
私人课程项目
{
final字符串名称;
final int drawableId;
Item(String name,int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
}
解决方案
使用位置
param并获取该位置的商品
Item item =(Item)parent.getItemAtPosition(position);
String name = item.name;
,您可以将下面的内容移至单独的 .java
file
public class Item
{
String name;
int drawableId;
Item(String name,int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
I'm trying to set up an onclick listener for a gridview. The gridview shows text and an image. When an item is clicked, I want it to get the text from that item and check it against some if statements. Here is my class. Due to that fact that it in a fragment, and I'm new to fragments, I am having difficulty figuring it out. Thanks
IMPORTS HERE...
public class Items extends SherlockFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_main, container, false);
GridView gridView = (GridView)view.findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(getActivity()));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(SOMETHING.equals("Furniture")){
Toast.makeText(getActivity(), "Furny", Toast.LENGTH_SHORT).show();
//THIS IS WHERE I'M STUCK. I DON'T KNOW WHAT TO REPLACE "SOMETHING" WITH
}
}
});
return view;
}
public class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("Weapons", R.drawable.BOMB));
items.add(new Item("Tools", R.drawable.TOOL));
items.add(new Item("Furniture", R.drawable.CHAIR));
}
@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);
Options options = new BitmapFactory.Options();
options.inDither = false;
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(getResources(), item.drawableId, options);
final int maxSize = 120;
int outWidth;
int outHeight;
int inWidth = source.getWidth();
int inHeight = source.getHeight();
if(inWidth > inHeight){
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(source, outWidth, outHeight, false);
picture.setImageBitmap(resizedBitmap);
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;
}
}
}
}
解决方案
Use the position
param and get the item at that position
Item item= (Item) parent.getItemAtPosition(position);
String name = item.name;
and you can move the below to separate .java
file
public class Item
{
String name;
int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
这篇关于如何从一个ArrayList中的GridView onItemClick中获取字符串中的一个片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文