我成功创建了一个自定义网格视图(包含图像和文本)。现在我想写一个OnItemClickListener方法。我可以在OnItemClickListener方法上获得位置,但我希望能够点击“朋友”并看到“项目点击:朋友”
如何获取选择字符串而不是位置? 这是我的源代码
public class Item {
Bitmap image;
String title;
public Item(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public static Item item ;
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
item= data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
public static class RecordHolder {
public static TextView txtTitle;
ImageView imageItem;
}
}
public class MainActivity extends Activity {
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set grid view item
Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.home);
Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.personal);
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "User"));
gridArray.add(new Item(homeIcon, "House"));
gridArray.add(new Item(userIcon, "Friend"));
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "Personal"));
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "User"));
gridArray.add(new Item(homeIcon, "Building"));
gridArray.add(new Item(userIcon, "User"));
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "xyz"));
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid,
gridArray);
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Item Clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:3)
更改此
Toast.makeText(getApplicationContext(),
"Item Clicked: " + position, Toast.LENGTH_SHORT).show();
到
Toast.makeText(getApplicationContext(),
"Item Clicked: " + ((TextView) v.findViewById(R.id.item_text)).getText(), Toast.LENGTH_SHORT).show();
答案 1 :(得分:2)
我知道这个答案有点晚了但是我遇到了同样的问题,我通过使用setTag()
和getTag()
在可点击项目上使用标签解决了这个问题。我只想发布这种方法,以防将来帮助别人。为了使用这个方法和视图“保持模式”(就像你上面使用的那样),你只需要在对象内的另一个项目上设置“保存”你的数据。
例如:
您已拥有公开RecordHolder
公开TextView
对象,因此您可以在此TextView
上设置标记,如下所示:
holder.txtTitle.setTag("whatever you want")
然后在OnItemClickListener.onItemClick
方法中,您可以获得如下标记:
String tagValue = ((CustomGridViewAdapter.RecordHolder) view.getTag()).txtTitle.getTag().toString();
tagValue
将为whatever you want
(因此,如果您愿意,可以指定视图的名称)。如果您想要存储其他数据(如ID或引用)而不只是抓取TextView
或类似的文本,此方法很有效。如果您只想要TextView
中的相同文本,那么@ Giru的回答更合适。